blob: c7e377e2181c90099805c048958b6c8dfa9e7af9 [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) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +0000974 // Allowed nesting of constructs
975 // +------------------+-----------------+------------------------------------+
976 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
977 // +------------------+-----------------+------------------------------------+
978 // | parallel | parallel | * |
979 // | parallel | for | * |
980 // | parallel | simd | * |
981 // | parallel | sections | * |
982 // | parallel | section | + |
983 // | parallel | single | * |
984 // +------------------+-----------------+------------------------------------+
985 // | for | parallel | * |
986 // | for | for | + |
987 // | for | simd | * |
988 // | for | sections | + |
989 // | for | section | + |
990 // | for | single | + |
991 // +------------------+-----------------+------------------------------------+
992 // | simd | parallel | |
993 // | simd | for | |
994 // | simd | simd | |
995 // | simd | sections | |
996 // | simd | section | |
997 // | simd | single | |
998 // +------------------+-----------------+------------------------------------+
999 // | sections | parallel | * |
1000 // | sections | for | + |
1001 // | sections | simd | * |
1002 // | sections | sections | + |
1003 // | sections | section | * |
1004 // | sections | single | + |
1005 // +------------------+-----------------+------------------------------------+
1006 // | section | parallel | * |
1007 // | section | for | + |
1008 // | section | simd | * |
1009 // | section | sections | + |
1010 // | section | section | + |
1011 // | section | single | + |
1012 // +------------------+-----------------+------------------------------------+
1013 // | single | parallel | * |
1014 // | single | for | + |
1015 // | single | simd | * |
1016 // | single | sections | + |
1017 // | single | section | + |
1018 // | single | single | + |
1019 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001020 if (Stack->getCurScope()) {
1021 auto ParentRegion = Stack->getParentDirective();
1022 bool NestingProhibited = false;
1023 bool CloseNesting = true;
1024 bool ShouldBeInParallelRegion = false;
1025 if (isOpenMPSimdDirective(ParentRegion)) {
1026 // OpenMP [2.16, Nesting of Regions]
1027 // OpenMP constructs may not be nested inside a simd region.
1028 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1029 return true;
1030 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001031 if (CurrentRegion == OMPD_section) {
1032 // OpenMP [2.7.2, sections Construct, Restrictions]
1033 // Orphaned section directives are prohibited. That is, the section
1034 // directives must appear within the sections construct and must not be
1035 // encountered elsewhere in the sections region.
1036 if (ParentRegion != OMPD_sections) {
1037 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1038 << (ParentRegion != OMPD_unknown)
1039 << getOpenMPDirectiveName(ParentRegion);
1040 return true;
1041 }
1042 return false;
1043 }
Alexey Bataev549210e2014-06-24 04:39:47 +00001044 if (isOpenMPWorksharingDirective(CurrentRegion) &&
1045 !isOpenMPParallelDirective(CurrentRegion) &&
1046 !isOpenMPSimdDirective(CurrentRegion)) {
1047 // OpenMP [2.16, Nesting of Regions]
1048 // A worksharing region may not be closely nested inside a worksharing,
1049 // explicit task, critical, ordered, atomic, or master region.
1050 // TODO
1051 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) &&
1052 !isOpenMPSimdDirective(ParentRegion);
1053 ShouldBeInParallelRegion = true;
1054 }
1055 if (NestingProhibited) {
1056 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
1057 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << true
1058 << getOpenMPDirectiveName(CurrentRegion) << ShouldBeInParallelRegion;
1059 return true;
1060 }
1061 }
1062 return false;
1063}
1064
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001065StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1066 ArrayRef<OMPClause *> Clauses,
1067 Stmt *AStmt,
1068 SourceLocation StartLoc,
1069 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001070 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1071
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001072 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +00001073 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
1074 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001075
1076 // Check default data sharing attributes for referenced variables.
1077 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1078 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1079 if (DSAChecker.isErrorFound())
1080 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001081 // Generate list of implicitly defined firstprivate variables.
1082 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1083 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1084
1085 bool ErrorFound = false;
1086 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001087 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1088 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1089 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001090 ClausesWithImplicit.push_back(Implicit);
1091 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +00001092 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001093 } else
1094 ErrorFound = true;
1095 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001096
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001097 switch (Kind) {
1098 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001099 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1100 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001101 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001102 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +00001103 Res =
1104 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001105 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001106 case OMPD_for:
1107 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1108 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001109 case OMPD_sections:
1110 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1111 EndLoc);
1112 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001113 case OMPD_section:
1114 assert(ClausesWithImplicit.empty() &&
1115 "No clauses is allowed for 'omp section' directive");
1116 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1117 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001118 case OMPD_single:
1119 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1120 EndLoc);
1121 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001122 case OMPD_threadprivate:
1123 case OMPD_task:
1124 llvm_unreachable("OpenMP Directive is not allowed");
1125 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001126 llvm_unreachable("Unknown OpenMP directive");
1127 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001128
Alexey Bataeved09d242014-05-28 05:53:51 +00001129 if (ErrorFound)
1130 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001131 return Res;
1132}
1133
1134StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1135 Stmt *AStmt,
1136 SourceLocation StartLoc,
1137 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001138 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1139 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1140 // 1.2.2 OpenMP Language Terminology
1141 // Structured block - An executable statement with a single entry at the
1142 // top and a single exit at the bottom.
1143 // The point of exit cannot be a branch out of the structured block.
1144 // longjmp() and throw() must not violate the entry/exit criteria.
1145 CS->getCapturedDecl()->setNothrow();
1146
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001147 getCurFunction()->setHasBranchProtectedScope();
1148
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001149 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1150 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001151}
1152
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001153namespace {
1154/// \brief Helper class for checking canonical form of the OpenMP loops and
1155/// extracting iteration space of each loop in the loop nest, that will be used
1156/// for IR generation.
1157class OpenMPIterationSpaceChecker {
1158 /// \brief Reference to Sema.
1159 Sema &SemaRef;
1160 /// \brief A location for diagnostics (when there is no some better location).
1161 SourceLocation DefaultLoc;
1162 /// \brief A location for diagnostics (when increment is not compatible).
1163 SourceLocation ConditionLoc;
1164 /// \brief A source location for referring to condition later.
1165 SourceRange ConditionSrcRange;
1166 /// \brief Loop variable.
1167 VarDecl *Var;
1168 /// \brief Lower bound (initializer for the var).
1169 Expr *LB;
1170 /// \brief Upper bound.
1171 Expr *UB;
1172 /// \brief Loop step (increment).
1173 Expr *Step;
1174 /// \brief This flag is true when condition is one of:
1175 /// Var < UB
1176 /// Var <= UB
1177 /// UB > Var
1178 /// UB >= Var
1179 bool TestIsLessOp;
1180 /// \brief This flag is true when condition is strict ( < or > ).
1181 bool TestIsStrictOp;
1182 /// \brief This flag is true when step is subtracted on each iteration.
1183 bool SubtractStep;
1184
1185public:
1186 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1187 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1188 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1189 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1190 SubtractStep(false) {}
1191 /// \brief Check init-expr for canonical loop form and save loop counter
1192 /// variable - #Var and its initialization value - #LB.
1193 bool CheckInit(Stmt *S);
1194 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1195 /// for less/greater and for strict/non-strict comparison.
1196 bool CheckCond(Expr *S);
1197 /// \brief Check incr-expr for canonical loop form and return true if it
1198 /// does not conform, otherwise save loop step (#Step).
1199 bool CheckInc(Expr *S);
1200 /// \brief Return the loop counter variable.
1201 VarDecl *GetLoopVar() const { return Var; }
1202 /// \brief Return true if any expression is dependent.
1203 bool Dependent() const;
1204
1205private:
1206 /// \brief Check the right-hand side of an assignment in the increment
1207 /// expression.
1208 bool CheckIncRHS(Expr *RHS);
1209 /// \brief Helper to set loop counter variable and its initializer.
1210 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1211 /// \brief Helper to set upper bound.
1212 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1213 const SourceLocation &SL);
1214 /// \brief Helper to set loop increment.
1215 bool SetStep(Expr *NewStep, bool Subtract);
1216};
1217
1218bool OpenMPIterationSpaceChecker::Dependent() const {
1219 if (!Var) {
1220 assert(!LB && !UB && !Step);
1221 return false;
1222 }
1223 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1224 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1225}
1226
1227bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1228 // State consistency checking to ensure correct usage.
1229 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1230 !TestIsLessOp && !TestIsStrictOp);
1231 if (!NewVar || !NewLB)
1232 return true;
1233 Var = NewVar;
1234 LB = NewLB;
1235 return false;
1236}
1237
1238bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1239 const SourceRange &SR,
1240 const SourceLocation &SL) {
1241 // State consistency checking to ensure correct usage.
1242 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1243 !TestIsLessOp && !TestIsStrictOp);
1244 if (!NewUB)
1245 return true;
1246 UB = NewUB;
1247 TestIsLessOp = LessOp;
1248 TestIsStrictOp = StrictOp;
1249 ConditionSrcRange = SR;
1250 ConditionLoc = SL;
1251 return false;
1252}
1253
1254bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1255 // State consistency checking to ensure correct usage.
1256 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1257 if (!NewStep)
1258 return true;
1259 if (!NewStep->isValueDependent()) {
1260 // Check that the step is integer expression.
1261 SourceLocation StepLoc = NewStep->getLocStart();
1262 ExprResult Val =
1263 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1264 if (Val.isInvalid())
1265 return true;
1266 NewStep = Val.get();
1267
1268 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1269 // If test-expr is of form var relational-op b and relational-op is < or
1270 // <= then incr-expr must cause var to increase on each iteration of the
1271 // loop. If test-expr is of form var relational-op b and relational-op is
1272 // > or >= then incr-expr must cause var to decrease on each iteration of
1273 // the loop.
1274 // If test-expr is of form b relational-op var and relational-op is < or
1275 // <= then incr-expr must cause var to decrease on each iteration of the
1276 // loop. If test-expr is of form b relational-op var and relational-op is
1277 // > or >= then incr-expr must cause var to increase on each iteration of
1278 // the loop.
1279 llvm::APSInt Result;
1280 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1281 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1282 bool IsConstNeg =
1283 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1284 bool IsConstZero = IsConstant && !Result.getBoolValue();
1285 if (UB && (IsConstZero ||
1286 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1287 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1288 SemaRef.Diag(NewStep->getExprLoc(),
1289 diag::err_omp_loop_incr_not_compatible)
1290 << Var << TestIsLessOp << NewStep->getSourceRange();
1291 SemaRef.Diag(ConditionLoc,
1292 diag::note_omp_loop_cond_requres_compatible_incr)
1293 << TestIsLessOp << ConditionSrcRange;
1294 return true;
1295 }
1296 }
1297
1298 Step = NewStep;
1299 SubtractStep = Subtract;
1300 return false;
1301}
1302
1303bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1304 // Check init-expr for canonical loop form and save loop counter
1305 // variable - #Var and its initialization value - #LB.
1306 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1307 // var = lb
1308 // integer-type var = lb
1309 // random-access-iterator-type var = lb
1310 // pointer-type var = lb
1311 //
1312 if (!S) {
1313 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1314 return true;
1315 }
1316 if (Expr *E = dyn_cast<Expr>(S))
1317 S = E->IgnoreParens();
1318 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1319 if (BO->getOpcode() == BO_Assign)
1320 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1321 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1322 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1323 if (DS->isSingleDecl()) {
1324 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1325 if (Var->hasInit()) {
1326 // Accept non-canonical init form here but emit ext. warning.
1327 if (Var->getInitStyle() != VarDecl::CInit)
1328 SemaRef.Diag(S->getLocStart(),
1329 diag::ext_omp_loop_not_canonical_init)
1330 << S->getSourceRange();
1331 return SetVarAndLB(Var, Var->getInit());
1332 }
1333 }
1334 }
1335 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1336 if (CE->getOperator() == OO_Equal)
1337 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1338 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1339
1340 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1341 << S->getSourceRange();
1342 return true;
1343}
1344
Alexey Bataev23b69422014-06-18 07:08:49 +00001345/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001346/// variable (which may be the loop variable) if possible.
1347static const VarDecl *GetInitVarDecl(const Expr *E) {
1348 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001349 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001350 E = E->IgnoreParenImpCasts();
1351 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1352 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1353 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1354 CE->getArg(0) != nullptr)
1355 E = CE->getArg(0)->IgnoreParenImpCasts();
1356 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1357 if (!DRE)
1358 return nullptr;
1359 return dyn_cast<VarDecl>(DRE->getDecl());
1360}
1361
1362bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1363 // Check test-expr for canonical form, save upper-bound UB, flags for
1364 // less/greater and for strict/non-strict comparison.
1365 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1366 // var relational-op b
1367 // b relational-op var
1368 //
1369 if (!S) {
1370 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1371 return true;
1372 }
1373 S = S->IgnoreParenImpCasts();
1374 SourceLocation CondLoc = S->getLocStart();
1375 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1376 if (BO->isRelationalOp()) {
1377 if (GetInitVarDecl(BO->getLHS()) == Var)
1378 return SetUB(BO->getRHS(),
1379 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1380 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1381 BO->getSourceRange(), BO->getOperatorLoc());
1382 if (GetInitVarDecl(BO->getRHS()) == Var)
1383 return SetUB(BO->getLHS(),
1384 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1385 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1386 BO->getSourceRange(), BO->getOperatorLoc());
1387 }
1388 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1389 if (CE->getNumArgs() == 2) {
1390 auto Op = CE->getOperator();
1391 switch (Op) {
1392 case OO_Greater:
1393 case OO_GreaterEqual:
1394 case OO_Less:
1395 case OO_LessEqual:
1396 if (GetInitVarDecl(CE->getArg(0)) == Var)
1397 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1398 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1399 CE->getOperatorLoc());
1400 if (GetInitVarDecl(CE->getArg(1)) == Var)
1401 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1402 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1403 CE->getOperatorLoc());
1404 break;
1405 default:
1406 break;
1407 }
1408 }
1409 }
1410 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1411 << S->getSourceRange() << Var;
1412 return true;
1413}
1414
1415bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1416 // RHS of canonical loop form increment can be:
1417 // var + incr
1418 // incr + var
1419 // var - incr
1420 //
1421 RHS = RHS->IgnoreParenImpCasts();
1422 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1423 if (BO->isAdditiveOp()) {
1424 bool IsAdd = BO->getOpcode() == BO_Add;
1425 if (GetInitVarDecl(BO->getLHS()) == Var)
1426 return SetStep(BO->getRHS(), !IsAdd);
1427 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1428 return SetStep(BO->getLHS(), false);
1429 }
1430 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1431 bool IsAdd = CE->getOperator() == OO_Plus;
1432 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1433 if (GetInitVarDecl(CE->getArg(0)) == Var)
1434 return SetStep(CE->getArg(1), !IsAdd);
1435 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1436 return SetStep(CE->getArg(0), false);
1437 }
1438 }
1439 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1440 << RHS->getSourceRange() << Var;
1441 return true;
1442}
1443
1444bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1445 // Check incr-expr for canonical loop form and return true if it
1446 // does not conform.
1447 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1448 // ++var
1449 // var++
1450 // --var
1451 // var--
1452 // var += incr
1453 // var -= incr
1454 // var = var + incr
1455 // var = incr + var
1456 // var = var - incr
1457 //
1458 if (!S) {
1459 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1460 return true;
1461 }
1462 S = S->IgnoreParens();
1463 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1464 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1465 return SetStep(
1466 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1467 (UO->isDecrementOp() ? -1 : 1)).get(),
1468 false);
1469 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1470 switch (BO->getOpcode()) {
1471 case BO_AddAssign:
1472 case BO_SubAssign:
1473 if (GetInitVarDecl(BO->getLHS()) == Var)
1474 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1475 break;
1476 case BO_Assign:
1477 if (GetInitVarDecl(BO->getLHS()) == Var)
1478 return CheckIncRHS(BO->getRHS());
1479 break;
1480 default:
1481 break;
1482 }
1483 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1484 switch (CE->getOperator()) {
1485 case OO_PlusPlus:
1486 case OO_MinusMinus:
1487 if (GetInitVarDecl(CE->getArg(0)) == Var)
1488 return SetStep(
1489 SemaRef.ActOnIntegerConstant(
1490 CE->getLocStart(),
1491 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1492 false);
1493 break;
1494 case OO_PlusEqual:
1495 case OO_MinusEqual:
1496 if (GetInitVarDecl(CE->getArg(0)) == Var)
1497 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1498 break;
1499 case OO_Equal:
1500 if (GetInitVarDecl(CE->getArg(0)) == Var)
1501 return CheckIncRHS(CE->getArg(1));
1502 break;
1503 default:
1504 break;
1505 }
1506 }
1507 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1508 << S->getSourceRange() << Var;
1509 return true;
1510}
Alexey Bataev23b69422014-06-18 07:08:49 +00001511} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001512
1513/// \brief Called on a for stmt to check and extract its iteration space
1514/// for further processing (such as collapsing).
1515static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001516 Sema &SemaRef, DSAStackTy &DSA,
1517 unsigned CurrentNestedLoopCount,
1518 unsigned NestedLoopCount,
1519 Expr *NestedLoopCountExpr) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001520 // OpenMP [2.6, Canonical Loop Form]
1521 // for (init-expr; test-expr; incr-expr) structured-block
1522 auto For = dyn_cast_or_null<ForStmt>(S);
1523 if (!For) {
1524 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001525 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1526 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1527 << CurrentNestedLoopCount;
1528 if (NestedLoopCount > 1)
1529 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1530 diag::note_omp_collapse_expr)
1531 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001532 return true;
1533 }
1534 assert(For->getBody());
1535
1536 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1537
1538 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001539 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001540 if (ISC.CheckInit(Init)) {
1541 return true;
1542 }
1543
1544 bool HasErrors = false;
1545
1546 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001547 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001548
1549 // OpenMP [2.6, Canonical Loop Form]
1550 // Var is one of the following:
1551 // A variable of signed or unsigned integer type.
1552 // For C++, a variable of a random access iterator type.
1553 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001554 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001555 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1556 !VarType->isPointerType() &&
1557 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1558 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1559 << SemaRef.getLangOpts().CPlusPlus;
1560 HasErrors = true;
1561 }
1562
1563 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1564 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001565 // The loop iteration variable in the associated for-loop of a simd construct
1566 // with just one associated for-loop may be listed in a linear clause with a
1567 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001568 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1569 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001570 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001571 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1572 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) ||
1573 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1574 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001575 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001576 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1577 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001578 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001579 HasErrors = true;
1580 } else {
1581 // Make the loop iteration variable private by default.
1582 DSA.addDSA(Var, nullptr, OMPC_private);
1583 }
1584
Alexey Bataev7ff55242014-06-19 09:13:45 +00001585 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001586
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001587 // Check test-expr.
1588 HasErrors |= ISC.CheckCond(For->getCond());
1589
1590 // Check incr-expr.
1591 HasErrors |= ISC.CheckInc(For->getInc());
1592
1593 if (ISC.Dependent())
1594 return HasErrors;
1595
1596 // FIXME: Build loop's iteration space representation.
1597 return HasErrors;
1598}
1599
1600/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1601/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1602/// to get the first for loop.
1603static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1604 if (IgnoreCaptured)
1605 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1606 S = CapS->getCapturedStmt();
1607 // OpenMP [2.8.1, simd construct, Restrictions]
1608 // All loops associated with the construct must be perfectly nested; that is,
1609 // there must be no intervening code nor any OpenMP directive between any two
1610 // loops.
1611 while (true) {
1612 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1613 S = AS->getSubStmt();
1614 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1615 if (CS->size() != 1)
1616 break;
1617 S = CS->body_back();
1618 } else
1619 break;
1620 }
1621 return S;
1622}
1623
1624/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001625/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1626/// number of collapsed loops otherwise.
1627static unsigned CheckOpenMPLoop(OpenMPDirectiveKind DKind,
1628 Expr *NestedLoopCountExpr, Stmt *AStmt,
1629 Sema &SemaRef, DSAStackTy &DSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001630 unsigned NestedLoopCount = 1;
1631 if (NestedLoopCountExpr) {
1632 // Found 'collapse' clause - calculate collapse number.
1633 llvm::APSInt Result;
1634 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1635 NestedLoopCount = Result.getLimitedValue();
1636 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001637 // This is helper routine for loop directives (e.g., 'for', 'simd',
1638 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001639 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1640 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001641 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
1642 NestedLoopCount, NestedLoopCountExpr))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001643 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001644 // Move on to the next nested for loop, or to the loop body.
1645 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1646 }
1647
1648 // FIXME: Build resulting iteration space for IR generation (collapsing
1649 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001650 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001651}
1652
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001653static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001654 auto CollapseFilter = [](const OMPClause *C) -> bool {
1655 return C->getClauseKind() == OMPC_collapse;
1656 };
1657 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1658 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001659 if (I)
1660 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1661 return nullptr;
1662}
1663
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001664StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001665 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001666 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001667 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataevabfc0692014-06-25 06:52:00 +00001668 unsigned NestedLoopCount = CheckOpenMPLoop(
1669 OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack);
1670 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001671 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001672
1673 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001674 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1675 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001676}
1677
Alexey Bataevf29276e2014-06-18 04:14:57 +00001678StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses,
1679 Stmt *AStmt, SourceLocation StartLoc,
1680 SourceLocation EndLoc) {
1681 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataevabfc0692014-06-25 06:52:00 +00001682 unsigned NestedLoopCount = CheckOpenMPLoop(
1683 OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack);
1684 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001685 return StmtError();
1686
1687 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001688 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1689 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001690}
1691
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001692StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1693 Stmt *AStmt,
1694 SourceLocation StartLoc,
1695 SourceLocation EndLoc) {
1696 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1697 auto BaseStmt = AStmt;
1698 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1699 BaseStmt = CS->getCapturedStmt();
1700 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1701 auto S = C->children();
1702 if (!S)
1703 return StmtError();
1704 // All associated statements must be '#pragma omp section' except for
1705 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001706 for (++S; S; ++S) {
1707 auto SectionStmt = *S;
1708 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1709 if (SectionStmt)
1710 Diag(SectionStmt->getLocStart(),
1711 diag::err_omp_sections_substmt_not_section);
1712 return StmtError();
1713 }
1714 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001715 } else {
1716 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1717 return StmtError();
1718 }
1719
1720 getCurFunction()->setHasBranchProtectedScope();
1721
1722 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1723 AStmt);
1724}
1725
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001726StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1727 SourceLocation StartLoc,
1728 SourceLocation EndLoc) {
1729 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1730
1731 getCurFunction()->setHasBranchProtectedScope();
1732
1733 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1734}
1735
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001736StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
1737 Stmt *AStmt,
1738 SourceLocation StartLoc,
1739 SourceLocation EndLoc) {
1740 getCurFunction()->setHasBranchProtectedScope();
1741 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1742}
1743
Alexey Bataeved09d242014-05-28 05:53:51 +00001744OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001745 SourceLocation StartLoc,
1746 SourceLocation LParenLoc,
1747 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001748 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001749 switch (Kind) {
1750 case OMPC_if:
1751 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1752 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001753 case OMPC_num_threads:
1754 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1755 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001756 case OMPC_safelen:
1757 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1758 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001759 case OMPC_collapse:
1760 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1761 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001762 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001763 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001764 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001765 case OMPC_private:
1766 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001767 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001768 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001769 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001770 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001771 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001772 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001773 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001774 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001775 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001776 case OMPC_threadprivate:
1777 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001778 llvm_unreachable("Clause is not allowed.");
1779 }
1780 return Res;
1781}
1782
Alexey Bataeved09d242014-05-28 05:53:51 +00001783OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001784 SourceLocation LParenLoc,
1785 SourceLocation EndLoc) {
1786 Expr *ValExpr = Condition;
1787 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1788 !Condition->isInstantiationDependent() &&
1789 !Condition->containsUnexpandedParameterPack()) {
1790 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001791 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001792 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001793 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001794
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001795 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001796 }
1797
1798 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1799}
1800
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001801ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1802 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001803 if (!Op)
1804 return ExprError();
1805
1806 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1807 public:
1808 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001809 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001810 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1811 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001812 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1813 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001814 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1815 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001816 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1817 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001818 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1819 QualType T,
1820 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001821 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1822 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001823 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1824 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001825 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001826 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001827 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001828 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1829 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001830 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1831 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001832 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1833 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001834 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001835 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001836 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001837 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1838 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001839 llvm_unreachable("conversion functions are permitted");
1840 }
1841 } ConvertDiagnoser;
1842 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1843}
1844
1845OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1846 SourceLocation StartLoc,
1847 SourceLocation LParenLoc,
1848 SourceLocation EndLoc) {
1849 Expr *ValExpr = NumThreads;
1850 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1851 !NumThreads->isInstantiationDependent() &&
1852 !NumThreads->containsUnexpandedParameterPack()) {
1853 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1854 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001855 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001856 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001857 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001858
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001859 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001860
1861 // OpenMP [2.5, Restrictions]
1862 // The num_threads expression must evaluate to a positive integer value.
1863 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001864 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1865 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001866 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1867 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001868 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001869 }
1870 }
1871
Alexey Bataeved09d242014-05-28 05:53:51 +00001872 return new (Context)
1873 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001874}
1875
Alexey Bataev62c87d22014-03-21 04:51:18 +00001876ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1877 OpenMPClauseKind CKind) {
1878 if (!E)
1879 return ExprError();
1880 if (E->isValueDependent() || E->isTypeDependent() ||
1881 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001882 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001883 llvm::APSInt Result;
1884 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1885 if (ICE.isInvalid())
1886 return ExprError();
1887 if (!Result.isStrictlyPositive()) {
1888 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1889 << getOpenMPClauseName(CKind) << E->getSourceRange();
1890 return ExprError();
1891 }
1892 return ICE;
1893}
1894
1895OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1896 SourceLocation LParenLoc,
1897 SourceLocation EndLoc) {
1898 // OpenMP [2.8.1, simd construct, Description]
1899 // The parameter of the safelen clause must be a constant
1900 // positive integer expression.
1901 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1902 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001903 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001904 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001905 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001906}
1907
Alexander Musman64d33f12014-06-04 07:53:32 +00001908OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1909 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001910 SourceLocation LParenLoc,
1911 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001912 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001913 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001914 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001915 // The parameter of the collapse clause must be a constant
1916 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001917 ExprResult NumForLoopsResult =
1918 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1919 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001920 return nullptr;
1921 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001922 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001923}
1924
Alexey Bataeved09d242014-05-28 05:53:51 +00001925OMPClause *Sema::ActOnOpenMPSimpleClause(
1926 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1927 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001928 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001929 switch (Kind) {
1930 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001931 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001932 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1933 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001934 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001935 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001936 Res = ActOnOpenMPProcBindClause(
1937 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1938 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001939 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001940 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001941 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001942 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001943 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001944 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001945 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001946 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001947 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001948 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001949 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001950 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001951 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001952 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001953 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001954 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001955 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001956 case OMPC_threadprivate:
1957 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001958 llvm_unreachable("Clause is not allowed.");
1959 }
1960 return Res;
1961}
1962
1963OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1964 SourceLocation KindKwLoc,
1965 SourceLocation StartLoc,
1966 SourceLocation LParenLoc,
1967 SourceLocation EndLoc) {
1968 if (Kind == OMPC_DEFAULT_unknown) {
1969 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001970 static_assert(OMPC_DEFAULT_unknown > 0,
1971 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001972 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001973 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001974 Values += "'";
1975 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1976 Values += "'";
1977 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001978 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001979 Values += " or ";
1980 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001981 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001982 break;
1983 default:
1984 Values += Sep;
1985 break;
1986 }
1987 }
1988 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001989 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001990 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001991 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001992 switch (Kind) {
1993 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001994 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001995 break;
1996 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001997 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001998 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001999 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002000 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002001 break;
2002 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002003 return new (Context)
2004 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002005}
2006
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002007OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2008 SourceLocation KindKwLoc,
2009 SourceLocation StartLoc,
2010 SourceLocation LParenLoc,
2011 SourceLocation EndLoc) {
2012 if (Kind == OMPC_PROC_BIND_unknown) {
2013 std::string Values;
2014 std::string Sep(", ");
2015 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2016 Values += "'";
2017 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2018 Values += "'";
2019 switch (i) {
2020 case OMPC_PROC_BIND_unknown - 2:
2021 Values += " or ";
2022 break;
2023 case OMPC_PROC_BIND_unknown - 1:
2024 break;
2025 default:
2026 Values += Sep;
2027 break;
2028 }
2029 }
2030 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002031 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002032 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002033 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002034 return new (Context)
2035 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002036}
2037
Alexey Bataev56dafe82014-06-20 07:16:17 +00002038OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2039 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2040 SourceLocation StartLoc, SourceLocation LParenLoc,
2041 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2042 SourceLocation EndLoc) {
2043 OMPClause *Res = nullptr;
2044 switch (Kind) {
2045 case OMPC_schedule:
2046 Res = ActOnOpenMPScheduleClause(
2047 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2048 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2049 break;
2050 case OMPC_if:
2051 case OMPC_num_threads:
2052 case OMPC_safelen:
2053 case OMPC_collapse:
2054 case OMPC_default:
2055 case OMPC_proc_bind:
2056 case OMPC_private:
2057 case OMPC_firstprivate:
2058 case OMPC_lastprivate:
2059 case OMPC_shared:
2060 case OMPC_reduction:
2061 case OMPC_linear:
2062 case OMPC_aligned:
2063 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002064 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002065 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002066 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002067 case OMPC_threadprivate:
2068 case OMPC_unknown:
2069 llvm_unreachable("Clause is not allowed.");
2070 }
2071 return Res;
2072}
2073
2074OMPClause *Sema::ActOnOpenMPScheduleClause(
2075 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2076 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2077 SourceLocation EndLoc) {
2078 if (Kind == OMPC_SCHEDULE_unknown) {
2079 std::string Values;
2080 std::string Sep(", ");
2081 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2082 Values += "'";
2083 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2084 Values += "'";
2085 switch (i) {
2086 case OMPC_SCHEDULE_unknown - 2:
2087 Values += " or ";
2088 break;
2089 case OMPC_SCHEDULE_unknown - 1:
2090 break;
2091 default:
2092 Values += Sep;
2093 break;
2094 }
2095 }
2096 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2097 << Values << getOpenMPClauseName(OMPC_schedule);
2098 return nullptr;
2099 }
2100 Expr *ValExpr = ChunkSize;
2101 if (ChunkSize) {
2102 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2103 !ChunkSize->isInstantiationDependent() &&
2104 !ChunkSize->containsUnexpandedParameterPack()) {
2105 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2106 ExprResult Val =
2107 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2108 if (Val.isInvalid())
2109 return nullptr;
2110
2111 ValExpr = Val.get();
2112
2113 // OpenMP [2.7.1, Restrictions]
2114 // chunk_size must be a loop invariant integer expression with a positive
2115 // value.
2116 llvm::APSInt Result;
2117 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2118 Result.isSigned() && !Result.isStrictlyPositive()) {
2119 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2120 << "schedule" << ChunkSize->getSourceRange();
2121 return nullptr;
2122 }
2123 }
2124 }
2125
2126 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2127 EndLoc, Kind, ValExpr);
2128}
2129
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002130OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2131 SourceLocation StartLoc,
2132 SourceLocation EndLoc) {
2133 OMPClause *Res = nullptr;
2134 switch (Kind) {
2135 case OMPC_ordered:
2136 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2137 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002138 case OMPC_nowait:
2139 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2140 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002141 case OMPC_if:
2142 case OMPC_num_threads:
2143 case OMPC_safelen:
2144 case OMPC_collapse:
2145 case OMPC_schedule:
2146 case OMPC_private:
2147 case OMPC_firstprivate:
2148 case OMPC_lastprivate:
2149 case OMPC_shared:
2150 case OMPC_reduction:
2151 case OMPC_linear:
2152 case OMPC_aligned:
2153 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002154 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002155 case OMPC_default:
2156 case OMPC_proc_bind:
2157 case OMPC_threadprivate:
2158 case OMPC_unknown:
2159 llvm_unreachable("Clause is not allowed.");
2160 }
2161 return Res;
2162}
2163
2164OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2165 SourceLocation EndLoc) {
2166 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2167}
2168
Alexey Bataev236070f2014-06-20 11:19:47 +00002169OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2170 SourceLocation EndLoc) {
2171 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2172}
2173
Alexey Bataevc5e02582014-06-16 07:08:35 +00002174OMPClause *Sema::ActOnOpenMPVarListClause(
2175 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2176 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2177 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2178 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002179 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002180 switch (Kind) {
2181 case OMPC_private:
2182 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2183 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002184 case OMPC_firstprivate:
2185 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2186 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002187 case OMPC_lastprivate:
2188 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2189 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002190 case OMPC_shared:
2191 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2192 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002193 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002194 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2195 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002196 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002197 case OMPC_linear:
2198 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2199 ColonLoc, EndLoc);
2200 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002201 case OMPC_aligned:
2202 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2203 ColonLoc, EndLoc);
2204 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002205 case OMPC_copyin:
2206 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2207 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002208 case OMPC_copyprivate:
2209 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2210 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002211 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002212 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002213 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002214 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002215 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002216 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002217 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002218 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002219 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002220 case OMPC_threadprivate:
2221 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002222 llvm_unreachable("Clause is not allowed.");
2223 }
2224 return Res;
2225}
2226
2227OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2228 SourceLocation StartLoc,
2229 SourceLocation LParenLoc,
2230 SourceLocation EndLoc) {
2231 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002232 for (auto &RefExpr : VarList) {
2233 assert(RefExpr && "NULL expr in OpenMP private clause.");
2234 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002235 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002236 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002237 continue;
2238 }
2239
Alexey Bataeved09d242014-05-28 05:53:51 +00002240 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002241 // OpenMP [2.1, C/C++]
2242 // A list item is a variable name.
2243 // OpenMP [2.9.3.3, Restrictions, p.1]
2244 // A variable that is part of another variable (as an array or
2245 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002246 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002247 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002248 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002249 continue;
2250 }
2251 Decl *D = DE->getDecl();
2252 VarDecl *VD = cast<VarDecl>(D);
2253
2254 QualType Type = VD->getType();
2255 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2256 // It will be analyzed later.
2257 Vars.push_back(DE);
2258 continue;
2259 }
2260
2261 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2262 // A variable that appears in a private clause must not have an incomplete
2263 // type or a reference type.
2264 if (RequireCompleteType(ELoc, Type,
2265 diag::err_omp_private_incomplete_type)) {
2266 continue;
2267 }
2268 if (Type->isReferenceType()) {
2269 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002270 << getOpenMPClauseName(OMPC_private) << Type;
2271 bool IsDecl =
2272 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2273 Diag(VD->getLocation(),
2274 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2275 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002276 continue;
2277 }
2278
2279 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2280 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002281 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002282 // class type.
2283 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002284 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2285 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002286 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002287 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2288 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2289 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002290 // FIXME This code must be replaced by actual constructing/destructing of
2291 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002292 if (RD) {
2293 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2294 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002295 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002296 if (!CD ||
2297 CheckConstructorAccess(ELoc, CD,
2298 InitializedEntity::InitializeTemporary(Type),
2299 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002300 CD->isDeleted()) {
2301 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002302 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002303 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2304 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002305 Diag(VD->getLocation(),
2306 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2307 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002308 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2309 continue;
2310 }
2311 MarkFunctionReferenced(ELoc, CD);
2312 DiagnoseUseOfDecl(CD, ELoc);
2313
2314 CXXDestructorDecl *DD = RD->getDestructor();
2315 if (DD) {
2316 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2317 DD->isDeleted()) {
2318 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002319 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002320 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2321 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002322 Diag(VD->getLocation(),
2323 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2324 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002325 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2326 continue;
2327 }
2328 MarkFunctionReferenced(ELoc, DD);
2329 DiagnoseUseOfDecl(DD, ELoc);
2330 }
2331 }
2332
Alexey Bataev758e55e2013-09-06 18:03:48 +00002333 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2334 // in a Construct]
2335 // Variables with the predetermined data-sharing attributes may not be
2336 // listed in data-sharing attributes clauses, except for the cases
2337 // listed below. For these exceptions only, listing a predetermined
2338 // variable in a data-sharing attribute clause is allowed and overrides
2339 // the variable's predetermined data-sharing attributes.
2340 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2341 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002342 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2343 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002344 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002345 continue;
2346 }
2347
2348 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002349 Vars.push_back(DE);
2350 }
2351
Alexey Bataeved09d242014-05-28 05:53:51 +00002352 if (Vars.empty())
2353 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002354
2355 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2356}
2357
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002358OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2359 SourceLocation StartLoc,
2360 SourceLocation LParenLoc,
2361 SourceLocation EndLoc) {
2362 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002363 for (auto &RefExpr : VarList) {
2364 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2365 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002366 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002367 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002368 continue;
2369 }
2370
Alexey Bataeved09d242014-05-28 05:53:51 +00002371 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002372 // OpenMP [2.1, C/C++]
2373 // A list item is a variable name.
2374 // OpenMP [2.9.3.3, Restrictions, p.1]
2375 // A variable that is part of another variable (as an array or
2376 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002377 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002378 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002379 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002380 continue;
2381 }
2382 Decl *D = DE->getDecl();
2383 VarDecl *VD = cast<VarDecl>(D);
2384
2385 QualType Type = VD->getType();
2386 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2387 // It will be analyzed later.
2388 Vars.push_back(DE);
2389 continue;
2390 }
2391
2392 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2393 // A variable that appears in a private clause must not have an incomplete
2394 // type or a reference type.
2395 if (RequireCompleteType(ELoc, Type,
2396 diag::err_omp_firstprivate_incomplete_type)) {
2397 continue;
2398 }
2399 if (Type->isReferenceType()) {
2400 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002401 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2402 bool IsDecl =
2403 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2404 Diag(VD->getLocation(),
2405 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2406 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002407 continue;
2408 }
2409
2410 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2411 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002412 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002413 // class type.
2414 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002415 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2416 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2417 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002418 // FIXME This code must be replaced by actual constructing/destructing of
2419 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002420 if (RD) {
2421 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2422 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002423 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002424 if (!CD ||
2425 CheckConstructorAccess(ELoc, CD,
2426 InitializedEntity::InitializeTemporary(Type),
2427 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002428 CD->isDeleted()) {
2429 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002430 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002431 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2432 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002433 Diag(VD->getLocation(),
2434 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2435 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002436 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2437 continue;
2438 }
2439 MarkFunctionReferenced(ELoc, CD);
2440 DiagnoseUseOfDecl(CD, ELoc);
2441
2442 CXXDestructorDecl *DD = RD->getDestructor();
2443 if (DD) {
2444 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2445 DD->isDeleted()) {
2446 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002447 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002448 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2449 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002450 Diag(VD->getLocation(),
2451 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2452 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002453 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2454 continue;
2455 }
2456 MarkFunctionReferenced(ELoc, DD);
2457 DiagnoseUseOfDecl(DD, ELoc);
2458 }
2459 }
2460
2461 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2462 // variable and it was checked already.
2463 if (StartLoc.isValid() && EndLoc.isValid()) {
2464 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2465 Type = Type.getNonReferenceType().getCanonicalType();
2466 bool IsConstant = Type.isConstant(Context);
2467 Type = Context.getBaseElementType(Type);
2468 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2469 // A list item that specifies a given variable may not appear in more
2470 // than one clause on the same directive, except that a variable may be
2471 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002472 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002473 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002474 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002475 << getOpenMPClauseName(DVar.CKind)
2476 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002477 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002478 continue;
2479 }
2480
2481 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2482 // in a Construct]
2483 // Variables with the predetermined data-sharing attributes may not be
2484 // listed in data-sharing attributes clauses, except for the cases
2485 // listed below. For these exceptions only, listing a predetermined
2486 // variable in a data-sharing attribute clause is allowed and overrides
2487 // the variable's predetermined data-sharing attributes.
2488 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2489 // in a Construct, C/C++, p.2]
2490 // Variables with const-qualified type having no mutable member may be
2491 // listed in a firstprivate clause, even if they are static data members.
2492 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2493 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2494 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002495 << getOpenMPClauseName(DVar.CKind)
2496 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002497 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002498 continue;
2499 }
2500
Alexey Bataevf29276e2014-06-18 04:14:57 +00002501 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002502 // OpenMP [2.9.3.4, Restrictions, p.2]
2503 // A list item that is private within a parallel region must not appear
2504 // in a firstprivate clause on a worksharing construct if any of the
2505 // worksharing regions arising from the worksharing construct ever bind
2506 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002507 if (isOpenMPWorksharingDirective(CurrDir) &&
2508 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002509 DVar = DSAStack->getImplicitDSA(VD);
2510 if (DVar.CKind != OMPC_shared) {
2511 Diag(ELoc, diag::err_omp_required_access)
2512 << getOpenMPClauseName(OMPC_firstprivate)
2513 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002514 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002515 continue;
2516 }
2517 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002518 // OpenMP [2.9.3.4, Restrictions, p.3]
2519 // A list item that appears in a reduction clause of a parallel construct
2520 // must not appear in a firstprivate clause on a worksharing or task
2521 // construct if any of the worksharing or task regions arising from the
2522 // worksharing or task construct ever bind to any of the parallel regions
2523 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002524 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002525 // OpenMP [2.9.3.4, Restrictions, p.4]
2526 // A list item that appears in a reduction clause in worksharing
2527 // construct must not appear in a firstprivate clause in a task construct
2528 // encountered during execution of any of the worksharing regions arising
2529 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002530 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002531 }
2532
2533 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2534 Vars.push_back(DE);
2535 }
2536
Alexey Bataeved09d242014-05-28 05:53:51 +00002537 if (Vars.empty())
2538 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002539
2540 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2541 Vars);
2542}
2543
Alexander Musman1bb328c2014-06-04 13:06:39 +00002544OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2545 SourceLocation StartLoc,
2546 SourceLocation LParenLoc,
2547 SourceLocation EndLoc) {
2548 SmallVector<Expr *, 8> Vars;
2549 for (auto &RefExpr : VarList) {
2550 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2551 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2552 // It will be analyzed later.
2553 Vars.push_back(RefExpr);
2554 continue;
2555 }
2556
2557 SourceLocation ELoc = RefExpr->getExprLoc();
2558 // OpenMP [2.1, C/C++]
2559 // A list item is a variable name.
2560 // OpenMP [2.14.3.5, Restrictions, p.1]
2561 // A variable that is part of another variable (as an array or structure
2562 // element) cannot appear in a lastprivate clause.
2563 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2564 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2565 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2566 continue;
2567 }
2568 Decl *D = DE->getDecl();
2569 VarDecl *VD = cast<VarDecl>(D);
2570
2571 QualType Type = VD->getType();
2572 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2573 // It will be analyzed later.
2574 Vars.push_back(DE);
2575 continue;
2576 }
2577
2578 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2579 // A variable that appears in a lastprivate clause must not have an
2580 // incomplete type or a reference type.
2581 if (RequireCompleteType(ELoc, Type,
2582 diag::err_omp_lastprivate_incomplete_type)) {
2583 continue;
2584 }
2585 if (Type->isReferenceType()) {
2586 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2587 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2588 bool IsDecl =
2589 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2590 Diag(VD->getLocation(),
2591 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2592 << VD;
2593 continue;
2594 }
2595
2596 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2597 // in a Construct]
2598 // Variables with the predetermined data-sharing attributes may not be
2599 // listed in data-sharing attributes clauses, except for the cases
2600 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002601 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2602 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2603 DVar.CKind != OMPC_firstprivate &&
2604 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2605 Diag(ELoc, diag::err_omp_wrong_dsa)
2606 << getOpenMPClauseName(DVar.CKind)
2607 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002608 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002609 continue;
2610 }
2611
Alexey Bataevf29276e2014-06-18 04:14:57 +00002612 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2613 // OpenMP [2.14.3.5, Restrictions, p.2]
2614 // A list item that is private within a parallel region, or that appears in
2615 // the reduction clause of a parallel construct, must not appear in a
2616 // lastprivate clause on a worksharing construct if any of the corresponding
2617 // worksharing regions ever binds to any of the corresponding parallel
2618 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002619 if (isOpenMPWorksharingDirective(CurrDir) &&
2620 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002621 DVar = DSAStack->getImplicitDSA(VD);
2622 if (DVar.CKind != OMPC_shared) {
2623 Diag(ELoc, diag::err_omp_required_access)
2624 << getOpenMPClauseName(OMPC_lastprivate)
2625 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002626 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002627 continue;
2628 }
2629 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002630 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002631 // A variable of class type (or array thereof) that appears in a
2632 // lastprivate clause requires an accessible, unambiguous default
2633 // constructor for the class type, unless the list item is also specified
2634 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002635 // A variable of class type (or array thereof) that appears in a
2636 // lastprivate clause requires an accessible, unambiguous copy assignment
2637 // operator for the class type.
2638 while (Type.getNonReferenceType()->isArrayType())
2639 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2640 ->getElementType();
2641 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2642 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2643 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002644 // FIXME This code must be replaced by actual copying and destructing of the
2645 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002646 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002647 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2648 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002649 if (MD) {
2650 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2651 MD->isDeleted()) {
2652 Diag(ELoc, diag::err_omp_required_method)
2653 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2654 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2655 VarDecl::DeclarationOnly;
2656 Diag(VD->getLocation(),
2657 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2658 << VD;
2659 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2660 continue;
2661 }
2662 MarkFunctionReferenced(ELoc, MD);
2663 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002664 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002665
2666 CXXDestructorDecl *DD = RD->getDestructor();
2667 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002668 PartialDiagnostic PD =
2669 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002670 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2671 DD->isDeleted()) {
2672 Diag(ELoc, diag::err_omp_required_method)
2673 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2674 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2675 VarDecl::DeclarationOnly;
2676 Diag(VD->getLocation(),
2677 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2678 << VD;
2679 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2680 continue;
2681 }
2682 MarkFunctionReferenced(ELoc, DD);
2683 DiagnoseUseOfDecl(DD, ELoc);
2684 }
2685 }
2686
Alexey Bataevf29276e2014-06-18 04:14:57 +00002687 if (DVar.CKind != OMPC_firstprivate)
2688 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002689 Vars.push_back(DE);
2690 }
2691
2692 if (Vars.empty())
2693 return nullptr;
2694
2695 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2696 Vars);
2697}
2698
Alexey Bataev758e55e2013-09-06 18:03:48 +00002699OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2700 SourceLocation StartLoc,
2701 SourceLocation LParenLoc,
2702 SourceLocation EndLoc) {
2703 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002704 for (auto &RefExpr : VarList) {
2705 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2706 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002707 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002708 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002709 continue;
2710 }
2711
Alexey Bataeved09d242014-05-28 05:53:51 +00002712 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002713 // OpenMP [2.1, C/C++]
2714 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002715 // OpenMP [2.14.3.2, Restrictions, p.1]
2716 // A variable that is part of another variable (as an array or structure
2717 // element) cannot appear in a shared unless it is a static data member
2718 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002719 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002720 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002721 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002722 continue;
2723 }
2724 Decl *D = DE->getDecl();
2725 VarDecl *VD = cast<VarDecl>(D);
2726
2727 QualType Type = VD->getType();
2728 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2729 // It will be analyzed later.
2730 Vars.push_back(DE);
2731 continue;
2732 }
2733
2734 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2735 // in a Construct]
2736 // Variables with the predetermined data-sharing attributes may not be
2737 // listed in data-sharing attributes clauses, except for the cases
2738 // listed below. For these exceptions only, listing a predetermined
2739 // variable in a data-sharing attribute clause is allowed and overrides
2740 // the variable's predetermined data-sharing attributes.
2741 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002742 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2743 DVar.RefExpr) {
2744 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2745 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002746 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002747 continue;
2748 }
2749
2750 DSAStack->addDSA(VD, DE, OMPC_shared);
2751 Vars.push_back(DE);
2752 }
2753
Alexey Bataeved09d242014-05-28 05:53:51 +00002754 if (Vars.empty())
2755 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002756
2757 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2758}
2759
Alexey Bataevc5e02582014-06-16 07:08:35 +00002760namespace {
2761class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2762 DSAStackTy *Stack;
2763
2764public:
2765 bool VisitDeclRefExpr(DeclRefExpr *E) {
2766 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2767 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2768 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2769 return false;
2770 if (DVar.CKind != OMPC_unknown)
2771 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002772 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002773 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002774 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002775 return true;
2776 return false;
2777 }
2778 return false;
2779 }
2780 bool VisitStmt(Stmt *S) {
2781 for (auto Child : S->children()) {
2782 if (Child && Visit(Child))
2783 return true;
2784 }
2785 return false;
2786 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002787 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002788};
Alexey Bataev23b69422014-06-18 07:08:49 +00002789} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002790
2791OMPClause *Sema::ActOnOpenMPReductionClause(
2792 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2793 SourceLocation ColonLoc, SourceLocation EndLoc,
2794 CXXScopeSpec &ReductionIdScopeSpec,
2795 const DeclarationNameInfo &ReductionId) {
2796 // TODO: Allow scope specification search when 'declare reduction' is
2797 // supported.
2798 assert(ReductionIdScopeSpec.isEmpty() &&
2799 "No support for scoped reduction identifiers yet.");
2800
2801 auto DN = ReductionId.getName();
2802 auto OOK = DN.getCXXOverloadedOperator();
2803 BinaryOperatorKind BOK = BO_Comma;
2804
2805 // OpenMP [2.14.3.6, reduction clause]
2806 // C
2807 // reduction-identifier is either an identifier or one of the following
2808 // operators: +, -, *, &, |, ^, && and ||
2809 // C++
2810 // reduction-identifier is either an id-expression or one of the following
2811 // operators: +, -, *, &, |, ^, && and ||
2812 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2813 switch (OOK) {
2814 case OO_Plus:
2815 case OO_Minus:
2816 BOK = BO_AddAssign;
2817 break;
2818 case OO_Star:
2819 BOK = BO_MulAssign;
2820 break;
2821 case OO_Amp:
2822 BOK = BO_AndAssign;
2823 break;
2824 case OO_Pipe:
2825 BOK = BO_OrAssign;
2826 break;
2827 case OO_Caret:
2828 BOK = BO_XorAssign;
2829 break;
2830 case OO_AmpAmp:
2831 BOK = BO_LAnd;
2832 break;
2833 case OO_PipePipe:
2834 BOK = BO_LOr;
2835 break;
2836 default:
2837 if (auto II = DN.getAsIdentifierInfo()) {
2838 if (II->isStr("max"))
2839 BOK = BO_GT;
2840 else if (II->isStr("min"))
2841 BOK = BO_LT;
2842 }
2843 break;
2844 }
2845 SourceRange ReductionIdRange;
2846 if (ReductionIdScopeSpec.isValid()) {
2847 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2848 }
2849 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2850 if (BOK == BO_Comma) {
2851 // Not allowed reduction identifier is found.
2852 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2853 << ReductionIdRange;
2854 return nullptr;
2855 }
2856
2857 SmallVector<Expr *, 8> Vars;
2858 for (auto RefExpr : VarList) {
2859 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2860 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2861 // It will be analyzed later.
2862 Vars.push_back(RefExpr);
2863 continue;
2864 }
2865
2866 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2867 RefExpr->isInstantiationDependent() ||
2868 RefExpr->containsUnexpandedParameterPack()) {
2869 // It will be analyzed later.
2870 Vars.push_back(RefExpr);
2871 continue;
2872 }
2873
2874 auto ELoc = RefExpr->getExprLoc();
2875 auto ERange = RefExpr->getSourceRange();
2876 // OpenMP [2.1, C/C++]
2877 // A list item is a variable or array section, subject to the restrictions
2878 // specified in Section 2.4 on page 42 and in each of the sections
2879 // describing clauses and directives for which a list appears.
2880 // OpenMP [2.14.3.3, Restrictions, p.1]
2881 // A variable that is part of another variable (as an array or
2882 // structure element) cannot appear in a private clause.
2883 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2884 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2885 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2886 continue;
2887 }
2888 auto D = DE->getDecl();
2889 auto VD = cast<VarDecl>(D);
2890 auto Type = VD->getType();
2891 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2892 // A variable that appears in a private clause must not have an incomplete
2893 // type or a reference type.
2894 if (RequireCompleteType(ELoc, Type,
2895 diag::err_omp_reduction_incomplete_type))
2896 continue;
2897 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2898 // Arrays may not appear in a reduction clause.
2899 if (Type.getNonReferenceType()->isArrayType()) {
2900 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2901 bool IsDecl =
2902 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2903 Diag(VD->getLocation(),
2904 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2905 << VD;
2906 continue;
2907 }
2908 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2909 // A list item that appears in a reduction clause must not be
2910 // const-qualified.
2911 if (Type.getNonReferenceType().isConstant(Context)) {
2912 Diag(ELoc, diag::err_omp_const_variable)
2913 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2914 bool IsDecl =
2915 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2916 Diag(VD->getLocation(),
2917 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2918 << VD;
2919 continue;
2920 }
2921 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2922 // If a list-item is a reference type then it must bind to the same object
2923 // for all threads of the team.
2924 VarDecl *VDDef = VD->getDefinition();
2925 if (Type->isReferenceType() && VDDef) {
2926 DSARefChecker Check(DSAStack);
2927 if (Check.Visit(VDDef->getInit())) {
2928 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2929 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2930 continue;
2931 }
2932 }
2933 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2934 // The type of a list item that appears in a reduction clause must be valid
2935 // for the reduction-identifier. For a max or min reduction in C, the type
2936 // of the list item must be an allowed arithmetic data type: char, int,
2937 // float, double, or _Bool, possibly modified with long, short, signed, or
2938 // unsigned. For a max or min reduction in C++, the type of the list item
2939 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2940 // double, or bool, possibly modified with long, short, signed, or unsigned.
2941 if ((BOK == BO_GT || BOK == BO_LT) &&
2942 !(Type->isScalarType() ||
2943 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2944 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2945 << getLangOpts().CPlusPlus;
2946 bool IsDecl =
2947 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2948 Diag(VD->getLocation(),
2949 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2950 << VD;
2951 continue;
2952 }
2953 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2954 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2955 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2956 bool IsDecl =
2957 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2958 Diag(VD->getLocation(),
2959 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2960 << VD;
2961 continue;
2962 }
2963 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2964 getDiagnostics().setSuppressAllDiagnostics(true);
2965 ExprResult ReductionOp =
2966 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2967 RefExpr, RefExpr);
2968 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2969 if (ReductionOp.isInvalid()) {
2970 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002971 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002972 bool IsDecl =
2973 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2974 Diag(VD->getLocation(),
2975 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2976 << VD;
2977 continue;
2978 }
2979
2980 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2981 // in a Construct]
2982 // Variables with the predetermined data-sharing attributes may not be
2983 // listed in data-sharing attributes clauses, except for the cases
2984 // listed below. For these exceptions only, listing a predetermined
2985 // variable in a data-sharing attribute clause is allowed and overrides
2986 // the variable's predetermined data-sharing attributes.
2987 // OpenMP [2.14.3.6, Restrictions, p.3]
2988 // Any number of reduction clauses can be specified on the directive,
2989 // but a list item can appear only once in the reduction clauses for that
2990 // directive.
2991 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2992 if (DVar.CKind == OMPC_reduction) {
2993 Diag(ELoc, diag::err_omp_once_referenced)
2994 << getOpenMPClauseName(OMPC_reduction);
2995 if (DVar.RefExpr) {
2996 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2997 }
2998 } else if (DVar.CKind != OMPC_unknown) {
2999 Diag(ELoc, diag::err_omp_wrong_dsa)
3000 << getOpenMPClauseName(DVar.CKind)
3001 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003002 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003003 continue;
3004 }
3005
3006 // OpenMP [2.14.3.6, Restrictions, p.1]
3007 // A list item that appears in a reduction clause of a worksharing
3008 // construct must be shared in the parallel regions to which any of the
3009 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003010 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003011 if (isOpenMPWorksharingDirective(CurrDir) &&
3012 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003013 DVar = DSAStack->getImplicitDSA(VD);
3014 if (DVar.CKind != OMPC_shared) {
3015 Diag(ELoc, diag::err_omp_required_access)
3016 << getOpenMPClauseName(OMPC_reduction)
3017 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003018 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003019 continue;
3020 }
3021 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003022
3023 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3024 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3025 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003026 // FIXME This code must be replaced by actual constructing/destructing of
3027 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003028 if (RD) {
3029 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3030 PartialDiagnostic PD =
3031 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003032 if (!CD ||
3033 CheckConstructorAccess(ELoc, CD,
3034 InitializedEntity::InitializeTemporary(Type),
3035 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003036 CD->isDeleted()) {
3037 Diag(ELoc, diag::err_omp_required_method)
3038 << getOpenMPClauseName(OMPC_reduction) << 0;
3039 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3040 VarDecl::DeclarationOnly;
3041 Diag(VD->getLocation(),
3042 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3043 << VD;
3044 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3045 continue;
3046 }
3047 MarkFunctionReferenced(ELoc, CD);
3048 DiagnoseUseOfDecl(CD, ELoc);
3049
3050 CXXDestructorDecl *DD = RD->getDestructor();
3051 if (DD) {
3052 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3053 DD->isDeleted()) {
3054 Diag(ELoc, diag::err_omp_required_method)
3055 << getOpenMPClauseName(OMPC_reduction) << 4;
3056 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3057 VarDecl::DeclarationOnly;
3058 Diag(VD->getLocation(),
3059 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3060 << VD;
3061 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3062 continue;
3063 }
3064 MarkFunctionReferenced(ELoc, DD);
3065 DiagnoseUseOfDecl(DD, ELoc);
3066 }
3067 }
3068
3069 DSAStack->addDSA(VD, DE, OMPC_reduction);
3070 Vars.push_back(DE);
3071 }
3072
3073 if (Vars.empty())
3074 return nullptr;
3075
3076 return OMPReductionClause::Create(
3077 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3078 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3079}
3080
Alexander Musman8dba6642014-04-22 13:09:42 +00003081OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3082 SourceLocation StartLoc,
3083 SourceLocation LParenLoc,
3084 SourceLocation ColonLoc,
3085 SourceLocation EndLoc) {
3086 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003087 for (auto &RefExpr : VarList) {
3088 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3089 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003090 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003091 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003092 continue;
3093 }
3094
3095 // OpenMP [2.14.3.7, linear clause]
3096 // A list item that appears in a linear clause is subject to the private
3097 // clause semantics described in Section 2.14.3.3 on page 159 except as
3098 // noted. In addition, the value of the new list item on each iteration
3099 // of the associated loop(s) corresponds to the value of the original
3100 // list item before entering the construct plus the logical number of
3101 // the iteration times linear-step.
3102
Alexey Bataeved09d242014-05-28 05:53:51 +00003103 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003104 // OpenMP [2.1, C/C++]
3105 // A list item is a variable name.
3106 // OpenMP [2.14.3.3, Restrictions, p.1]
3107 // A variable that is part of another variable (as an array or
3108 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003109 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003110 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003111 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003112 continue;
3113 }
3114
3115 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3116
3117 // OpenMP [2.14.3.7, linear clause]
3118 // A list-item cannot appear in more than one linear clause.
3119 // A list-item that appears in a linear clause cannot appear in any
3120 // other data-sharing attribute clause.
3121 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
3122 if (DVar.RefExpr) {
3123 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3124 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003125 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003126 continue;
3127 }
3128
3129 QualType QType = VD->getType();
3130 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3131 // It will be analyzed later.
3132 Vars.push_back(DE);
3133 continue;
3134 }
3135
3136 // A variable must not have an incomplete type or a reference type.
3137 if (RequireCompleteType(ELoc, QType,
3138 diag::err_omp_linear_incomplete_type)) {
3139 continue;
3140 }
3141 if (QType->isReferenceType()) {
3142 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3143 << getOpenMPClauseName(OMPC_linear) << QType;
3144 bool IsDecl =
3145 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3146 Diag(VD->getLocation(),
3147 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3148 << VD;
3149 continue;
3150 }
3151
3152 // A list item must not be const-qualified.
3153 if (QType.isConstant(Context)) {
3154 Diag(ELoc, diag::err_omp_const_variable)
3155 << getOpenMPClauseName(OMPC_linear);
3156 bool IsDecl =
3157 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3158 Diag(VD->getLocation(),
3159 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3160 << VD;
3161 continue;
3162 }
3163
3164 // A list item must be of integral or pointer type.
3165 QType = QType.getUnqualifiedType().getCanonicalType();
3166 const Type *Ty = QType.getTypePtrOrNull();
3167 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3168 !Ty->isPointerType())) {
3169 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3170 bool IsDecl =
3171 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3172 Diag(VD->getLocation(),
3173 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3174 << VD;
3175 continue;
3176 }
3177
3178 DSAStack->addDSA(VD, DE, OMPC_linear);
3179 Vars.push_back(DE);
3180 }
3181
3182 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003183 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003184
3185 Expr *StepExpr = Step;
3186 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3187 !Step->isInstantiationDependent() &&
3188 !Step->containsUnexpandedParameterPack()) {
3189 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003190 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003191 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003192 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003193 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003194
3195 // Warn about zero linear step (it would be probably better specified as
3196 // making corresponding variables 'const').
3197 llvm::APSInt Result;
3198 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3199 !Result.isNegative() && !Result.isStrictlyPositive())
3200 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3201 << (Vars.size() > 1);
3202 }
3203
3204 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3205 Vars, StepExpr);
3206}
3207
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003208OMPClause *Sema::ActOnOpenMPAlignedClause(
3209 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3210 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3211
3212 SmallVector<Expr *, 8> Vars;
3213 for (auto &RefExpr : VarList) {
3214 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3215 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3216 // It will be analyzed later.
3217 Vars.push_back(RefExpr);
3218 continue;
3219 }
3220
3221 SourceLocation ELoc = RefExpr->getExprLoc();
3222 // OpenMP [2.1, C/C++]
3223 // A list item is a variable name.
3224 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3225 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3226 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3227 continue;
3228 }
3229
3230 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3231
3232 // OpenMP [2.8.1, simd construct, Restrictions]
3233 // The type of list items appearing in the aligned clause must be
3234 // array, pointer, reference to array, or reference to pointer.
3235 QualType QType = DE->getType()
3236 .getNonReferenceType()
3237 .getUnqualifiedType()
3238 .getCanonicalType();
3239 const Type *Ty = QType.getTypePtrOrNull();
3240 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3241 !Ty->isPointerType())) {
3242 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3243 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3244 bool IsDecl =
3245 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3246 Diag(VD->getLocation(),
3247 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3248 << VD;
3249 continue;
3250 }
3251
3252 // OpenMP [2.8.1, simd construct, Restrictions]
3253 // A list-item cannot appear in more than one aligned clause.
3254 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3255 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3256 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3257 << getOpenMPClauseName(OMPC_aligned);
3258 continue;
3259 }
3260
3261 Vars.push_back(DE);
3262 }
3263
3264 // OpenMP [2.8.1, simd construct, Description]
3265 // The parameter of the aligned clause, alignment, must be a constant
3266 // positive integer expression.
3267 // If no optional parameter is specified, implementation-defined default
3268 // alignments for SIMD instructions on the target platforms are assumed.
3269 if (Alignment != nullptr) {
3270 ExprResult AlignResult =
3271 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3272 if (AlignResult.isInvalid())
3273 return nullptr;
3274 Alignment = AlignResult.get();
3275 }
3276 if (Vars.empty())
3277 return nullptr;
3278
3279 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3280 EndLoc, Vars, Alignment);
3281}
3282
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003283OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3284 SourceLocation StartLoc,
3285 SourceLocation LParenLoc,
3286 SourceLocation EndLoc) {
3287 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003288 for (auto &RefExpr : VarList) {
3289 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3290 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003291 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003292 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003293 continue;
3294 }
3295
Alexey Bataeved09d242014-05-28 05:53:51 +00003296 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003297 // OpenMP [2.1, C/C++]
3298 // A list item is a variable name.
3299 // OpenMP [2.14.4.1, Restrictions, p.1]
3300 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003301 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003302 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003303 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003304 continue;
3305 }
3306
3307 Decl *D = DE->getDecl();
3308 VarDecl *VD = cast<VarDecl>(D);
3309
3310 QualType Type = VD->getType();
3311 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3312 // It will be analyzed later.
3313 Vars.push_back(DE);
3314 continue;
3315 }
3316
3317 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3318 // A list item that appears in a copyin clause must be threadprivate.
3319 if (!DSAStack->isThreadPrivate(VD)) {
3320 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003321 << getOpenMPClauseName(OMPC_copyin)
3322 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003323 continue;
3324 }
3325
3326 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3327 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003328 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003329 // operator for the class type.
3330 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003331 CXXRecordDecl *RD =
3332 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003333 // FIXME This code must be replaced by actual assignment of the
3334 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003335 if (RD) {
3336 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3337 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003338 if (MD) {
3339 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3340 MD->isDeleted()) {
3341 Diag(ELoc, diag::err_omp_required_method)
3342 << getOpenMPClauseName(OMPC_copyin) << 2;
3343 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3344 VarDecl::DeclarationOnly;
3345 Diag(VD->getLocation(),
3346 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3347 << VD;
3348 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3349 continue;
3350 }
3351 MarkFunctionReferenced(ELoc, MD);
3352 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003353 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003354 }
3355
3356 DSAStack->addDSA(VD, DE, OMPC_copyin);
3357 Vars.push_back(DE);
3358 }
3359
Alexey Bataeved09d242014-05-28 05:53:51 +00003360 if (Vars.empty())
3361 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003362
3363 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3364}
3365
Alexey Bataevbae9a792014-06-27 10:37:06 +00003366OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
3367 SourceLocation StartLoc,
3368 SourceLocation LParenLoc,
3369 SourceLocation EndLoc) {
3370 SmallVector<Expr *, 8> Vars;
3371 for (auto &RefExpr : VarList) {
3372 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
3373 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3374 // It will be analyzed later.
3375 Vars.push_back(RefExpr);
3376 continue;
3377 }
3378
3379 SourceLocation ELoc = RefExpr->getExprLoc();
3380 // OpenMP [2.1, C/C++]
3381 // A list item is a variable name.
3382 // OpenMP [2.14.4.1, Restrictions, p.1]
3383 // A list item that appears in a copyin clause must be threadprivate.
3384 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3385 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3386 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3387 continue;
3388 }
3389
3390 Decl *D = DE->getDecl();
3391 VarDecl *VD = cast<VarDecl>(D);
3392
3393 QualType Type = VD->getType();
3394 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3395 // It will be analyzed later.
3396 Vars.push_back(DE);
3397 continue;
3398 }
3399
3400 // OpenMP [2.14.4.2, Restrictions, p.2]
3401 // A list item that appears in a copyprivate clause may not appear in a
3402 // private or firstprivate clause on the single construct.
3403 if (!DSAStack->isThreadPrivate(VD)) {
3404 auto DVar = DSAStack->getTopDSA(VD);
3405 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
3406 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
3407 Diag(ELoc, diag::err_omp_wrong_dsa)
3408 << getOpenMPClauseName(DVar.CKind)
3409 << getOpenMPClauseName(OMPC_copyprivate);
3410 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3411 continue;
3412 }
3413
3414 // OpenMP [2.11.4.2, Restrictions, p.1]
3415 // All list items that appear in a copyprivate clause must be either
3416 // threadprivate or private in the enclosing context.
3417 if (DVar.CKind == OMPC_unknown) {
3418 DVar = DSAStack->getImplicitDSA(VD);
3419 if (DVar.CKind == OMPC_shared) {
3420 Diag(ELoc, diag::err_omp_required_access)
3421 << getOpenMPClauseName(OMPC_copyprivate)
3422 << "threadprivate or private in the enclosing context";
3423 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3424 continue;
3425 }
3426 }
3427 }
3428
3429 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3430 // A variable of class type (or array thereof) that appears in a
3431 // copyin clause requires an accessible, unambiguous copy assignment
3432 // operator for the class type.
3433 Type = Context.getBaseElementType(Type);
3434 CXXRecordDecl *RD =
3435 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
3436 // FIXME This code must be replaced by actual assignment of the
3437 // threadprivate variable.
3438 if (RD) {
3439 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3440 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
3441 if (MD) {
3442 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3443 MD->isDeleted()) {
3444 Diag(ELoc, diag::err_omp_required_method)
3445 << getOpenMPClauseName(OMPC_copyprivate) << 2;
3446 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3447 VarDecl::DeclarationOnly;
3448 Diag(VD->getLocation(),
3449 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3450 << VD;
3451 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3452 continue;
3453 }
3454 MarkFunctionReferenced(ELoc, MD);
3455 DiagnoseUseOfDecl(MD, ELoc);
3456 }
3457 }
3458
3459 // No need to mark vars as copyprivate, they are already threadprivate or
3460 // implicitly private.
3461 Vars.push_back(DE);
3462 }
3463
3464 if (Vars.empty())
3465 return nullptr;
3466
3467 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3468}
3469
Alexey Bataev758e55e2013-09-06 18:03:48 +00003470#undef DSAStack