blob: 6ff617fc178bf1ef7f7ea5d3d5a95a3f4bc9df36 [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 Bataeved09d242014-05-28 05:53:51 +0000838
Alexey Bataev758e55e2013-09-06 18:03:48 +0000839public:
840 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000841 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000842 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000843 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
844 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000845
846 SourceLocation ELoc = E->getExprLoc();
847
848 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
849 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
850 if (DVar.CKind != OMPC_unknown) {
851 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000852 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000853 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000854 return;
855 }
856 // The default(none) clause requires that each variable that is referenced
857 // in the construct, and does not have a predetermined data-sharing
858 // attribute, must have its data-sharing attribute explicitly determined
859 // by being listed in a data-sharing attribute clause.
860 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataevf29276e2014-06-18 04:14:57 +0000861 (isOpenMPParallelDirective(DKind) || DKind == OMPD_task)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000862 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000863 SemaRef.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000864 return;
865 }
866
867 // OpenMP [2.9.3.6, Restrictions, p.2]
868 // A list item that appears in a reduction clause of the innermost
869 // enclosing worksharing or parallel construct may not be accessed in an
870 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000871 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev23b69422014-06-18 07:08:49 +0000872 MatchesAlways());
Alexey Bataevc5e02582014-06-16 07:08:35 +0000873 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
874 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000875 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
876 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000877 return;
878 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000879
880 // Define implicit data-sharing attributes for task.
881 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000882 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
883 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000884 }
885 }
886 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000887 for (auto C : S->clauses())
888 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000889 for (StmtRange R = C->children(); R; ++R)
890 if (Stmt *Child = *R)
891 Visit(Child);
892 }
893 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000894 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
895 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000896 if (Stmt *Child = *I)
897 if (!isa<OMPExecutableDirective>(Child))
898 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000899 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000900
901 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000902 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000903
Alexey Bataev7ff55242014-06-19 09:13:45 +0000904 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
905 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000906};
Alexey Bataeved09d242014-05-28 05:53:51 +0000907} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000908
Alexey Bataevbae9a792014-06-27 10:37:06 +0000909void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000910 switch (DKind) {
911 case OMPD_parallel: {
912 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
913 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000914 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000915 std::make_pair(".global_tid.", KmpInt32PtrTy),
916 std::make_pair(".bound_tid.", KmpInt32PtrTy),
917 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000918 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000919 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
920 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000921 break;
922 }
923 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000924 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000925 std::make_pair(StringRef(), QualType()) // __context with shared vars
926 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000927 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
928 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000929 break;
930 }
931 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000932 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000933 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000934 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000935 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
936 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000937 break;
938 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000939 case OMPD_sections: {
940 Sema::CapturedParamNameType Params[] = {
941 std::make_pair(StringRef(), QualType()) // __context with shared vars
942 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000943 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
944 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000945 break;
946 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000947 case OMPD_section: {
948 Sema::CapturedParamNameType Params[] = {
949 std::make_pair(StringRef(), QualType()) // __context with shared vars
950 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000951 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
952 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000953 break;
954 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000955 case OMPD_single: {
956 Sema::CapturedParamNameType Params[] = {
957 std::make_pair(StringRef(), QualType()) // __context with shared vars
958 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000959 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
960 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000961 break;
962 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000963 case OMPD_threadprivate:
964 case OMPD_task:
965 llvm_unreachable("OpenMP Directive is not allowed");
966 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000967 llvm_unreachable("Unknown OpenMP directive");
968 }
969}
970
Alexey Bataev549210e2014-06-24 04:39:47 +0000971bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
972 OpenMPDirectiveKind CurrentRegion,
973 SourceLocation StartLoc) {
974 if (Stack->getCurScope()) {
975 auto ParentRegion = Stack->getParentDirective();
976 bool NestingProhibited = false;
977 bool CloseNesting = true;
978 bool ShouldBeInParallelRegion = false;
979 if (isOpenMPSimdDirective(ParentRegion)) {
980 // OpenMP [2.16, Nesting of Regions]
981 // OpenMP constructs may not be nested inside a simd region.
982 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
983 return true;
984 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000985 if (CurrentRegion == OMPD_section) {
986 // OpenMP [2.7.2, sections Construct, Restrictions]
987 // Orphaned section directives are prohibited. That is, the section
988 // directives must appear within the sections construct and must not be
989 // encountered elsewhere in the sections region.
990 if (ParentRegion != OMPD_sections) {
991 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
992 << (ParentRegion != OMPD_unknown)
993 << getOpenMPDirectiveName(ParentRegion);
994 return true;
995 }
996 return false;
997 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000998 if (isOpenMPWorksharingDirective(CurrentRegion) &&
999 !isOpenMPParallelDirective(CurrentRegion) &&
1000 !isOpenMPSimdDirective(CurrentRegion)) {
1001 // OpenMP [2.16, Nesting of Regions]
1002 // A worksharing region may not be closely nested inside a worksharing,
1003 // explicit task, critical, ordered, atomic, or master region.
1004 // TODO
1005 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) &&
1006 !isOpenMPSimdDirective(ParentRegion);
1007 ShouldBeInParallelRegion = true;
1008 }
1009 if (NestingProhibited) {
1010 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
1011 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << true
1012 << getOpenMPDirectiveName(CurrentRegion) << ShouldBeInParallelRegion;
1013 return true;
1014 }
1015 }
1016 return false;
1017}
1018
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001019StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1020 ArrayRef<OMPClause *> Clauses,
1021 Stmt *AStmt,
1022 SourceLocation StartLoc,
1023 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001024 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1025
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001026 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +00001027 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
1028 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001029
1030 // Check default data sharing attributes for referenced variables.
1031 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1032 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1033 if (DSAChecker.isErrorFound())
1034 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001035 // Generate list of implicitly defined firstprivate variables.
1036 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1037 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1038
1039 bool ErrorFound = false;
1040 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001041 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1042 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1043 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001044 ClausesWithImplicit.push_back(Implicit);
1045 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +00001046 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001047 } else
1048 ErrorFound = true;
1049 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001050
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001051 switch (Kind) {
1052 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001053 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1054 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001055 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001056 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +00001057 Res =
1058 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001059 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001060 case OMPD_for:
1061 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1062 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001063 case OMPD_sections:
1064 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1065 EndLoc);
1066 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001067 case OMPD_section:
1068 assert(ClausesWithImplicit.empty() &&
1069 "No clauses is allowed for 'omp section' directive");
1070 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1071 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001072 case OMPD_single:
1073 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1074 EndLoc);
1075 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001076 case OMPD_threadprivate:
1077 case OMPD_task:
1078 llvm_unreachable("OpenMP Directive is not allowed");
1079 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001080 llvm_unreachable("Unknown OpenMP directive");
1081 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001082
Alexey Bataeved09d242014-05-28 05:53:51 +00001083 if (ErrorFound)
1084 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085 return Res;
1086}
1087
1088StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1089 Stmt *AStmt,
1090 SourceLocation StartLoc,
1091 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001092 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1093 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1094 // 1.2.2 OpenMP Language Terminology
1095 // Structured block - An executable statement with a single entry at the
1096 // top and a single exit at the bottom.
1097 // The point of exit cannot be a branch out of the structured block.
1098 // longjmp() and throw() must not violate the entry/exit criteria.
1099 CS->getCapturedDecl()->setNothrow();
1100
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001101 getCurFunction()->setHasBranchProtectedScope();
1102
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001103 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1104 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001105}
1106
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001107namespace {
1108/// \brief Helper class for checking canonical form of the OpenMP loops and
1109/// extracting iteration space of each loop in the loop nest, that will be used
1110/// for IR generation.
1111class OpenMPIterationSpaceChecker {
1112 /// \brief Reference to Sema.
1113 Sema &SemaRef;
1114 /// \brief A location for diagnostics (when there is no some better location).
1115 SourceLocation DefaultLoc;
1116 /// \brief A location for diagnostics (when increment is not compatible).
1117 SourceLocation ConditionLoc;
1118 /// \brief A source location for referring to condition later.
1119 SourceRange ConditionSrcRange;
1120 /// \brief Loop variable.
1121 VarDecl *Var;
1122 /// \brief Lower bound (initializer for the var).
1123 Expr *LB;
1124 /// \brief Upper bound.
1125 Expr *UB;
1126 /// \brief Loop step (increment).
1127 Expr *Step;
1128 /// \brief This flag is true when condition is one of:
1129 /// Var < UB
1130 /// Var <= UB
1131 /// UB > Var
1132 /// UB >= Var
1133 bool TestIsLessOp;
1134 /// \brief This flag is true when condition is strict ( < or > ).
1135 bool TestIsStrictOp;
1136 /// \brief This flag is true when step is subtracted on each iteration.
1137 bool SubtractStep;
1138
1139public:
1140 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1141 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1142 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1143 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1144 SubtractStep(false) {}
1145 /// \brief Check init-expr for canonical loop form and save loop counter
1146 /// variable - #Var and its initialization value - #LB.
1147 bool CheckInit(Stmt *S);
1148 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1149 /// for less/greater and for strict/non-strict comparison.
1150 bool CheckCond(Expr *S);
1151 /// \brief Check incr-expr for canonical loop form and return true if it
1152 /// does not conform, otherwise save loop step (#Step).
1153 bool CheckInc(Expr *S);
1154 /// \brief Return the loop counter variable.
1155 VarDecl *GetLoopVar() const { return Var; }
1156 /// \brief Return true if any expression is dependent.
1157 bool Dependent() const;
1158
1159private:
1160 /// \brief Check the right-hand side of an assignment in the increment
1161 /// expression.
1162 bool CheckIncRHS(Expr *RHS);
1163 /// \brief Helper to set loop counter variable and its initializer.
1164 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1165 /// \brief Helper to set upper bound.
1166 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1167 const SourceLocation &SL);
1168 /// \brief Helper to set loop increment.
1169 bool SetStep(Expr *NewStep, bool Subtract);
1170};
1171
1172bool OpenMPIterationSpaceChecker::Dependent() const {
1173 if (!Var) {
1174 assert(!LB && !UB && !Step);
1175 return false;
1176 }
1177 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1178 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1179}
1180
1181bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1182 // State consistency checking to ensure correct usage.
1183 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1184 !TestIsLessOp && !TestIsStrictOp);
1185 if (!NewVar || !NewLB)
1186 return true;
1187 Var = NewVar;
1188 LB = NewLB;
1189 return false;
1190}
1191
1192bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1193 const SourceRange &SR,
1194 const SourceLocation &SL) {
1195 // State consistency checking to ensure correct usage.
1196 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1197 !TestIsLessOp && !TestIsStrictOp);
1198 if (!NewUB)
1199 return true;
1200 UB = NewUB;
1201 TestIsLessOp = LessOp;
1202 TestIsStrictOp = StrictOp;
1203 ConditionSrcRange = SR;
1204 ConditionLoc = SL;
1205 return false;
1206}
1207
1208bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1209 // State consistency checking to ensure correct usage.
1210 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1211 if (!NewStep)
1212 return true;
1213 if (!NewStep->isValueDependent()) {
1214 // Check that the step is integer expression.
1215 SourceLocation StepLoc = NewStep->getLocStart();
1216 ExprResult Val =
1217 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1218 if (Val.isInvalid())
1219 return true;
1220 NewStep = Val.get();
1221
1222 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1223 // If test-expr is of form var relational-op b and relational-op is < or
1224 // <= then incr-expr must cause var to increase on each iteration of the
1225 // loop. If test-expr is of form var relational-op b and relational-op is
1226 // > or >= then incr-expr must cause var to decrease on each iteration of
1227 // the loop.
1228 // If test-expr is of form b relational-op var and relational-op is < or
1229 // <= then incr-expr must cause var to decrease on each iteration of the
1230 // loop. If test-expr is of form b relational-op var and relational-op is
1231 // > or >= then incr-expr must cause var to increase on each iteration of
1232 // the loop.
1233 llvm::APSInt Result;
1234 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1235 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1236 bool IsConstNeg =
1237 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1238 bool IsConstZero = IsConstant && !Result.getBoolValue();
1239 if (UB && (IsConstZero ||
1240 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1241 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1242 SemaRef.Diag(NewStep->getExprLoc(),
1243 diag::err_omp_loop_incr_not_compatible)
1244 << Var << TestIsLessOp << NewStep->getSourceRange();
1245 SemaRef.Diag(ConditionLoc,
1246 diag::note_omp_loop_cond_requres_compatible_incr)
1247 << TestIsLessOp << ConditionSrcRange;
1248 return true;
1249 }
1250 }
1251
1252 Step = NewStep;
1253 SubtractStep = Subtract;
1254 return false;
1255}
1256
1257bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1258 // Check init-expr for canonical loop form and save loop counter
1259 // variable - #Var and its initialization value - #LB.
1260 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1261 // var = lb
1262 // integer-type var = lb
1263 // random-access-iterator-type var = lb
1264 // pointer-type var = lb
1265 //
1266 if (!S) {
1267 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1268 return true;
1269 }
1270 if (Expr *E = dyn_cast<Expr>(S))
1271 S = E->IgnoreParens();
1272 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1273 if (BO->getOpcode() == BO_Assign)
1274 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1275 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1276 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1277 if (DS->isSingleDecl()) {
1278 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1279 if (Var->hasInit()) {
1280 // Accept non-canonical init form here but emit ext. warning.
1281 if (Var->getInitStyle() != VarDecl::CInit)
1282 SemaRef.Diag(S->getLocStart(),
1283 diag::ext_omp_loop_not_canonical_init)
1284 << S->getSourceRange();
1285 return SetVarAndLB(Var, Var->getInit());
1286 }
1287 }
1288 }
1289 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1290 if (CE->getOperator() == OO_Equal)
1291 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1292 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1293
1294 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1295 << S->getSourceRange();
1296 return true;
1297}
1298
Alexey Bataev23b69422014-06-18 07:08:49 +00001299/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001300/// variable (which may be the loop variable) if possible.
1301static const VarDecl *GetInitVarDecl(const Expr *E) {
1302 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001303 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001304 E = E->IgnoreParenImpCasts();
1305 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1306 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1307 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1308 CE->getArg(0) != nullptr)
1309 E = CE->getArg(0)->IgnoreParenImpCasts();
1310 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1311 if (!DRE)
1312 return nullptr;
1313 return dyn_cast<VarDecl>(DRE->getDecl());
1314}
1315
1316bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1317 // Check test-expr for canonical form, save upper-bound UB, flags for
1318 // less/greater and for strict/non-strict comparison.
1319 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1320 // var relational-op b
1321 // b relational-op var
1322 //
1323 if (!S) {
1324 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1325 return true;
1326 }
1327 S = S->IgnoreParenImpCasts();
1328 SourceLocation CondLoc = S->getLocStart();
1329 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1330 if (BO->isRelationalOp()) {
1331 if (GetInitVarDecl(BO->getLHS()) == Var)
1332 return SetUB(BO->getRHS(),
1333 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1334 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1335 BO->getSourceRange(), BO->getOperatorLoc());
1336 if (GetInitVarDecl(BO->getRHS()) == Var)
1337 return SetUB(BO->getLHS(),
1338 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1339 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1340 BO->getSourceRange(), BO->getOperatorLoc());
1341 }
1342 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1343 if (CE->getNumArgs() == 2) {
1344 auto Op = CE->getOperator();
1345 switch (Op) {
1346 case OO_Greater:
1347 case OO_GreaterEqual:
1348 case OO_Less:
1349 case OO_LessEqual:
1350 if (GetInitVarDecl(CE->getArg(0)) == Var)
1351 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1352 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1353 CE->getOperatorLoc());
1354 if (GetInitVarDecl(CE->getArg(1)) == Var)
1355 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1356 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1357 CE->getOperatorLoc());
1358 break;
1359 default:
1360 break;
1361 }
1362 }
1363 }
1364 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1365 << S->getSourceRange() << Var;
1366 return true;
1367}
1368
1369bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1370 // RHS of canonical loop form increment can be:
1371 // var + incr
1372 // incr + var
1373 // var - incr
1374 //
1375 RHS = RHS->IgnoreParenImpCasts();
1376 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1377 if (BO->isAdditiveOp()) {
1378 bool IsAdd = BO->getOpcode() == BO_Add;
1379 if (GetInitVarDecl(BO->getLHS()) == Var)
1380 return SetStep(BO->getRHS(), !IsAdd);
1381 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1382 return SetStep(BO->getLHS(), false);
1383 }
1384 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1385 bool IsAdd = CE->getOperator() == OO_Plus;
1386 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1387 if (GetInitVarDecl(CE->getArg(0)) == Var)
1388 return SetStep(CE->getArg(1), !IsAdd);
1389 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1390 return SetStep(CE->getArg(0), false);
1391 }
1392 }
1393 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1394 << RHS->getSourceRange() << Var;
1395 return true;
1396}
1397
1398bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1399 // Check incr-expr for canonical loop form and return true if it
1400 // does not conform.
1401 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1402 // ++var
1403 // var++
1404 // --var
1405 // var--
1406 // var += incr
1407 // var -= incr
1408 // var = var + incr
1409 // var = incr + var
1410 // var = var - incr
1411 //
1412 if (!S) {
1413 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1414 return true;
1415 }
1416 S = S->IgnoreParens();
1417 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1418 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1419 return SetStep(
1420 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1421 (UO->isDecrementOp() ? -1 : 1)).get(),
1422 false);
1423 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1424 switch (BO->getOpcode()) {
1425 case BO_AddAssign:
1426 case BO_SubAssign:
1427 if (GetInitVarDecl(BO->getLHS()) == Var)
1428 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1429 break;
1430 case BO_Assign:
1431 if (GetInitVarDecl(BO->getLHS()) == Var)
1432 return CheckIncRHS(BO->getRHS());
1433 break;
1434 default:
1435 break;
1436 }
1437 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1438 switch (CE->getOperator()) {
1439 case OO_PlusPlus:
1440 case OO_MinusMinus:
1441 if (GetInitVarDecl(CE->getArg(0)) == Var)
1442 return SetStep(
1443 SemaRef.ActOnIntegerConstant(
1444 CE->getLocStart(),
1445 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1446 false);
1447 break;
1448 case OO_PlusEqual:
1449 case OO_MinusEqual:
1450 if (GetInitVarDecl(CE->getArg(0)) == Var)
1451 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1452 break;
1453 case OO_Equal:
1454 if (GetInitVarDecl(CE->getArg(0)) == Var)
1455 return CheckIncRHS(CE->getArg(1));
1456 break;
1457 default:
1458 break;
1459 }
1460 }
1461 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1462 << S->getSourceRange() << Var;
1463 return true;
1464}
Alexey Bataev23b69422014-06-18 07:08:49 +00001465} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001466
1467/// \brief Called on a for stmt to check and extract its iteration space
1468/// for further processing (such as collapsing).
1469static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001470 Sema &SemaRef, DSAStackTy &DSA,
1471 unsigned CurrentNestedLoopCount,
1472 unsigned NestedLoopCount,
1473 Expr *NestedLoopCountExpr) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001474 // OpenMP [2.6, Canonical Loop Form]
1475 // for (init-expr; test-expr; incr-expr) structured-block
1476 auto For = dyn_cast_or_null<ForStmt>(S);
1477 if (!For) {
1478 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001479 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1480 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1481 << CurrentNestedLoopCount;
1482 if (NestedLoopCount > 1)
1483 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1484 diag::note_omp_collapse_expr)
1485 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001486 return true;
1487 }
1488 assert(For->getBody());
1489
1490 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1491
1492 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001493 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001494 if (ISC.CheckInit(Init)) {
1495 return true;
1496 }
1497
1498 bool HasErrors = false;
1499
1500 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001501 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001502
1503 // OpenMP [2.6, Canonical Loop Form]
1504 // Var is one of the following:
1505 // A variable of signed or unsigned integer type.
1506 // For C++, a variable of a random access iterator type.
1507 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001508 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001509 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1510 !VarType->isPointerType() &&
1511 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1512 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1513 << SemaRef.getLangOpts().CPlusPlus;
1514 HasErrors = true;
1515 }
1516
1517 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1518 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001519 // The loop iteration variable in the associated for-loop of a simd construct
1520 // with just one associated for-loop may be listed in a linear clause with a
1521 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001522 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1523 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001524 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001525 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1526 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) ||
1527 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1528 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001529 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001530 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1531 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001532 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001533 HasErrors = true;
1534 } else {
1535 // Make the loop iteration variable private by default.
1536 DSA.addDSA(Var, nullptr, OMPC_private);
1537 }
1538
Alexey Bataev7ff55242014-06-19 09:13:45 +00001539 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001540
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001541 // Check test-expr.
1542 HasErrors |= ISC.CheckCond(For->getCond());
1543
1544 // Check incr-expr.
1545 HasErrors |= ISC.CheckInc(For->getInc());
1546
1547 if (ISC.Dependent())
1548 return HasErrors;
1549
1550 // FIXME: Build loop's iteration space representation.
1551 return HasErrors;
1552}
1553
1554/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1555/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1556/// to get the first for loop.
1557static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1558 if (IgnoreCaptured)
1559 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1560 S = CapS->getCapturedStmt();
1561 // OpenMP [2.8.1, simd construct, Restrictions]
1562 // All loops associated with the construct must be perfectly nested; that is,
1563 // there must be no intervening code nor any OpenMP directive between any two
1564 // loops.
1565 while (true) {
1566 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1567 S = AS->getSubStmt();
1568 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1569 if (CS->size() != 1)
1570 break;
1571 S = CS->body_back();
1572 } else
1573 break;
1574 }
1575 return S;
1576}
1577
1578/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001579/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1580/// number of collapsed loops otherwise.
1581static unsigned CheckOpenMPLoop(OpenMPDirectiveKind DKind,
1582 Expr *NestedLoopCountExpr, Stmt *AStmt,
1583 Sema &SemaRef, DSAStackTy &DSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001584 unsigned NestedLoopCount = 1;
1585 if (NestedLoopCountExpr) {
1586 // Found 'collapse' clause - calculate collapse number.
1587 llvm::APSInt Result;
1588 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1589 NestedLoopCount = Result.getLimitedValue();
1590 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001591 // This is helper routine for loop directives (e.g., 'for', 'simd',
1592 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001593 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1594 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001595 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
1596 NestedLoopCount, NestedLoopCountExpr))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001597 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001598 // Move on to the next nested for loop, or to the loop body.
1599 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1600 }
1601
1602 // FIXME: Build resulting iteration space for IR generation (collapsing
1603 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001604 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001605}
1606
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001607static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001608 auto CollapseFilter = [](const OMPClause *C) -> bool {
1609 return C->getClauseKind() == OMPC_collapse;
1610 };
1611 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1612 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001613 if (I)
1614 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1615 return nullptr;
1616}
1617
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001618StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001619 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001620 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001621 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataevabfc0692014-06-25 06:52:00 +00001622 unsigned NestedLoopCount = CheckOpenMPLoop(
1623 OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack);
1624 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001625 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001626
1627 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001628 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1629 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001630}
1631
Alexey Bataevf29276e2014-06-18 04:14:57 +00001632StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses,
1633 Stmt *AStmt, SourceLocation StartLoc,
1634 SourceLocation EndLoc) {
1635 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataevabfc0692014-06-25 06:52:00 +00001636 unsigned NestedLoopCount = CheckOpenMPLoop(
1637 OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack);
1638 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001639 return StmtError();
1640
1641 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001642 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1643 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001644}
1645
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001646StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1647 Stmt *AStmt,
1648 SourceLocation StartLoc,
1649 SourceLocation EndLoc) {
1650 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1651 auto BaseStmt = AStmt;
1652 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1653 BaseStmt = CS->getCapturedStmt();
1654 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1655 auto S = C->children();
1656 if (!S)
1657 return StmtError();
1658 // All associated statements must be '#pragma omp section' except for
1659 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001660 for (++S; S; ++S) {
1661 auto SectionStmt = *S;
1662 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1663 if (SectionStmt)
1664 Diag(SectionStmt->getLocStart(),
1665 diag::err_omp_sections_substmt_not_section);
1666 return StmtError();
1667 }
1668 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001669 } else {
1670 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1671 return StmtError();
1672 }
1673
1674 getCurFunction()->setHasBranchProtectedScope();
1675
1676 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1677 AStmt);
1678}
1679
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001680StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1681 SourceLocation StartLoc,
1682 SourceLocation EndLoc) {
1683 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1684
1685 getCurFunction()->setHasBranchProtectedScope();
1686
1687 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1688}
1689
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001690StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
1691 Stmt *AStmt,
1692 SourceLocation StartLoc,
1693 SourceLocation EndLoc) {
1694 getCurFunction()->setHasBranchProtectedScope();
1695 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1696}
1697
Alexey Bataeved09d242014-05-28 05:53:51 +00001698OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001699 SourceLocation StartLoc,
1700 SourceLocation LParenLoc,
1701 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001702 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001703 switch (Kind) {
1704 case OMPC_if:
1705 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1706 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001707 case OMPC_num_threads:
1708 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1709 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001710 case OMPC_safelen:
1711 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1712 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001713 case OMPC_collapse:
1714 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1715 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001716 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001717 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001718 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001719 case OMPC_private:
1720 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001721 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001722 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001723 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001724 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001725 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001726 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001727 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001728 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001729 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001730 case OMPC_threadprivate:
1731 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001732 llvm_unreachable("Clause is not allowed.");
1733 }
1734 return Res;
1735}
1736
Alexey Bataeved09d242014-05-28 05:53:51 +00001737OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001738 SourceLocation LParenLoc,
1739 SourceLocation EndLoc) {
1740 Expr *ValExpr = Condition;
1741 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1742 !Condition->isInstantiationDependent() &&
1743 !Condition->containsUnexpandedParameterPack()) {
1744 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001745 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001746 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001747 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001748
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001749 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001750 }
1751
1752 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1753}
1754
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001755ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1756 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001757 if (!Op)
1758 return ExprError();
1759
1760 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1761 public:
1762 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001763 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001764 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1765 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001766 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1767 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001768 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1769 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001770 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1771 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001772 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1773 QualType T,
1774 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001775 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1776 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001777 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1778 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001779 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001780 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001781 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001782 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1783 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001784 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1785 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001786 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1787 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001788 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001789 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001790 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001791 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1792 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001793 llvm_unreachable("conversion functions are permitted");
1794 }
1795 } ConvertDiagnoser;
1796 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1797}
1798
1799OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1800 SourceLocation StartLoc,
1801 SourceLocation LParenLoc,
1802 SourceLocation EndLoc) {
1803 Expr *ValExpr = NumThreads;
1804 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1805 !NumThreads->isInstantiationDependent() &&
1806 !NumThreads->containsUnexpandedParameterPack()) {
1807 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1808 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001809 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001810 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001811 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001812
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001813 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001814
1815 // OpenMP [2.5, Restrictions]
1816 // The num_threads expression must evaluate to a positive integer value.
1817 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001818 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1819 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001820 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1821 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001822 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001823 }
1824 }
1825
Alexey Bataeved09d242014-05-28 05:53:51 +00001826 return new (Context)
1827 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001828}
1829
Alexey Bataev62c87d22014-03-21 04:51:18 +00001830ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1831 OpenMPClauseKind CKind) {
1832 if (!E)
1833 return ExprError();
1834 if (E->isValueDependent() || E->isTypeDependent() ||
1835 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001836 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001837 llvm::APSInt Result;
1838 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1839 if (ICE.isInvalid())
1840 return ExprError();
1841 if (!Result.isStrictlyPositive()) {
1842 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1843 << getOpenMPClauseName(CKind) << E->getSourceRange();
1844 return ExprError();
1845 }
1846 return ICE;
1847}
1848
1849OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1850 SourceLocation LParenLoc,
1851 SourceLocation EndLoc) {
1852 // OpenMP [2.8.1, simd construct, Description]
1853 // The parameter of the safelen clause must be a constant
1854 // positive integer expression.
1855 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1856 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001857 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001858 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001859 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001860}
1861
Alexander Musman64d33f12014-06-04 07:53:32 +00001862OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1863 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001864 SourceLocation LParenLoc,
1865 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001866 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001867 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001868 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001869 // The parameter of the collapse clause must be a constant
1870 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001871 ExprResult NumForLoopsResult =
1872 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1873 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001874 return nullptr;
1875 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001876 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001877}
1878
Alexey Bataeved09d242014-05-28 05:53:51 +00001879OMPClause *Sema::ActOnOpenMPSimpleClause(
1880 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1881 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001882 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001883 switch (Kind) {
1884 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001885 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001886 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1887 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001888 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001889 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001890 Res = ActOnOpenMPProcBindClause(
1891 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1892 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001893 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001894 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001895 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001896 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001897 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001898 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001899 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001900 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001901 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001902 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001903 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001904 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001905 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001906 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001907 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001908 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001909 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001910 case OMPC_threadprivate:
1911 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001912 llvm_unreachable("Clause is not allowed.");
1913 }
1914 return Res;
1915}
1916
1917OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1918 SourceLocation KindKwLoc,
1919 SourceLocation StartLoc,
1920 SourceLocation LParenLoc,
1921 SourceLocation EndLoc) {
1922 if (Kind == OMPC_DEFAULT_unknown) {
1923 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001924 static_assert(OMPC_DEFAULT_unknown > 0,
1925 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001926 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001927 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001928 Values += "'";
1929 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1930 Values += "'";
1931 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001932 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001933 Values += " or ";
1934 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001935 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001936 break;
1937 default:
1938 Values += Sep;
1939 break;
1940 }
1941 }
1942 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001943 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001944 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001945 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001946 switch (Kind) {
1947 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001948 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001949 break;
1950 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001951 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001952 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001953 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001954 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001955 break;
1956 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001957 return new (Context)
1958 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001959}
1960
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001961OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1962 SourceLocation KindKwLoc,
1963 SourceLocation StartLoc,
1964 SourceLocation LParenLoc,
1965 SourceLocation EndLoc) {
1966 if (Kind == OMPC_PROC_BIND_unknown) {
1967 std::string Values;
1968 std::string Sep(", ");
1969 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1970 Values += "'";
1971 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1972 Values += "'";
1973 switch (i) {
1974 case OMPC_PROC_BIND_unknown - 2:
1975 Values += " or ";
1976 break;
1977 case OMPC_PROC_BIND_unknown - 1:
1978 break;
1979 default:
1980 Values += Sep;
1981 break;
1982 }
1983 }
1984 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001985 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001986 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001987 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001988 return new (Context)
1989 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001990}
1991
Alexey Bataev56dafe82014-06-20 07:16:17 +00001992OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1993 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
1994 SourceLocation StartLoc, SourceLocation LParenLoc,
1995 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
1996 SourceLocation EndLoc) {
1997 OMPClause *Res = nullptr;
1998 switch (Kind) {
1999 case OMPC_schedule:
2000 Res = ActOnOpenMPScheduleClause(
2001 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2002 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2003 break;
2004 case OMPC_if:
2005 case OMPC_num_threads:
2006 case OMPC_safelen:
2007 case OMPC_collapse:
2008 case OMPC_default:
2009 case OMPC_proc_bind:
2010 case OMPC_private:
2011 case OMPC_firstprivate:
2012 case OMPC_lastprivate:
2013 case OMPC_shared:
2014 case OMPC_reduction:
2015 case OMPC_linear:
2016 case OMPC_aligned:
2017 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002018 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002019 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002020 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002021 case OMPC_threadprivate:
2022 case OMPC_unknown:
2023 llvm_unreachable("Clause is not allowed.");
2024 }
2025 return Res;
2026}
2027
2028OMPClause *Sema::ActOnOpenMPScheduleClause(
2029 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2030 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2031 SourceLocation EndLoc) {
2032 if (Kind == OMPC_SCHEDULE_unknown) {
2033 std::string Values;
2034 std::string Sep(", ");
2035 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2036 Values += "'";
2037 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2038 Values += "'";
2039 switch (i) {
2040 case OMPC_SCHEDULE_unknown - 2:
2041 Values += " or ";
2042 break;
2043 case OMPC_SCHEDULE_unknown - 1:
2044 break;
2045 default:
2046 Values += Sep;
2047 break;
2048 }
2049 }
2050 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2051 << Values << getOpenMPClauseName(OMPC_schedule);
2052 return nullptr;
2053 }
2054 Expr *ValExpr = ChunkSize;
2055 if (ChunkSize) {
2056 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2057 !ChunkSize->isInstantiationDependent() &&
2058 !ChunkSize->containsUnexpandedParameterPack()) {
2059 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2060 ExprResult Val =
2061 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2062 if (Val.isInvalid())
2063 return nullptr;
2064
2065 ValExpr = Val.get();
2066
2067 // OpenMP [2.7.1, Restrictions]
2068 // chunk_size must be a loop invariant integer expression with a positive
2069 // value.
2070 llvm::APSInt Result;
2071 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2072 Result.isSigned() && !Result.isStrictlyPositive()) {
2073 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2074 << "schedule" << ChunkSize->getSourceRange();
2075 return nullptr;
2076 }
2077 }
2078 }
2079
2080 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2081 EndLoc, Kind, ValExpr);
2082}
2083
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002084OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2085 SourceLocation StartLoc,
2086 SourceLocation EndLoc) {
2087 OMPClause *Res = nullptr;
2088 switch (Kind) {
2089 case OMPC_ordered:
2090 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2091 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002092 case OMPC_nowait:
2093 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2094 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002095 case OMPC_if:
2096 case OMPC_num_threads:
2097 case OMPC_safelen:
2098 case OMPC_collapse:
2099 case OMPC_schedule:
2100 case OMPC_private:
2101 case OMPC_firstprivate:
2102 case OMPC_lastprivate:
2103 case OMPC_shared:
2104 case OMPC_reduction:
2105 case OMPC_linear:
2106 case OMPC_aligned:
2107 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002108 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002109 case OMPC_default:
2110 case OMPC_proc_bind:
2111 case OMPC_threadprivate:
2112 case OMPC_unknown:
2113 llvm_unreachable("Clause is not allowed.");
2114 }
2115 return Res;
2116}
2117
2118OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2119 SourceLocation EndLoc) {
2120 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2121}
2122
Alexey Bataev236070f2014-06-20 11:19:47 +00002123OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2124 SourceLocation EndLoc) {
2125 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2126}
2127
Alexey Bataevc5e02582014-06-16 07:08:35 +00002128OMPClause *Sema::ActOnOpenMPVarListClause(
2129 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2130 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2131 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2132 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002133 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002134 switch (Kind) {
2135 case OMPC_private:
2136 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2137 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002138 case OMPC_firstprivate:
2139 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2140 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002141 case OMPC_lastprivate:
2142 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2143 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002144 case OMPC_shared:
2145 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2146 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002147 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002148 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2149 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002150 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002151 case OMPC_linear:
2152 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2153 ColonLoc, EndLoc);
2154 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002155 case OMPC_aligned:
2156 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2157 ColonLoc, EndLoc);
2158 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002159 case OMPC_copyin:
2160 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2161 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002162 case OMPC_copyprivate:
2163 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2164 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002165 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002166 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002167 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002168 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002169 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002170 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002171 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002172 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002173 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002174 case OMPC_threadprivate:
2175 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002176 llvm_unreachable("Clause is not allowed.");
2177 }
2178 return Res;
2179}
2180
2181OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2182 SourceLocation StartLoc,
2183 SourceLocation LParenLoc,
2184 SourceLocation EndLoc) {
2185 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002186 for (auto &RefExpr : VarList) {
2187 assert(RefExpr && "NULL expr in OpenMP private clause.");
2188 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002189 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002190 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002191 continue;
2192 }
2193
Alexey Bataeved09d242014-05-28 05:53:51 +00002194 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002195 // OpenMP [2.1, C/C++]
2196 // A list item is a variable name.
2197 // OpenMP [2.9.3.3, Restrictions, p.1]
2198 // A variable that is part of another variable (as an array or
2199 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002200 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002201 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002202 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002203 continue;
2204 }
2205 Decl *D = DE->getDecl();
2206 VarDecl *VD = cast<VarDecl>(D);
2207
2208 QualType Type = VD->getType();
2209 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2210 // It will be analyzed later.
2211 Vars.push_back(DE);
2212 continue;
2213 }
2214
2215 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2216 // A variable that appears in a private clause must not have an incomplete
2217 // type or a reference type.
2218 if (RequireCompleteType(ELoc, Type,
2219 diag::err_omp_private_incomplete_type)) {
2220 continue;
2221 }
2222 if (Type->isReferenceType()) {
2223 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002224 << getOpenMPClauseName(OMPC_private) << Type;
2225 bool IsDecl =
2226 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2227 Diag(VD->getLocation(),
2228 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2229 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002230 continue;
2231 }
2232
2233 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2234 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002235 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002236 // class type.
2237 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002238 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2239 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002240 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002241 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2242 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2243 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002244 // FIXME This code must be replaced by actual constructing/destructing of
2245 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002246 if (RD) {
2247 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2248 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002249 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002250 if (!CD ||
2251 CheckConstructorAccess(ELoc, CD,
2252 InitializedEntity::InitializeTemporary(Type),
2253 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002254 CD->isDeleted()) {
2255 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002256 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002257 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2258 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002259 Diag(VD->getLocation(),
2260 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2261 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002262 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2263 continue;
2264 }
2265 MarkFunctionReferenced(ELoc, CD);
2266 DiagnoseUseOfDecl(CD, ELoc);
2267
2268 CXXDestructorDecl *DD = RD->getDestructor();
2269 if (DD) {
2270 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2271 DD->isDeleted()) {
2272 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002273 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002274 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2275 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002276 Diag(VD->getLocation(),
2277 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2278 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002279 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2280 continue;
2281 }
2282 MarkFunctionReferenced(ELoc, DD);
2283 DiagnoseUseOfDecl(DD, ELoc);
2284 }
2285 }
2286
Alexey Bataev758e55e2013-09-06 18:03:48 +00002287 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2288 // in a Construct]
2289 // Variables with the predetermined data-sharing attributes may not be
2290 // listed in data-sharing attributes clauses, except for the cases
2291 // listed below. For these exceptions only, listing a predetermined
2292 // variable in a data-sharing attribute clause is allowed and overrides
2293 // the variable's predetermined data-sharing attributes.
2294 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2295 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002296 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2297 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002298 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002299 continue;
2300 }
2301
2302 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002303 Vars.push_back(DE);
2304 }
2305
Alexey Bataeved09d242014-05-28 05:53:51 +00002306 if (Vars.empty())
2307 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002308
2309 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2310}
2311
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002312OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2313 SourceLocation StartLoc,
2314 SourceLocation LParenLoc,
2315 SourceLocation EndLoc) {
2316 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002317 for (auto &RefExpr : VarList) {
2318 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2319 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002320 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002321 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002322 continue;
2323 }
2324
Alexey Bataeved09d242014-05-28 05:53:51 +00002325 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002326 // OpenMP [2.1, C/C++]
2327 // A list item is a variable name.
2328 // OpenMP [2.9.3.3, Restrictions, p.1]
2329 // A variable that is part of another variable (as an array or
2330 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002331 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002332 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002333 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002334 continue;
2335 }
2336 Decl *D = DE->getDecl();
2337 VarDecl *VD = cast<VarDecl>(D);
2338
2339 QualType Type = VD->getType();
2340 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2341 // It will be analyzed later.
2342 Vars.push_back(DE);
2343 continue;
2344 }
2345
2346 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2347 // A variable that appears in a private clause must not have an incomplete
2348 // type or a reference type.
2349 if (RequireCompleteType(ELoc, Type,
2350 diag::err_omp_firstprivate_incomplete_type)) {
2351 continue;
2352 }
2353 if (Type->isReferenceType()) {
2354 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002355 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2356 bool IsDecl =
2357 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2358 Diag(VD->getLocation(),
2359 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2360 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002361 continue;
2362 }
2363
2364 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2365 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002366 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002367 // class type.
2368 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002369 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2370 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2371 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002372 // FIXME This code must be replaced by actual constructing/destructing of
2373 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002374 if (RD) {
2375 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2376 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002377 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002378 if (!CD ||
2379 CheckConstructorAccess(ELoc, CD,
2380 InitializedEntity::InitializeTemporary(Type),
2381 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002382 CD->isDeleted()) {
2383 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002384 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002385 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2386 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002387 Diag(VD->getLocation(),
2388 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2389 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002390 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2391 continue;
2392 }
2393 MarkFunctionReferenced(ELoc, CD);
2394 DiagnoseUseOfDecl(CD, ELoc);
2395
2396 CXXDestructorDecl *DD = RD->getDestructor();
2397 if (DD) {
2398 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2399 DD->isDeleted()) {
2400 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002401 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002402 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2403 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002404 Diag(VD->getLocation(),
2405 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2406 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002407 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2408 continue;
2409 }
2410 MarkFunctionReferenced(ELoc, DD);
2411 DiagnoseUseOfDecl(DD, ELoc);
2412 }
2413 }
2414
2415 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2416 // variable and it was checked already.
2417 if (StartLoc.isValid() && EndLoc.isValid()) {
2418 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2419 Type = Type.getNonReferenceType().getCanonicalType();
2420 bool IsConstant = Type.isConstant(Context);
2421 Type = Context.getBaseElementType(Type);
2422 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2423 // A list item that specifies a given variable may not appear in more
2424 // than one clause on the same directive, except that a variable may be
2425 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002426 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002427 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002428 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002429 << getOpenMPClauseName(DVar.CKind)
2430 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002431 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002432 continue;
2433 }
2434
2435 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2436 // in a Construct]
2437 // Variables with the predetermined data-sharing attributes may not be
2438 // listed in data-sharing attributes clauses, except for the cases
2439 // listed below. For these exceptions only, listing a predetermined
2440 // variable in a data-sharing attribute clause is allowed and overrides
2441 // the variable's predetermined data-sharing attributes.
2442 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2443 // in a Construct, C/C++, p.2]
2444 // Variables with const-qualified type having no mutable member may be
2445 // listed in a firstprivate clause, even if they are static data members.
2446 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2447 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2448 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002449 << getOpenMPClauseName(DVar.CKind)
2450 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002451 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002452 continue;
2453 }
2454
Alexey Bataevf29276e2014-06-18 04:14:57 +00002455 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002456 // OpenMP [2.9.3.4, Restrictions, p.2]
2457 // A list item that is private within a parallel region must not appear
2458 // in a firstprivate clause on a worksharing construct if any of the
2459 // worksharing regions arising from the worksharing construct ever bind
2460 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002461 if (isOpenMPWorksharingDirective(CurrDir) &&
2462 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002463 DVar = DSAStack->getImplicitDSA(VD);
2464 if (DVar.CKind != OMPC_shared) {
2465 Diag(ELoc, diag::err_omp_required_access)
2466 << getOpenMPClauseName(OMPC_firstprivate)
2467 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002468 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002469 continue;
2470 }
2471 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002472 // OpenMP [2.9.3.4, Restrictions, p.3]
2473 // A list item that appears in a reduction clause of a parallel construct
2474 // must not appear in a firstprivate clause on a worksharing or task
2475 // construct if any of the worksharing or task regions arising from the
2476 // worksharing or task construct ever bind to any of the parallel regions
2477 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002478 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002479 // OpenMP [2.9.3.4, Restrictions, p.4]
2480 // A list item that appears in a reduction clause in worksharing
2481 // construct must not appear in a firstprivate clause in a task construct
2482 // encountered during execution of any of the worksharing regions arising
2483 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002484 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002485 }
2486
2487 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2488 Vars.push_back(DE);
2489 }
2490
Alexey Bataeved09d242014-05-28 05:53:51 +00002491 if (Vars.empty())
2492 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002493
2494 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2495 Vars);
2496}
2497
Alexander Musman1bb328c2014-06-04 13:06:39 +00002498OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2499 SourceLocation StartLoc,
2500 SourceLocation LParenLoc,
2501 SourceLocation EndLoc) {
2502 SmallVector<Expr *, 8> Vars;
2503 for (auto &RefExpr : VarList) {
2504 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2505 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2506 // It will be analyzed later.
2507 Vars.push_back(RefExpr);
2508 continue;
2509 }
2510
2511 SourceLocation ELoc = RefExpr->getExprLoc();
2512 // OpenMP [2.1, C/C++]
2513 // A list item is a variable name.
2514 // OpenMP [2.14.3.5, Restrictions, p.1]
2515 // A variable that is part of another variable (as an array or structure
2516 // element) cannot appear in a lastprivate clause.
2517 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2518 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2519 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2520 continue;
2521 }
2522 Decl *D = DE->getDecl();
2523 VarDecl *VD = cast<VarDecl>(D);
2524
2525 QualType Type = VD->getType();
2526 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2527 // It will be analyzed later.
2528 Vars.push_back(DE);
2529 continue;
2530 }
2531
2532 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2533 // A variable that appears in a lastprivate clause must not have an
2534 // incomplete type or a reference type.
2535 if (RequireCompleteType(ELoc, Type,
2536 diag::err_omp_lastprivate_incomplete_type)) {
2537 continue;
2538 }
2539 if (Type->isReferenceType()) {
2540 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2541 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2542 bool IsDecl =
2543 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2544 Diag(VD->getLocation(),
2545 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2546 << VD;
2547 continue;
2548 }
2549
2550 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2551 // in a Construct]
2552 // Variables with the predetermined data-sharing attributes may not be
2553 // listed in data-sharing attributes clauses, except for the cases
2554 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002555 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2556 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2557 DVar.CKind != OMPC_firstprivate &&
2558 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2559 Diag(ELoc, diag::err_omp_wrong_dsa)
2560 << getOpenMPClauseName(DVar.CKind)
2561 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002562 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002563 continue;
2564 }
2565
Alexey Bataevf29276e2014-06-18 04:14:57 +00002566 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2567 // OpenMP [2.14.3.5, Restrictions, p.2]
2568 // A list item that is private within a parallel region, or that appears in
2569 // the reduction clause of a parallel construct, must not appear in a
2570 // lastprivate clause on a worksharing construct if any of the corresponding
2571 // worksharing regions ever binds to any of the corresponding parallel
2572 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002573 if (isOpenMPWorksharingDirective(CurrDir) &&
2574 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002575 DVar = DSAStack->getImplicitDSA(VD);
2576 if (DVar.CKind != OMPC_shared) {
2577 Diag(ELoc, diag::err_omp_required_access)
2578 << getOpenMPClauseName(OMPC_lastprivate)
2579 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002580 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002581 continue;
2582 }
2583 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002584 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002585 // A variable of class type (or array thereof) that appears in a
2586 // lastprivate clause requires an accessible, unambiguous default
2587 // constructor for the class type, unless the list item is also specified
2588 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002589 // A variable of class type (or array thereof) that appears in a
2590 // lastprivate clause requires an accessible, unambiguous copy assignment
2591 // operator for the class type.
2592 while (Type.getNonReferenceType()->isArrayType())
2593 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2594 ->getElementType();
2595 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2596 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2597 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002598 // FIXME This code must be replaced by actual copying and destructing of the
2599 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002600 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002601 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2602 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002603 if (MD) {
2604 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2605 MD->isDeleted()) {
2606 Diag(ELoc, diag::err_omp_required_method)
2607 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2608 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2609 VarDecl::DeclarationOnly;
2610 Diag(VD->getLocation(),
2611 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2612 << VD;
2613 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2614 continue;
2615 }
2616 MarkFunctionReferenced(ELoc, MD);
2617 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002618 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002619
2620 CXXDestructorDecl *DD = RD->getDestructor();
2621 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002622 PartialDiagnostic PD =
2623 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002624 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2625 DD->isDeleted()) {
2626 Diag(ELoc, diag::err_omp_required_method)
2627 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2628 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2629 VarDecl::DeclarationOnly;
2630 Diag(VD->getLocation(),
2631 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2632 << VD;
2633 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2634 continue;
2635 }
2636 MarkFunctionReferenced(ELoc, DD);
2637 DiagnoseUseOfDecl(DD, ELoc);
2638 }
2639 }
2640
Alexey Bataevf29276e2014-06-18 04:14:57 +00002641 if (DVar.CKind != OMPC_firstprivate)
2642 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002643 Vars.push_back(DE);
2644 }
2645
2646 if (Vars.empty())
2647 return nullptr;
2648
2649 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2650 Vars);
2651}
2652
Alexey Bataev758e55e2013-09-06 18:03:48 +00002653OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2654 SourceLocation StartLoc,
2655 SourceLocation LParenLoc,
2656 SourceLocation EndLoc) {
2657 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002658 for (auto &RefExpr : VarList) {
2659 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2660 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002661 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002662 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002663 continue;
2664 }
2665
Alexey Bataeved09d242014-05-28 05:53:51 +00002666 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002667 // OpenMP [2.1, C/C++]
2668 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002669 // OpenMP [2.14.3.2, Restrictions, p.1]
2670 // A variable that is part of another variable (as an array or structure
2671 // element) cannot appear in a shared unless it is a static data member
2672 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002673 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002674 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002675 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002676 continue;
2677 }
2678 Decl *D = DE->getDecl();
2679 VarDecl *VD = cast<VarDecl>(D);
2680
2681 QualType Type = VD->getType();
2682 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2683 // It will be analyzed later.
2684 Vars.push_back(DE);
2685 continue;
2686 }
2687
2688 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2689 // in a Construct]
2690 // Variables with the predetermined data-sharing attributes may not be
2691 // listed in data-sharing attributes clauses, except for the cases
2692 // listed below. For these exceptions only, listing a predetermined
2693 // variable in a data-sharing attribute clause is allowed and overrides
2694 // the variable's predetermined data-sharing attributes.
2695 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002696 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2697 DVar.RefExpr) {
2698 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2699 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002700 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002701 continue;
2702 }
2703
2704 DSAStack->addDSA(VD, DE, OMPC_shared);
2705 Vars.push_back(DE);
2706 }
2707
Alexey Bataeved09d242014-05-28 05:53:51 +00002708 if (Vars.empty())
2709 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002710
2711 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2712}
2713
Alexey Bataevc5e02582014-06-16 07:08:35 +00002714namespace {
2715class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2716 DSAStackTy *Stack;
2717
2718public:
2719 bool VisitDeclRefExpr(DeclRefExpr *E) {
2720 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2721 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2722 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2723 return false;
2724 if (DVar.CKind != OMPC_unknown)
2725 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002726 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002727 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002728 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002729 return true;
2730 return false;
2731 }
2732 return false;
2733 }
2734 bool VisitStmt(Stmt *S) {
2735 for (auto Child : S->children()) {
2736 if (Child && Visit(Child))
2737 return true;
2738 }
2739 return false;
2740 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002741 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002742};
Alexey Bataev23b69422014-06-18 07:08:49 +00002743} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002744
2745OMPClause *Sema::ActOnOpenMPReductionClause(
2746 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2747 SourceLocation ColonLoc, SourceLocation EndLoc,
2748 CXXScopeSpec &ReductionIdScopeSpec,
2749 const DeclarationNameInfo &ReductionId) {
2750 // TODO: Allow scope specification search when 'declare reduction' is
2751 // supported.
2752 assert(ReductionIdScopeSpec.isEmpty() &&
2753 "No support for scoped reduction identifiers yet.");
2754
2755 auto DN = ReductionId.getName();
2756 auto OOK = DN.getCXXOverloadedOperator();
2757 BinaryOperatorKind BOK = BO_Comma;
2758
2759 // OpenMP [2.14.3.6, reduction clause]
2760 // C
2761 // reduction-identifier is either an identifier or one of the following
2762 // operators: +, -, *, &, |, ^, && and ||
2763 // C++
2764 // reduction-identifier is either an id-expression or one of the following
2765 // operators: +, -, *, &, |, ^, && and ||
2766 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2767 switch (OOK) {
2768 case OO_Plus:
2769 case OO_Minus:
2770 BOK = BO_AddAssign;
2771 break;
2772 case OO_Star:
2773 BOK = BO_MulAssign;
2774 break;
2775 case OO_Amp:
2776 BOK = BO_AndAssign;
2777 break;
2778 case OO_Pipe:
2779 BOK = BO_OrAssign;
2780 break;
2781 case OO_Caret:
2782 BOK = BO_XorAssign;
2783 break;
2784 case OO_AmpAmp:
2785 BOK = BO_LAnd;
2786 break;
2787 case OO_PipePipe:
2788 BOK = BO_LOr;
2789 break;
2790 default:
2791 if (auto II = DN.getAsIdentifierInfo()) {
2792 if (II->isStr("max"))
2793 BOK = BO_GT;
2794 else if (II->isStr("min"))
2795 BOK = BO_LT;
2796 }
2797 break;
2798 }
2799 SourceRange ReductionIdRange;
2800 if (ReductionIdScopeSpec.isValid()) {
2801 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2802 }
2803 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2804 if (BOK == BO_Comma) {
2805 // Not allowed reduction identifier is found.
2806 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2807 << ReductionIdRange;
2808 return nullptr;
2809 }
2810
2811 SmallVector<Expr *, 8> Vars;
2812 for (auto RefExpr : VarList) {
2813 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2814 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2815 // It will be analyzed later.
2816 Vars.push_back(RefExpr);
2817 continue;
2818 }
2819
2820 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2821 RefExpr->isInstantiationDependent() ||
2822 RefExpr->containsUnexpandedParameterPack()) {
2823 // It will be analyzed later.
2824 Vars.push_back(RefExpr);
2825 continue;
2826 }
2827
2828 auto ELoc = RefExpr->getExprLoc();
2829 auto ERange = RefExpr->getSourceRange();
2830 // OpenMP [2.1, C/C++]
2831 // A list item is a variable or array section, subject to the restrictions
2832 // specified in Section 2.4 on page 42 and in each of the sections
2833 // describing clauses and directives for which a list appears.
2834 // OpenMP [2.14.3.3, Restrictions, p.1]
2835 // A variable that is part of another variable (as an array or
2836 // structure element) cannot appear in a private clause.
2837 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2838 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2839 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2840 continue;
2841 }
2842 auto D = DE->getDecl();
2843 auto VD = cast<VarDecl>(D);
2844 auto Type = VD->getType();
2845 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2846 // A variable that appears in a private clause must not have an incomplete
2847 // type or a reference type.
2848 if (RequireCompleteType(ELoc, Type,
2849 diag::err_omp_reduction_incomplete_type))
2850 continue;
2851 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2852 // Arrays may not appear in a reduction clause.
2853 if (Type.getNonReferenceType()->isArrayType()) {
2854 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2855 bool IsDecl =
2856 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2857 Diag(VD->getLocation(),
2858 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2859 << VD;
2860 continue;
2861 }
2862 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2863 // A list item that appears in a reduction clause must not be
2864 // const-qualified.
2865 if (Type.getNonReferenceType().isConstant(Context)) {
2866 Diag(ELoc, diag::err_omp_const_variable)
2867 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2868 bool IsDecl =
2869 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2870 Diag(VD->getLocation(),
2871 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2872 << VD;
2873 continue;
2874 }
2875 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2876 // If a list-item is a reference type then it must bind to the same object
2877 // for all threads of the team.
2878 VarDecl *VDDef = VD->getDefinition();
2879 if (Type->isReferenceType() && VDDef) {
2880 DSARefChecker Check(DSAStack);
2881 if (Check.Visit(VDDef->getInit())) {
2882 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2883 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2884 continue;
2885 }
2886 }
2887 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2888 // The type of a list item that appears in a reduction clause must be valid
2889 // for the reduction-identifier. For a max or min reduction in C, the type
2890 // of the list item must be an allowed arithmetic data type: char, int,
2891 // float, double, or _Bool, possibly modified with long, short, signed, or
2892 // unsigned. For a max or min reduction in C++, the type of the list item
2893 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2894 // double, or bool, possibly modified with long, short, signed, or unsigned.
2895 if ((BOK == BO_GT || BOK == BO_LT) &&
2896 !(Type->isScalarType() ||
2897 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2898 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2899 << getLangOpts().CPlusPlus;
2900 bool IsDecl =
2901 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2902 Diag(VD->getLocation(),
2903 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2904 << VD;
2905 continue;
2906 }
2907 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2908 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2909 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2910 bool IsDecl =
2911 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2912 Diag(VD->getLocation(),
2913 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2914 << VD;
2915 continue;
2916 }
2917 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2918 getDiagnostics().setSuppressAllDiagnostics(true);
2919 ExprResult ReductionOp =
2920 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2921 RefExpr, RefExpr);
2922 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2923 if (ReductionOp.isInvalid()) {
2924 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002925 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002926 bool IsDecl =
2927 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2928 Diag(VD->getLocation(),
2929 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2930 << VD;
2931 continue;
2932 }
2933
2934 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2935 // in a Construct]
2936 // Variables with the predetermined data-sharing attributes may not be
2937 // listed in data-sharing attributes clauses, except for the cases
2938 // listed below. For these exceptions only, listing a predetermined
2939 // variable in a data-sharing attribute clause is allowed and overrides
2940 // the variable's predetermined data-sharing attributes.
2941 // OpenMP [2.14.3.6, Restrictions, p.3]
2942 // Any number of reduction clauses can be specified on the directive,
2943 // but a list item can appear only once in the reduction clauses for that
2944 // directive.
2945 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2946 if (DVar.CKind == OMPC_reduction) {
2947 Diag(ELoc, diag::err_omp_once_referenced)
2948 << getOpenMPClauseName(OMPC_reduction);
2949 if (DVar.RefExpr) {
2950 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2951 }
2952 } else if (DVar.CKind != OMPC_unknown) {
2953 Diag(ELoc, diag::err_omp_wrong_dsa)
2954 << getOpenMPClauseName(DVar.CKind)
2955 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002956 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002957 continue;
2958 }
2959
2960 // OpenMP [2.14.3.6, Restrictions, p.1]
2961 // A list item that appears in a reduction clause of a worksharing
2962 // construct must be shared in the parallel regions to which any of the
2963 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002964 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00002965 if (isOpenMPWorksharingDirective(CurrDir) &&
2966 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002967 DVar = DSAStack->getImplicitDSA(VD);
2968 if (DVar.CKind != OMPC_shared) {
2969 Diag(ELoc, diag::err_omp_required_access)
2970 << getOpenMPClauseName(OMPC_reduction)
2971 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002972 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002973 continue;
2974 }
2975 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00002976
2977 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2978 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2979 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002980 // FIXME This code must be replaced by actual constructing/destructing of
2981 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00002982 if (RD) {
2983 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2984 PartialDiagnostic PD =
2985 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00002986 if (!CD ||
2987 CheckConstructorAccess(ELoc, CD,
2988 InitializedEntity::InitializeTemporary(Type),
2989 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00002990 CD->isDeleted()) {
2991 Diag(ELoc, diag::err_omp_required_method)
2992 << getOpenMPClauseName(OMPC_reduction) << 0;
2993 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2994 VarDecl::DeclarationOnly;
2995 Diag(VD->getLocation(),
2996 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2997 << VD;
2998 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2999 continue;
3000 }
3001 MarkFunctionReferenced(ELoc, CD);
3002 DiagnoseUseOfDecl(CD, ELoc);
3003
3004 CXXDestructorDecl *DD = RD->getDestructor();
3005 if (DD) {
3006 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3007 DD->isDeleted()) {
3008 Diag(ELoc, diag::err_omp_required_method)
3009 << getOpenMPClauseName(OMPC_reduction) << 4;
3010 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3011 VarDecl::DeclarationOnly;
3012 Diag(VD->getLocation(),
3013 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3014 << VD;
3015 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3016 continue;
3017 }
3018 MarkFunctionReferenced(ELoc, DD);
3019 DiagnoseUseOfDecl(DD, ELoc);
3020 }
3021 }
3022
3023 DSAStack->addDSA(VD, DE, OMPC_reduction);
3024 Vars.push_back(DE);
3025 }
3026
3027 if (Vars.empty())
3028 return nullptr;
3029
3030 return OMPReductionClause::Create(
3031 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3032 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3033}
3034
Alexander Musman8dba6642014-04-22 13:09:42 +00003035OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3036 SourceLocation StartLoc,
3037 SourceLocation LParenLoc,
3038 SourceLocation ColonLoc,
3039 SourceLocation EndLoc) {
3040 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003041 for (auto &RefExpr : VarList) {
3042 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3043 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003044 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003045 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003046 continue;
3047 }
3048
3049 // OpenMP [2.14.3.7, linear clause]
3050 // A list item that appears in a linear clause is subject to the private
3051 // clause semantics described in Section 2.14.3.3 on page 159 except as
3052 // noted. In addition, the value of the new list item on each iteration
3053 // of the associated loop(s) corresponds to the value of the original
3054 // list item before entering the construct plus the logical number of
3055 // the iteration times linear-step.
3056
Alexey Bataeved09d242014-05-28 05:53:51 +00003057 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003058 // OpenMP [2.1, C/C++]
3059 // A list item is a variable name.
3060 // OpenMP [2.14.3.3, Restrictions, p.1]
3061 // A variable that is part of another variable (as an array or
3062 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003063 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003064 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003065 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003066 continue;
3067 }
3068
3069 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3070
3071 // OpenMP [2.14.3.7, linear clause]
3072 // A list-item cannot appear in more than one linear clause.
3073 // A list-item that appears in a linear clause cannot appear in any
3074 // other data-sharing attribute clause.
3075 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
3076 if (DVar.RefExpr) {
3077 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3078 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003079 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003080 continue;
3081 }
3082
3083 QualType QType = VD->getType();
3084 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3085 // It will be analyzed later.
3086 Vars.push_back(DE);
3087 continue;
3088 }
3089
3090 // A variable must not have an incomplete type or a reference type.
3091 if (RequireCompleteType(ELoc, QType,
3092 diag::err_omp_linear_incomplete_type)) {
3093 continue;
3094 }
3095 if (QType->isReferenceType()) {
3096 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3097 << getOpenMPClauseName(OMPC_linear) << QType;
3098 bool IsDecl =
3099 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3100 Diag(VD->getLocation(),
3101 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3102 << VD;
3103 continue;
3104 }
3105
3106 // A list item must not be const-qualified.
3107 if (QType.isConstant(Context)) {
3108 Diag(ELoc, diag::err_omp_const_variable)
3109 << getOpenMPClauseName(OMPC_linear);
3110 bool IsDecl =
3111 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3112 Diag(VD->getLocation(),
3113 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3114 << VD;
3115 continue;
3116 }
3117
3118 // A list item must be of integral or pointer type.
3119 QType = QType.getUnqualifiedType().getCanonicalType();
3120 const Type *Ty = QType.getTypePtrOrNull();
3121 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3122 !Ty->isPointerType())) {
3123 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3124 bool IsDecl =
3125 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3126 Diag(VD->getLocation(),
3127 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3128 << VD;
3129 continue;
3130 }
3131
3132 DSAStack->addDSA(VD, DE, OMPC_linear);
3133 Vars.push_back(DE);
3134 }
3135
3136 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003137 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003138
3139 Expr *StepExpr = Step;
3140 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3141 !Step->isInstantiationDependent() &&
3142 !Step->containsUnexpandedParameterPack()) {
3143 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003144 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003145 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003146 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003147 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003148
3149 // Warn about zero linear step (it would be probably better specified as
3150 // making corresponding variables 'const').
3151 llvm::APSInt Result;
3152 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3153 !Result.isNegative() && !Result.isStrictlyPositive())
3154 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3155 << (Vars.size() > 1);
3156 }
3157
3158 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3159 Vars, StepExpr);
3160}
3161
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003162OMPClause *Sema::ActOnOpenMPAlignedClause(
3163 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3164 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3165
3166 SmallVector<Expr *, 8> Vars;
3167 for (auto &RefExpr : VarList) {
3168 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3169 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3170 // It will be analyzed later.
3171 Vars.push_back(RefExpr);
3172 continue;
3173 }
3174
3175 SourceLocation ELoc = RefExpr->getExprLoc();
3176 // OpenMP [2.1, C/C++]
3177 // A list item is a variable name.
3178 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3179 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3180 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3181 continue;
3182 }
3183
3184 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3185
3186 // OpenMP [2.8.1, simd construct, Restrictions]
3187 // The type of list items appearing in the aligned clause must be
3188 // array, pointer, reference to array, or reference to pointer.
3189 QualType QType = DE->getType()
3190 .getNonReferenceType()
3191 .getUnqualifiedType()
3192 .getCanonicalType();
3193 const Type *Ty = QType.getTypePtrOrNull();
3194 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3195 !Ty->isPointerType())) {
3196 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3197 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3198 bool IsDecl =
3199 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3200 Diag(VD->getLocation(),
3201 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3202 << VD;
3203 continue;
3204 }
3205
3206 // OpenMP [2.8.1, simd construct, Restrictions]
3207 // A list-item cannot appear in more than one aligned clause.
3208 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3209 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3210 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3211 << getOpenMPClauseName(OMPC_aligned);
3212 continue;
3213 }
3214
3215 Vars.push_back(DE);
3216 }
3217
3218 // OpenMP [2.8.1, simd construct, Description]
3219 // The parameter of the aligned clause, alignment, must be a constant
3220 // positive integer expression.
3221 // If no optional parameter is specified, implementation-defined default
3222 // alignments for SIMD instructions on the target platforms are assumed.
3223 if (Alignment != nullptr) {
3224 ExprResult AlignResult =
3225 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3226 if (AlignResult.isInvalid())
3227 return nullptr;
3228 Alignment = AlignResult.get();
3229 }
3230 if (Vars.empty())
3231 return nullptr;
3232
3233 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3234 EndLoc, Vars, Alignment);
3235}
3236
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003237OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3238 SourceLocation StartLoc,
3239 SourceLocation LParenLoc,
3240 SourceLocation EndLoc) {
3241 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003242 for (auto &RefExpr : VarList) {
3243 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3244 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003245 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003246 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003247 continue;
3248 }
3249
Alexey Bataeved09d242014-05-28 05:53:51 +00003250 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003251 // OpenMP [2.1, C/C++]
3252 // A list item is a variable name.
3253 // OpenMP [2.14.4.1, Restrictions, p.1]
3254 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003255 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003256 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003257 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003258 continue;
3259 }
3260
3261 Decl *D = DE->getDecl();
3262 VarDecl *VD = cast<VarDecl>(D);
3263
3264 QualType Type = VD->getType();
3265 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3266 // It will be analyzed later.
3267 Vars.push_back(DE);
3268 continue;
3269 }
3270
3271 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3272 // A list item that appears in a copyin clause must be threadprivate.
3273 if (!DSAStack->isThreadPrivate(VD)) {
3274 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003275 << getOpenMPClauseName(OMPC_copyin)
3276 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003277 continue;
3278 }
3279
3280 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3281 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003282 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003283 // operator for the class type.
3284 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003285 CXXRecordDecl *RD =
3286 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003287 // FIXME This code must be replaced by actual assignment of the
3288 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003289 if (RD) {
3290 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3291 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003292 if (MD) {
3293 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3294 MD->isDeleted()) {
3295 Diag(ELoc, diag::err_omp_required_method)
3296 << getOpenMPClauseName(OMPC_copyin) << 2;
3297 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3298 VarDecl::DeclarationOnly;
3299 Diag(VD->getLocation(),
3300 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3301 << VD;
3302 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3303 continue;
3304 }
3305 MarkFunctionReferenced(ELoc, MD);
3306 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003307 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003308 }
3309
3310 DSAStack->addDSA(VD, DE, OMPC_copyin);
3311 Vars.push_back(DE);
3312 }
3313
Alexey Bataeved09d242014-05-28 05:53:51 +00003314 if (Vars.empty())
3315 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003316
3317 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3318}
3319
Alexey Bataevbae9a792014-06-27 10:37:06 +00003320OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
3321 SourceLocation StartLoc,
3322 SourceLocation LParenLoc,
3323 SourceLocation EndLoc) {
3324 SmallVector<Expr *, 8> Vars;
3325 for (auto &RefExpr : VarList) {
3326 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
3327 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3328 // It will be analyzed later.
3329 Vars.push_back(RefExpr);
3330 continue;
3331 }
3332
3333 SourceLocation ELoc = RefExpr->getExprLoc();
3334 // OpenMP [2.1, C/C++]
3335 // A list item is a variable name.
3336 // OpenMP [2.14.4.1, Restrictions, p.1]
3337 // A list item that appears in a copyin clause must be threadprivate.
3338 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3339 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3340 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3341 continue;
3342 }
3343
3344 Decl *D = DE->getDecl();
3345 VarDecl *VD = cast<VarDecl>(D);
3346
3347 QualType Type = VD->getType();
3348 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3349 // It will be analyzed later.
3350 Vars.push_back(DE);
3351 continue;
3352 }
3353
3354 // OpenMP [2.14.4.2, Restrictions, p.2]
3355 // A list item that appears in a copyprivate clause may not appear in a
3356 // private or firstprivate clause on the single construct.
3357 if (!DSAStack->isThreadPrivate(VD)) {
3358 auto DVar = DSAStack->getTopDSA(VD);
3359 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
3360 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
3361 Diag(ELoc, diag::err_omp_wrong_dsa)
3362 << getOpenMPClauseName(DVar.CKind)
3363 << getOpenMPClauseName(OMPC_copyprivate);
3364 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3365 continue;
3366 }
3367
3368 // OpenMP [2.11.4.2, Restrictions, p.1]
3369 // All list items that appear in a copyprivate clause must be either
3370 // threadprivate or private in the enclosing context.
3371 if (DVar.CKind == OMPC_unknown) {
3372 DVar = DSAStack->getImplicitDSA(VD);
3373 if (DVar.CKind == OMPC_shared) {
3374 Diag(ELoc, diag::err_omp_required_access)
3375 << getOpenMPClauseName(OMPC_copyprivate)
3376 << "threadprivate or private in the enclosing context";
3377 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3378 continue;
3379 }
3380 }
3381 }
3382
3383 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3384 // A variable of class type (or array thereof) that appears in a
3385 // copyin clause requires an accessible, unambiguous copy assignment
3386 // operator for the class type.
3387 Type = Context.getBaseElementType(Type);
3388 CXXRecordDecl *RD =
3389 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
3390 // FIXME This code must be replaced by actual assignment of the
3391 // threadprivate variable.
3392 if (RD) {
3393 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3394 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
3395 if (MD) {
3396 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3397 MD->isDeleted()) {
3398 Diag(ELoc, diag::err_omp_required_method)
3399 << getOpenMPClauseName(OMPC_copyprivate) << 2;
3400 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3401 VarDecl::DeclarationOnly;
3402 Diag(VD->getLocation(),
3403 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3404 << VD;
3405 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3406 continue;
3407 }
3408 MarkFunctionReferenced(ELoc, MD);
3409 DiagnoseUseOfDecl(MD, ELoc);
3410 }
3411 }
3412
3413 // No need to mark vars as copyprivate, they are already threadprivate or
3414 // implicitly private.
3415 Vars.push_back(DE);
3416 }
3417
3418 if (Vars.empty())
3419 return nullptr;
3420
3421 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3422}
3423
Alexey Bataev758e55e2013-09-06 18:03:48 +00003424#undef DSAStack