blob: 603dd56c4fe88b544cedf41bb57cabbf198a3848 [file] [log] [blame]
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataevc6400582013-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 Bataev6af701f2013-05-13 04:18:18 +000011/// clauses.
Alexey Bataevc6400582013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Stephen Hines6bcf27b2014-05-29 04:14:42 -070015#include "clang/AST/ASTContext.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000016#include "clang/AST/Decl.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070022#include "clang/Basic/OpenMPKinds.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000023#include "clang/Lex/Preprocessor.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000024#include "clang/Sema/Initialization.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000025#include "clang/Sema/Lookup.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070028#include "clang/Sema/SemaInternal.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000029using namespace clang;
30
Alexey Bataev0c018352013-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 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070038 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 Bataev0c018352013-09-06 18:03:48 +000041};
42
43/// \brief Stack for tracking declarations used in OpenMP directives and
44/// clauses and their data-sharing attributes.
45class DSAStackTy {
46public:
47 struct DSAVarData {
48 OpenMPDirectiveKind DKind;
49 OpenMPClauseKind CKind;
50 DeclRefExpr *RefExpr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070051 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr) {}
Alexey Bataev0c018352013-09-06 18:03:48 +000052 };
Stephen Hines6bcf27b2014-05-29 04:14:42 -070053
Alexey Bataev0c018352013-09-06 18:03:48 +000054private:
55 struct DSAInfo {
56 OpenMPClauseKind Attributes;
57 DeclRefExpr *RefExpr;
58 };
59 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
60
61 struct SharingMapTy {
62 DeclSAMapTy SharingMap;
63 DefaultDataSharingAttributes DefaultAttr;
64 OpenMPDirectiveKind Directive;
65 DeclarationNameInfo DirectiveName;
66 Scope *CurScope;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070067 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataev0c018352013-09-06 18:03:48 +000068 Scope *CurScope)
Stephen Hines6bcf27b2014-05-29 04:14:42 -070069 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
70 DirectiveName(std::move(Name)), CurScope(CurScope) {}
Alexey Bataev0c018352013-09-06 18:03:48 +000071 SharingMapTy()
Stephen Hines6bcf27b2014-05-29 04:14:42 -070072 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(OMPD_unknown),
73 DirectiveName(), CurScope(nullptr) {}
Alexey Bataev0c018352013-09-06 18:03:48 +000074 };
75
76 typedef SmallVector<SharingMapTy, 64> StackTy;
77
78 /// \brief Stack of used declaration and their data-sharing attributes.
79 StackTy Stack;
80 Sema &Actions;
81
82 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
83
84 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Stephen Hines651f13c2014-04-23 16:59:28 -070085
86 /// \brief Checks if the variable is a local for OpenMP region.
87 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Stephen Hines6bcf27b2014-05-29 04:14:42 -070088
Alexey Bataev0c018352013-09-06 18:03:48 +000089public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -070090 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) {}
Alexey Bataev0c018352013-09-06 18:03:48 +000091
92 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
93 Scope *CurScope) {
94 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
95 }
96
97 void pop() {
98 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
99 Stack.pop_back();
100 }
101
102 /// \brief Adds explicit data sharing attribute to the specified declaration.
103 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
104
Alexey Bataev0c018352013-09-06 18:03:48 +0000105 /// \brief Returns data sharing attributes from top of the stack for the
106 /// specified declaration.
107 DSAVarData getTopDSA(VarDecl *D);
108 /// \brief Returns data-sharing attributes for the specified declaration.
109 DSAVarData getImplicitDSA(VarDecl *D);
Alexey Bataevd195bc32013-10-01 05:32:34 +0000110 /// \brief Checks if the specified variables has \a CKind data-sharing
111 /// attribute in \a DKind directive.
112 DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
113 OpenMPDirectiveKind DKind = OMPD_unknown);
114
Alexey Bataev0c018352013-09-06 18:03:48 +0000115 /// \brief Returns currently analyzed directive.
116 OpenMPDirectiveKind getCurrentDirective() const {
117 return Stack.back().Directive;
118 }
119
120 /// \brief Set default data sharing attribute to none.
121 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
122 /// \brief Set default data sharing attribute to shared.
123 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
124
125 DefaultDataSharingAttributes getDefaultDSA() const {
126 return Stack.back().DefaultAttr;
127 }
128
Stephen Hines651f13c2014-04-23 16:59:28 -0700129 /// \brief Checks if the spewcified variable is threadprivate.
130 bool isThreadPrivate(VarDecl *D) {
131 DSAVarData DVar = getTopDSA(D);
132 return (DVar.CKind == OMPC_threadprivate || DVar.CKind == OMPC_copyin);
133 }
134
135 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev0c018352013-09-06 18:03:48 +0000136 Scope *getCurScope() { return Stack.back().CurScope; }
137};
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700138} // namespace
Alexey Bataev0c018352013-09-06 18:03:48 +0000139
140DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
141 VarDecl *D) {
142 DSAVarData DVar;
143 if (Iter == Stack.rend() - 1) {
144 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
145 // in a region but not in construct]
146 // File-scope or namespace-scope variables referenced in called routines
147 // in the region are shared unless they appear in a threadprivate
148 // directive.
149 // TODO
150 if (!D->isFunctionOrMethodVarDecl())
151 DVar.CKind = OMPC_shared;
152
Alexey Bataevd195bc32013-10-01 05:32:34 +0000153 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
154 // in a region but not in construct]
155 // Variables with static storage duration that are declared in called
156 // routines in the region are shared.
157 if (D->hasGlobalStorage())
158 DVar.CKind = OMPC_shared;
159
Alexey Bataev0c018352013-09-06 18:03:48 +0000160 return DVar;
161 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700162
Alexey Bataev0c018352013-09-06 18:03:48 +0000163 DVar.DKind = Iter->Directive;
Stephen Hines651f13c2014-04-23 16:59:28 -0700164 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
165 // in a Construct, C/C++, predetermined, p.1]
166 // Variables with automatic storage duration that are declared in a scope
167 // inside the construct are private.
168 if (DVar.DKind != OMPD_parallel) {
169 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700170 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700171 DVar.CKind = OMPC_private;
172 return DVar;
173 }
174 }
175
Alexey Bataev0c018352013-09-06 18:03:48 +0000176 // Explicitly specified attributes and local variables with predetermined
177 // attributes.
178 if (Iter->SharingMap.count(D)) {
179 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
180 DVar.CKind = Iter->SharingMap[D].Attributes;
181 return DVar;
182 }
183
184 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
185 // in a Construct, C/C++, implicitly determined, p.1]
186 // In a parallel or task construct, the data-sharing attributes of these
187 // variables are determined by the default clause, if present.
188 switch (Iter->DefaultAttr) {
189 case DSA_shared:
190 DVar.CKind = OMPC_shared;
191 return DVar;
192 case DSA_none:
193 return DVar;
194 case DSA_unspecified:
195 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
196 // in a Construct, implicitly determined, p.2]
197 // In a parallel construct, if no default clause is present, these
198 // variables are shared.
199 if (DVar.DKind == OMPD_parallel) {
200 DVar.CKind = OMPC_shared;
201 return DVar;
202 }
203
204 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
205 // in a Construct, implicitly determined, p.4]
206 // In a task construct, if no default clause is present, a variable that in
207 // the enclosing context is determined to be shared by all implicit tasks
208 // bound to the current team is shared.
209 // TODO
210 if (DVar.DKind == OMPD_task) {
211 DSAVarData DVarTemp;
Stephen Hines651f13c2014-04-23 16:59:28 -0700212 for (StackTy::reverse_iterator I = std::next(Iter),
213 EE = std::prev(Stack.rend());
Alexey Bataev0c018352013-09-06 18:03:48 +0000214 I != EE; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
216 // Referenced
Alexey Bataev0c018352013-09-06 18:03:48 +0000217 // in a Construct, implicitly determined, p.6]
218 // In a task construct, if no default clause is present, a variable
219 // whose data-sharing attribute is not determined by the rules above is
220 // firstprivate.
221 DVarTemp = getDSA(I, D);
222 if (DVarTemp.CKind != OMPC_shared) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700223 DVar.RefExpr = nullptr;
Alexey Bataev0c018352013-09-06 18:03:48 +0000224 DVar.DKind = OMPD_task;
Alexey Bataevd195bc32013-10-01 05:32:34 +0000225 DVar.CKind = OMPC_firstprivate;
Alexey Bataev0c018352013-09-06 18:03:48 +0000226 return DVar;
227 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700228 if (I->Directive == OMPD_parallel)
229 break;
Alexey Bataev0c018352013-09-06 18:03:48 +0000230 }
231 DVar.DKind = OMPD_task;
Alexey Bataev0c018352013-09-06 18:03:48 +0000232 DVar.CKind =
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700233 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev0c018352013-09-06 18:03:48 +0000234 return DVar;
235 }
236 }
237 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
238 // in a Construct, implicitly determined, p.3]
239 // For constructs other than task, if no default clause is present, these
240 // variables inherit their data-sharing attributes from the enclosing
241 // context.
Stephen Hines651f13c2014-04-23 16:59:28 -0700242 return getDSA(std::next(Iter), D);
Alexey Bataev0c018352013-09-06 18:03:48 +0000243}
244
245void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
246 if (A == OMPC_threadprivate) {
247 Stack[0].SharingMap[D].Attributes = A;
248 Stack[0].SharingMap[D].RefExpr = E;
249 } else {
250 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
251 Stack.back().SharingMap[D].Attributes = A;
252 Stack.back().SharingMap[D].RefExpr = E;
253 }
254}
255
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700256bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700257 if (Stack.size() > 2) {
258 reverse_iterator I = Iter, E = Stack.rend() - 1;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700259 Scope *TopScope = nullptr;
260 while (I != E && I->Directive != OMPD_parallel) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700261 ++I;
262 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700263 if (I == E)
264 return false;
265 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700266 Scope *CurScope = getCurScope();
267 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev0c018352013-09-06 18:03:48 +0000268 CurScope = CurScope->getParent();
Stephen Hines651f13c2014-04-23 16:59:28 -0700269 }
270 return CurScope != TopScope;
Alexey Bataev0c018352013-09-06 18:03:48 +0000271 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700272 return false;
Alexey Bataev0c018352013-09-06 18:03:48 +0000273}
274
275DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
276 DSAVarData DVar;
277
278 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
279 // in a Construct, C/C++, predetermined, p.1]
280 // Variables appearing in threadprivate directives are threadprivate.
281 if (D->getTLSKind() != VarDecl::TLS_None) {
282 DVar.CKind = OMPC_threadprivate;
283 return DVar;
284 }
285 if (Stack[0].SharingMap.count(D)) {
286 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
287 DVar.CKind = OMPC_threadprivate;
288 return DVar;
289 }
290
291 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
292 // in a Construct, C/C++, predetermined, p.1]
293 // Variables with automatic storage duration that are declared in a scope
294 // inside the construct are private.
Stephen Hines651f13c2014-04-23 16:59:28 -0700295 OpenMPDirectiveKind Kind = getCurrentDirective();
296 if (Kind != OMPD_parallel) {
297 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700298 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700299 DVar.CKind = OMPC_private;
300 return DVar;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700301 }
Alexey Bataev0c018352013-09-06 18:03:48 +0000302 }
303
304 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
305 // in a Construct, C/C++, predetermined, p.4]
306 // Static data memebers are shared.
307 if (D->isStaticDataMember()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700308 // Variables with const-qualified type having no mutable member may be
309 // listed
Alexey Bataevd195bc32013-10-01 05:32:34 +0000310 // in a firstprivate clause, even if they are static data members.
311 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
312 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
313 return DVar;
314
Alexey Bataev0c018352013-09-06 18:03:48 +0000315 DVar.CKind = OMPC_shared;
316 return DVar;
317 }
318
319 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
320 bool IsConstant = Type.isConstant(Actions.getASTContext());
321 while (Type->isArrayType()) {
322 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
323 Type = ElemType.getNonReferenceType().getCanonicalType();
324 }
325 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
326 // in a Construct, C/C++, predetermined, p.6]
327 // Variables with const qualified type having no mutable member are
328 // shared.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700329 CXXRecordDecl *RD =
330 Actions.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev0c018352013-09-06 18:03:48 +0000331 if (IsConstant &&
332 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
333 // Variables with const-qualified type having no mutable member may be
334 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd195bc32013-10-01 05:32:34 +0000335 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
336 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
337 return DVar;
338
Alexey Bataev0c018352013-09-06 18:03:48 +0000339 DVar.CKind = OMPC_shared;
340 return DVar;
341 }
342
343 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
344 // in a Construct, C/C++, predetermined, p.7]
345 // Variables with static storage duration that are declared in a scope
346 // inside the construct are shared.
Stephen Hines651f13c2014-04-23 16:59:28 -0700347 if (D->isStaticLocal()) {
Alexey Bataev0c018352013-09-06 18:03:48 +0000348 DVar.CKind = OMPC_shared;
349 return DVar;
350 }
351
352 // Explicitly specified attributes and local variables with predetermined
353 // attributes.
354 if (Stack.back().SharingMap.count(D)) {
355 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
356 DVar.CKind = Stack.back().SharingMap[D].Attributes;
357 }
358
359 return DVar;
360}
361
362DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700363 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev0c018352013-09-06 18:03:48 +0000364}
365
Alexey Bataevd195bc32013-10-01 05:32:34 +0000366DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
367 OpenMPDirectiveKind DKind) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700368 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
369 E = std::prev(Stack.rend());
Alexey Bataevd195bc32013-10-01 05:32:34 +0000370 I != E; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700371 if (DKind != OMPD_unknown && DKind != I->Directive)
372 continue;
Alexey Bataevd195bc32013-10-01 05:32:34 +0000373 DSAVarData DVar = getDSA(I, D);
374 if (DVar.CKind == CKind)
375 return DVar;
376 }
377 return DSAVarData();
378}
379
Alexey Bataev0c018352013-09-06 18:03:48 +0000380void Sema::InitDataSharingAttributesStack() {
381 VarDataSharingAttributesStack = new DSAStackTy(*this);
382}
383
384#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
385
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700386void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev0c018352013-09-06 18:03:48 +0000387
388void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
389 const DeclarationNameInfo &DirName,
390 Scope *CurScope) {
391 DSAStack->push(DKind, DirName, CurScope);
392 PushExpressionEvaluationContext(PotentiallyEvaluated);
393}
394
395void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
396 DSAStack->pop();
397 DiscardCleanupsInEvaluationContext();
398 PopExpressionEvaluationContext();
399}
400
Alexey Bataevc6400582013-03-22 06:34:35 +0000401namespace {
402
Alexey Bataev6af701f2013-05-13 04:18:18 +0000403class VarDeclFilterCCC : public CorrectionCandidateCallback {
404private:
405 Sema &Actions;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700406
Alexey Bataev6af701f2013-05-13 04:18:18 +0000407public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700408 VarDeclFilterCCC(Sema &S) : Actions(S) {}
Stephen Hines651f13c2014-04-23 16:59:28 -0700409 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6af701f2013-05-13 04:18:18 +0000410 NamedDecl *ND = Candidate.getCorrectionDecl();
411 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
412 return VD->hasGlobalStorage() &&
413 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
414 Actions.getCurScope());
Alexey Bataevc6400582013-03-22 06:34:35 +0000415 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000416 return false;
Alexey Bataevc6400582013-03-22 06:34:35 +0000417 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000418};
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700419} // namespace
Alexey Bataev6af701f2013-05-13 04:18:18 +0000420
421ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
422 CXXScopeSpec &ScopeSpec,
423 const DeclarationNameInfo &Id) {
424 LookupResult Lookup(*this, Id, LookupOrdinaryName);
425 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
426
427 if (Lookup.isAmbiguous())
428 return ExprError();
429
430 VarDecl *VD;
431 if (!Lookup.isSingleResult()) {
432 VarDeclFilterCCC Validator(*this);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700433 if (TypoCorrection Corrected =
434 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
435 CTK_ErrorRecovery)) {
Richard Smith2d670972013-08-17 00:46:16 +0000436 diagnoseTypo(Corrected,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700437 PDiag(Lookup.empty()
438 ? diag::err_undeclared_var_use_suggest
439 : diag::err_omp_expected_var_arg_suggest)
440 << Id.getName());
Richard Smith2d670972013-08-17 00:46:16 +0000441 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6af701f2013-05-13 04:18:18 +0000442 } else {
Richard Smith2d670972013-08-17 00:46:16 +0000443 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
444 : diag::err_omp_expected_var_arg)
445 << Id.getName();
446 return ExprError();
Alexey Bataev6af701f2013-05-13 04:18:18 +0000447 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000448 } else {
449 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700450 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6af701f2013-05-13 04:18:18 +0000451 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
452 return ExprError();
453 }
454 }
455 Lookup.suppressDiagnostics();
456
457 // OpenMP [2.9.2, Syntax, C/C++]
458 // Variables must be file-scope, namespace-scope, or static block-scope.
459 if (!VD->hasGlobalStorage()) {
460 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700461 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
462 bool IsDecl =
463 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000464 Diag(VD->getLocation(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700465 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
466 << VD;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000467 return ExprError();
468 }
469
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000470 VarDecl *CanonicalVD = VD->getCanonicalDecl();
471 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6af701f2013-05-13 04:18:18 +0000472 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
473 // A threadprivate directive for file-scope variables must appear outside
474 // any definition or declaration.
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000475 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
476 !getCurLexicalContext()->isTranslationUnit()) {
477 Diag(Id.getLoc(), diag::err_omp_var_scope)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700478 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
479 bool IsDecl =
480 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
481 Diag(VD->getLocation(),
482 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
483 << VD;
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000484 return ExprError();
485 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000486 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
487 // A threadprivate directive for static class member variables must appear
488 // in the class definition, in the same scope in which the member
489 // variables are declared.
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000490 if (CanonicalVD->isStaticDataMember() &&
491 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
492 Diag(Id.getLoc(), diag::err_omp_var_scope)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700493 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
494 bool IsDecl =
495 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
496 Diag(VD->getLocation(),
497 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
498 << VD;
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000499 return ExprError();
500 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000501 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
502 // A threadprivate directive for namespace-scope variables must appear
503 // outside any definition or declaration other than the namespace
504 // definition itself.
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000505 if (CanonicalVD->getDeclContext()->isNamespace() &&
506 (!getCurLexicalContext()->isFileContext() ||
507 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
508 Diag(Id.getLoc(), diag::err_omp_var_scope)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700509 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
510 bool IsDecl =
511 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
512 Diag(VD->getLocation(),
513 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
514 << VD;
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000515 return ExprError();
516 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000517 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
518 // A threadprivate directive for static block-scope variables must appear
519 // in the scope of the variable and not in a nested scope.
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000520 if (CanonicalVD->isStaticLocal() && CurScope &&
521 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6af701f2013-05-13 04:18:18 +0000522 Diag(Id.getLoc(), diag::err_omp_var_scope)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700523 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
524 bool IsDecl =
525 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
526 Diag(VD->getLocation(),
527 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
528 << VD;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000529 return ExprError();
530 }
531
532 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
533 // A threadprivate directive must lexically precede all references to any
534 // of the variables in its list.
535 if (VD->isUsed()) {
536 Diag(Id.getLoc(), diag::err_omp_var_used)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700537 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000538 return ExprError();
539 }
540
541 QualType ExprType = VD->getType().getNonReferenceType();
Stephen Hines651f13c2014-04-23 16:59:28 -0700542 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6af701f2013-05-13 04:18:18 +0000543 return DE;
544}
545
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700546Sema::DeclGroupPtrTy
547Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
548 ArrayRef<Expr *> VarList) {
Alexey Bataev6af701f2013-05-13 04:18:18 +0000549 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000550 CurContext->addDecl(D);
551 return DeclGroupPtrTy::make(DeclGroupRef(D));
552 }
553 return DeclGroupPtrTy();
554}
555
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700556OMPThreadPrivateDecl *
557Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6af701f2013-05-13 04:18:18 +0000558 SmallVector<Expr *, 8> Vars;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700559 for (auto &RefExpr : VarList) {
560 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6af701f2013-05-13 04:18:18 +0000561 VarDecl *VD = cast<VarDecl>(DE->getDecl());
562 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataevc6400582013-03-22 06:34:35 +0000563
564 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
565 // A threadprivate variable must not have an incomplete type.
566 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6af701f2013-05-13 04:18:18 +0000567 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000568 continue;
569 }
570
571 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
572 // A threadprivate variable must not have a reference type.
573 if (VD->getType()->isReferenceType()) {
574 Diag(ILoc, diag::err_omp_ref_type_arg)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700575 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
576 bool IsDecl =
577 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
578 Diag(VD->getLocation(),
579 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
580 << VD;
Alexey Bataevc6400582013-03-22 06:34:35 +0000581 continue;
582 }
583
Richard Smith38afbc72013-04-13 02:43:54 +0000584 // Check if this is a TLS variable.
585 if (VD->getTLSKind()) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000586 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700587 bool IsDecl =
588 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
589 Diag(VD->getLocation(),
590 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
591 << VD;
Alexey Bataevc6400582013-03-22 06:34:35 +0000592 continue;
593 }
594
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700595 Vars.push_back(RefExpr);
Stephen Hines651f13c2014-04-23 16:59:28 -0700596 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataevc6400582013-03-22 06:34:35 +0000597 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700598 OMPThreadPrivateDecl *D = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700599 if (!Vars.empty()) {
600 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
601 Vars);
602 D->setAccess(AS_public);
603 }
604 return D;
Alexey Bataevc6400582013-03-22 06:34:35 +0000605}
Alexey Bataev6af701f2013-05-13 04:18:18 +0000606
Alexey Bataev0c018352013-09-06 18:03:48 +0000607namespace {
608class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
609 DSAStackTy *Stack;
610 Sema &Actions;
611 bool ErrorFound;
612 CapturedStmt *CS;
Alexey Bataevd195bc32013-10-01 05:32:34 +0000613 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700614
Alexey Bataev0c018352013-09-06 18:03:48 +0000615public:
616 void VisitDeclRefExpr(DeclRefExpr *E) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700617 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0c018352013-09-06 18:03:48 +0000618 // Skip internally declared variables.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700619 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
620 return;
Alexey Bataev0c018352013-09-06 18:03:48 +0000621
622 SourceLocation ELoc = E->getExprLoc();
623
624 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
625 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
626 if (DVar.CKind != OMPC_unknown) {
627 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700628 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd195bc32013-10-01 05:32:34 +0000629 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev0c018352013-09-06 18:03:48 +0000630 return;
631 }
632 // The default(none) clause requires that each variable that is referenced
633 // in the construct, and does not have a predetermined data-sharing
634 // attribute, must have its data-sharing attribute explicitly determined
635 // by being listed in a data-sharing attribute clause.
636 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
637 (DKind == OMPD_parallel || DKind == OMPD_task)) {
638 ErrorFound = true;
639 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
640 return;
641 }
642
643 // OpenMP [2.9.3.6, Restrictions, p.2]
644 // A list item that appears in a reduction clause of the innermost
645 // enclosing worksharing or parallel construct may not be accessed in an
646 // explicit task.
647 // TODO:
648
649 // Define implicit data-sharing attributes for task.
650 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd195bc32013-10-01 05:32:34 +0000651 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
652 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev0c018352013-09-06 18:03:48 +0000653 }
654 }
655 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700656 for (auto C : S->clauses())
657 if (C)
Alexey Bataev0c018352013-09-06 18:03:48 +0000658 for (StmtRange R = C->children(); R; ++R)
659 if (Stmt *Child = *R)
660 Visit(Child);
661 }
662 void VisitStmt(Stmt *S) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700663 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
664 ++I)
Alexey Bataev0c018352013-09-06 18:03:48 +0000665 if (Stmt *Child = *I)
666 if (!isa<OMPExecutableDirective>(Child))
667 Visit(Child);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700668 }
Alexey Bataev0c018352013-09-06 18:03:48 +0000669
670 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd195bc32013-10-01 05:32:34 +0000671 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev0c018352013-09-06 18:03:48 +0000672
673 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700674 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) {}
Alexey Bataev0c018352013-09-06 18:03:48 +0000675};
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700676} // namespace
677
678void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
679 Scope *CurScope) {
680 switch (DKind) {
681 case OMPD_parallel: {
682 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
683 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
684 Sema::CapturedParamNameType Params[3] = {
685 std::make_pair(".global_tid.", KmpInt32PtrTy),
686 std::make_pair(".bound_tid.", KmpInt32PtrTy),
687 std::make_pair(StringRef(), QualType()) // __context with shared vars
688 };
689 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
690 break;
691 }
692 case OMPD_simd: {
693 Sema::CapturedParamNameType Params[1] = {
694 std::make_pair(StringRef(), QualType()) // __context with shared vars
695 };
696 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
697 break;
698 }
699 case OMPD_threadprivate:
700 case OMPD_task:
701 llvm_unreachable("OpenMP Directive is not allowed");
702 case OMPD_unknown:
703 llvm_unreachable("Unknown OpenMP directive");
704 }
Alexey Bataev0c018352013-09-06 18:03:48 +0000705}
706
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000707StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
708 ArrayRef<OMPClause *> Clauses,
709 Stmt *AStmt,
710 SourceLocation StartLoc,
711 SourceLocation EndLoc) {
Alexey Bataev0c018352013-09-06 18:03:48 +0000712 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
713
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000714 StmtResult Res = StmtError();
Alexey Bataev0c018352013-09-06 18:03:48 +0000715
716 // Check default data sharing attributes for referenced variables.
717 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
718 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
719 if (DSAChecker.isErrorFound())
720 return StmtError();
Alexey Bataevd195bc32013-10-01 05:32:34 +0000721 // Generate list of implicitly defined firstprivate variables.
722 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
723 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
724
725 bool ErrorFound = false;
726 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700727 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
728 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
729 SourceLocation(), SourceLocation())) {
Alexey Bataevd195bc32013-10-01 05:32:34 +0000730 ClausesWithImplicit.push_back(Implicit);
731 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700732 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd195bc32013-10-01 05:32:34 +0000733 } else
734 ErrorFound = true;
735 }
Alexey Bataev0c018352013-09-06 18:03:48 +0000736
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000737 switch (Kind) {
738 case OMPD_parallel:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700739 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
740 EndLoc);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000741 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700742 case OMPD_simd:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700743 Res =
744 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Stephen Hines651f13c2014-04-23 16:59:28 -0700745 break;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000746 case OMPD_threadprivate:
747 case OMPD_task:
748 llvm_unreachable("OpenMP Directive is not allowed");
749 case OMPD_unknown:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000750 llvm_unreachable("Unknown OpenMP directive");
751 }
Alexey Bataevd195bc32013-10-01 05:32:34 +0000752
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700753 if (ErrorFound)
754 return StmtError();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000755 return Res;
756}
757
758StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
759 Stmt *AStmt,
760 SourceLocation StartLoc,
761 SourceLocation EndLoc) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700762 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
763 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
764 // 1.2.2 OpenMP Language Terminology
765 // Structured block - An executable statement with a single entry at the
766 // top and a single exit at the bottom.
767 // The point of exit cannot be a branch out of the structured block.
768 // longjmp() and throw() must not violate the entry/exit criteria.
769 CS->getCapturedDecl()->setNothrow();
770
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000771 getCurFunction()->setHasBranchProtectedScope();
772
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700773 return Owned(
774 OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt));
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000775}
776
Stephen Hines651f13c2014-04-23 16:59:28 -0700777StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700778 Stmt *AStmt, SourceLocation StartLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -0700779 SourceLocation EndLoc) {
780 Stmt *CStmt = AStmt;
781 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt))
782 CStmt = CS->getCapturedStmt();
783 while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt))
784 CStmt = AS->getSubStmt();
785 ForStmt *For = dyn_cast<ForStmt>(CStmt);
786 if (!For) {
787 Diag(CStmt->getLocStart(), diag::err_omp_not_for)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700788 << getOpenMPDirectiveName(OMPD_simd);
Stephen Hines651f13c2014-04-23 16:59:28 -0700789 return StmtError();
790 }
791
792 // FIXME: Checking loop canonical form, collapsing etc.
793
794 getCurFunction()->setHasBranchProtectedScope();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700795 return Owned(
796 OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt));
Stephen Hines651f13c2014-04-23 16:59:28 -0700797}
798
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700799OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Stephen Hines651f13c2014-04-23 16:59:28 -0700800 SourceLocation StartLoc,
801 SourceLocation LParenLoc,
802 SourceLocation EndLoc) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700803 OMPClause *Res = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700804 switch (Kind) {
805 case OMPC_if:
806 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
807 break;
808 case OMPC_num_threads:
809 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
810 break;
811 case OMPC_safelen:
812 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
813 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700814 case OMPC_collapse:
815 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
816 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700817 case OMPC_default:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700818 case OMPC_proc_bind:
Stephen Hines651f13c2014-04-23 16:59:28 -0700819 case OMPC_private:
820 case OMPC_firstprivate:
821 case OMPC_shared:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700822 case OMPC_linear:
Stephen Hines651f13c2014-04-23 16:59:28 -0700823 case OMPC_copyin:
824 case OMPC_threadprivate:
825 case OMPC_unknown:
Stephen Hines651f13c2014-04-23 16:59:28 -0700826 llvm_unreachable("Clause is not allowed.");
827 }
828 return Res;
829}
830
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700831OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -0700832 SourceLocation LParenLoc,
833 SourceLocation EndLoc) {
834 Expr *ValExpr = Condition;
835 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
836 !Condition->isInstantiationDependent() &&
837 !Condition->containsUnexpandedParameterPack()) {
838 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700839 Condition->getExprLoc(), Condition);
Stephen Hines651f13c2014-04-23 16:59:28 -0700840 if (Val.isInvalid())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700841 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700842
843 ValExpr = Val.take();
844 }
845
846 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
847}
848
849ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
850 Expr *Op) {
851 if (!Op)
852 return ExprError();
853
854 class IntConvertDiagnoser : public ICEConvertDiagnoser {
855 public:
856 IntConvertDiagnoser()
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700857 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Stephen Hines651f13c2014-04-23 16:59:28 -0700858 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
859 QualType T) override {
860 return S.Diag(Loc, diag::err_omp_not_integral) << T;
861 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700862 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
863 QualType T) override {
Stephen Hines651f13c2014-04-23 16:59:28 -0700864 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
865 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700866 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
867 QualType T,
868 QualType ConvTy) override {
Stephen Hines651f13c2014-04-23 16:59:28 -0700869 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
870 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700871 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
872 QualType ConvTy) override {
Stephen Hines651f13c2014-04-23 16:59:28 -0700873 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700874 << ConvTy->isEnumeralType() << ConvTy;
Stephen Hines651f13c2014-04-23 16:59:28 -0700875 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700876 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
877 QualType T) override {
Stephen Hines651f13c2014-04-23 16:59:28 -0700878 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
879 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700880 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
881 QualType ConvTy) override {
Stephen Hines651f13c2014-04-23 16:59:28 -0700882 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700883 << ConvTy->isEnumeralType() << ConvTy;
Stephen Hines651f13c2014-04-23 16:59:28 -0700884 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700885 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
886 QualType) override {
Stephen Hines651f13c2014-04-23 16:59:28 -0700887 llvm_unreachable("conversion functions are permitted");
888 }
889 } ConvertDiagnoser;
890 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
891}
892
893OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
894 SourceLocation StartLoc,
895 SourceLocation LParenLoc,
896 SourceLocation EndLoc) {
897 Expr *ValExpr = NumThreads;
898 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
899 !NumThreads->isInstantiationDependent() &&
900 !NumThreads->containsUnexpandedParameterPack()) {
901 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
902 ExprResult Val =
903 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
904 if (Val.isInvalid())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700905 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700906
907 ValExpr = Val.take();
908
909 // OpenMP [2.5, Restrictions]
910 // The num_threads expression must evaluate to a positive integer value.
911 llvm::APSInt Result;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700912 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
913 !Result.isStrictlyPositive()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700914 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
915 << "num_threads" << NumThreads->getSourceRange();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700916 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700917 }
918 }
919
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700920 return new (Context)
921 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Stephen Hines651f13c2014-04-23 16:59:28 -0700922}
923
924ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
925 OpenMPClauseKind CKind) {
926 if (!E)
927 return ExprError();
928 if (E->isValueDependent() || E->isTypeDependent() ||
929 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
930 return Owned(E);
931 llvm::APSInt Result;
932 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
933 if (ICE.isInvalid())
934 return ExprError();
935 if (!Result.isStrictlyPositive()) {
936 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
937 << getOpenMPClauseName(CKind) << E->getSourceRange();
938 return ExprError();
939 }
940 return ICE;
941}
942
943OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
944 SourceLocation LParenLoc,
945 SourceLocation EndLoc) {
946 // OpenMP [2.8.1, simd construct, Description]
947 // The parameter of the safelen clause must be a constant
948 // positive integer expression.
949 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
950 if (Safelen.isInvalid())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700951 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -0700952 return new (Context)
953 OMPSafelenClause(Safelen.take(), StartLoc, LParenLoc, EndLoc);
954}
955
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700956OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *Num, SourceLocation StartLoc,
957 SourceLocation LParenLoc,
958 SourceLocation EndLoc) {
959 // OpenMP [2.8.1, simd construct, Description]
960 // The parameter of the collapse clause must be a constant
961 // positive integer expression.
962 ExprResult NumForLoops =
963 VerifyPositiveIntegerConstantInClause(Num, OMPC_collapse);
964 if (NumForLoops.isInvalid())
965 return nullptr;
966 return new (Context)
967 OMPCollapseClause(NumForLoops.take(), StartLoc, LParenLoc, EndLoc);
968}
969
970OMPClause *Sema::ActOnOpenMPSimpleClause(
971 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
972 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
973 OMPClause *Res = nullptr;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000974 switch (Kind) {
975 case OMPC_default:
Alexey Bataev0c018352013-09-06 18:03:48 +0000976 Res =
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700977 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
978 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
979 break;
980 case OMPC_proc_bind:
981 Res = ActOnOpenMPProcBindClause(
982 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
983 LParenLoc, EndLoc);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000984 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700985 case OMPC_if:
986 case OMPC_num_threads:
987 case OMPC_safelen:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700988 case OMPC_collapse:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000989 case OMPC_private:
Alexey Bataevd195bc32013-10-01 05:32:34 +0000990 case OMPC_firstprivate:
Alexey Bataev0c018352013-09-06 18:03:48 +0000991 case OMPC_shared:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700992 case OMPC_linear:
Stephen Hines651f13c2014-04-23 16:59:28 -0700993 case OMPC_copyin:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000994 case OMPC_threadprivate:
995 case OMPC_unknown:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000996 llvm_unreachable("Clause is not allowed.");
997 }
998 return Res;
999}
1000
1001OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1002 SourceLocation KindKwLoc,
1003 SourceLocation StartLoc,
1004 SourceLocation LParenLoc,
1005 SourceLocation EndLoc) {
1006 if (Kind == OMPC_DEFAULT_unknown) {
1007 std::string Values;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001008 static_assert(OMPC_DEFAULT_unknown > 0,
1009 "OMPC_DEFAULT_unknown not greater than 0");
Stephen Hines651f13c2014-04-23 16:59:28 -07001010 std::string Sep(", ");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001011 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001012 Values += "'";
1013 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1014 Values += "'";
1015 switch (i) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001016 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001017 Values += " or ";
1018 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001019 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001020 break;
1021 default:
1022 Values += Sep;
1023 break;
1024 }
1025 }
1026 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001027 << Values << getOpenMPClauseName(OMPC_default);
1028 return nullptr;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001029 }
Alexey Bataev0c018352013-09-06 18:03:48 +00001030 switch (Kind) {
1031 case OMPC_DEFAULT_none:
1032 DSAStack->setDefaultDSANone();
1033 break;
1034 case OMPC_DEFAULT_shared:
1035 DSAStack->setDefaultDSAShared();
1036 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07001037 case OMPC_DEFAULT_unknown:
Stephen Hines651f13c2014-04-23 16:59:28 -07001038 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev0c018352013-09-06 18:03:48 +00001039 break;
1040 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001041 return new (Context)
1042 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001043}
1044
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001045OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1046 SourceLocation KindKwLoc,
1047 SourceLocation StartLoc,
1048 SourceLocation LParenLoc,
1049 SourceLocation EndLoc) {
1050 if (Kind == OMPC_PROC_BIND_unknown) {
1051 std::string Values;
1052 std::string Sep(", ");
1053 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1054 Values += "'";
1055 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1056 Values += "'";
1057 switch (i) {
1058 case OMPC_PROC_BIND_unknown - 2:
1059 Values += " or ";
1060 break;
1061 case OMPC_PROC_BIND_unknown - 1:
1062 break;
1063 default:
1064 Values += Sep;
1065 break;
1066 }
1067 }
1068 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
1069 << Values << getOpenMPClauseName(OMPC_proc_bind);
1070 return nullptr;
1071 }
1072 return new (Context)
1073 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
1074}
1075
1076OMPClause *
1077Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, ArrayRef<Expr *> VarList,
1078 Expr *TailExpr, SourceLocation StartLoc,
1079 SourceLocation LParenLoc,
1080 SourceLocation ColonLoc, SourceLocation EndLoc) {
1081 OMPClause *Res = nullptr;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001082 switch (Kind) {
1083 case OMPC_private:
1084 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1085 break;
Alexey Bataevd195bc32013-10-01 05:32:34 +00001086 case OMPC_firstprivate:
1087 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1088 break;
Alexey Bataev0c018352013-09-06 18:03:48 +00001089 case OMPC_shared:
1090 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1091 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001092 case OMPC_linear:
1093 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1094 ColonLoc, EndLoc);
1095 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07001096 case OMPC_copyin:
1097 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1098 break;
1099 case OMPC_if:
1100 case OMPC_num_threads:
1101 case OMPC_safelen:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001102 case OMPC_collapse:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001103 case OMPC_default:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001104 case OMPC_proc_bind:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001105 case OMPC_threadprivate:
1106 case OMPC_unknown:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001107 llvm_unreachable("Clause is not allowed.");
1108 }
1109 return Res;
1110}
1111
1112OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1113 SourceLocation StartLoc,
1114 SourceLocation LParenLoc,
1115 SourceLocation EndLoc) {
1116 SmallVector<Expr *, 8> Vars;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001117 for (auto &RefExpr : VarList) {
1118 assert(RefExpr && "NULL expr in OpenMP private clause.");
1119 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001120 // It will be analyzed later.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001121 Vars.push_back(RefExpr);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001122 continue;
1123 }
1124
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001125 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001126 // OpenMP [2.1, C/C++]
1127 // A list item is a variable name.
1128 // OpenMP [2.9.3.3, Restrictions, p.1]
1129 // A variable that is part of another variable (as an array or
1130 // structure element) cannot appear in a private clause.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001131 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001132 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001133 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001134 continue;
1135 }
1136 Decl *D = DE->getDecl();
1137 VarDecl *VD = cast<VarDecl>(D);
1138
1139 QualType Type = VD->getType();
1140 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1141 // It will be analyzed later.
1142 Vars.push_back(DE);
1143 continue;
1144 }
1145
1146 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1147 // A variable that appears in a private clause must not have an incomplete
1148 // type or a reference type.
1149 if (RequireCompleteType(ELoc, Type,
1150 diag::err_omp_private_incomplete_type)) {
1151 continue;
1152 }
1153 if (Type->isReferenceType()) {
1154 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001155 << getOpenMPClauseName(OMPC_private) << Type;
1156 bool IsDecl =
1157 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1158 Diag(VD->getLocation(),
1159 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1160 << VD;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001161 continue;
1162 }
1163
1164 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1165 // A variable of class type (or array thereof) that appears in a private
1166 // clause requires an accesible, unambiguous default constructor for the
1167 // class type.
1168 while (Type.getNonReferenceType()->isArrayType()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001169 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
1170 ->getElementType();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001171 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001172 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1173 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1174 : nullptr;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001175 if (RD) {
1176 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1177 PartialDiagnostic PD =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001178 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1179 if (!CD || CheckConstructorAccess(
1180 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1181 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001182 CD->isDeleted()) {
1183 Diag(ELoc, diag::err_omp_required_method)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001184 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001185 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1186 VarDecl::DeclarationOnly;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001187 Diag(VD->getLocation(),
1188 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1189 << VD;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001190 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1191 continue;
1192 }
1193 MarkFunctionReferenced(ELoc, CD);
1194 DiagnoseUseOfDecl(CD, ELoc);
1195
1196 CXXDestructorDecl *DD = RD->getDestructor();
1197 if (DD) {
1198 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1199 DD->isDeleted()) {
1200 Diag(ELoc, diag::err_omp_required_method)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001201 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001202 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1203 VarDecl::DeclarationOnly;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001204 Diag(VD->getLocation(),
1205 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1206 << VD;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001207 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1208 continue;
1209 }
1210 MarkFunctionReferenced(ELoc, DD);
1211 DiagnoseUseOfDecl(DD, ELoc);
1212 }
1213 }
1214
Alexey Bataev0c018352013-09-06 18:03:48 +00001215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1216 // in a Construct]
1217 // Variables with the predetermined data-sharing attributes may not be
1218 // listed in data-sharing attributes clauses, except for the cases
1219 // listed below. For these exceptions only, listing a predetermined
1220 // variable in a data-sharing attribute clause is allowed and overrides
1221 // the variable's predetermined data-sharing attributes.
1222 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1223 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001224 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1225 << getOpenMPClauseName(OMPC_private);
Alexey Bataev0c018352013-09-06 18:03:48 +00001226 if (DVar.RefExpr) {
1227 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001228 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev0c018352013-09-06 18:03:48 +00001229 } else {
1230 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001231 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev0c018352013-09-06 18:03:48 +00001232 }
1233 continue;
1234 }
1235
1236 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001237 Vars.push_back(DE);
1238 }
1239
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001240 if (Vars.empty())
1241 return nullptr;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +00001242
1243 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1244}
1245
Alexey Bataevd195bc32013-10-01 05:32:34 +00001246OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1247 SourceLocation StartLoc,
1248 SourceLocation LParenLoc,
1249 SourceLocation EndLoc) {
1250 SmallVector<Expr *, 8> Vars;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001251 for (auto &RefExpr : VarList) {
1252 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
1253 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd195bc32013-10-01 05:32:34 +00001254 // It will be analyzed later.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001255 Vars.push_back(RefExpr);
Alexey Bataevd195bc32013-10-01 05:32:34 +00001256 continue;
1257 }
1258
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001259 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd195bc32013-10-01 05:32:34 +00001260 // OpenMP [2.1, C/C++]
1261 // A list item is a variable name.
1262 // OpenMP [2.9.3.3, Restrictions, p.1]
1263 // A variable that is part of another variable (as an array or
1264 // structure element) cannot appear in a private clause.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001265 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd195bc32013-10-01 05:32:34 +00001266 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001267 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd195bc32013-10-01 05:32:34 +00001268 continue;
1269 }
1270 Decl *D = DE->getDecl();
1271 VarDecl *VD = cast<VarDecl>(D);
1272
1273 QualType Type = VD->getType();
1274 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1275 // It will be analyzed later.
1276 Vars.push_back(DE);
1277 continue;
1278 }
1279
1280 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1281 // A variable that appears in a private clause must not have an incomplete
1282 // type or a reference type.
1283 if (RequireCompleteType(ELoc, Type,
1284 diag::err_omp_firstprivate_incomplete_type)) {
1285 continue;
1286 }
1287 if (Type->isReferenceType()) {
1288 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001289 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1290 bool IsDecl =
1291 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1292 Diag(VD->getLocation(),
1293 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1294 << VD;
Alexey Bataevd195bc32013-10-01 05:32:34 +00001295 continue;
1296 }
1297
1298 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1299 // A variable of class type (or array thereof) that appears in a private
1300 // clause requires an accesible, unambiguous copy constructor for the
1301 // class type.
1302 Type = Context.getBaseElementType(Type);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001303 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1304 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1305 : nullptr;
Alexey Bataevd195bc32013-10-01 05:32:34 +00001306 if (RD) {
1307 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1308 PartialDiagnostic PD =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001309 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1310 if (!CD || CheckConstructorAccess(
1311 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1312 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd195bc32013-10-01 05:32:34 +00001313 CD->isDeleted()) {
1314 Diag(ELoc, diag::err_omp_required_method)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001315 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd195bc32013-10-01 05:32:34 +00001316 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1317 VarDecl::DeclarationOnly;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001318 Diag(VD->getLocation(),
1319 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1320 << VD;
Alexey Bataevd195bc32013-10-01 05:32:34 +00001321 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1322 continue;
1323 }
1324 MarkFunctionReferenced(ELoc, CD);
1325 DiagnoseUseOfDecl(CD, ELoc);
1326
1327 CXXDestructorDecl *DD = RD->getDestructor();
1328 if (DD) {
1329 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1330 DD->isDeleted()) {
1331 Diag(ELoc, diag::err_omp_required_method)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001332 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd195bc32013-10-01 05:32:34 +00001333 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1334 VarDecl::DeclarationOnly;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001335 Diag(VD->getLocation(),
1336 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1337 << VD;
Alexey Bataevd195bc32013-10-01 05:32:34 +00001338 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1339 continue;
1340 }
1341 MarkFunctionReferenced(ELoc, DD);
1342 DiagnoseUseOfDecl(DD, ELoc);
1343 }
1344 }
1345
1346 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1347 // variable and it was checked already.
1348 if (StartLoc.isValid() && EndLoc.isValid()) {
1349 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1350 Type = Type.getNonReferenceType().getCanonicalType();
1351 bool IsConstant = Type.isConstant(Context);
1352 Type = Context.getBaseElementType(Type);
1353 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1354 // A list item that specifies a given variable may not appear in more
1355 // than one clause on the same directive, except that a variable may be
1356 // specified in both firstprivate and lastprivate clauses.
1357 // TODO: add processing for lastprivate.
1358 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1359 DVar.RefExpr) {
1360 Diag(ELoc, diag::err_omp_wrong_dsa)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001361 << getOpenMPClauseName(DVar.CKind)
1362 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd195bc32013-10-01 05:32:34 +00001363 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001364 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd195bc32013-10-01 05:32:34 +00001365 continue;
1366 }
1367
1368 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1369 // in a Construct]
1370 // Variables with the predetermined data-sharing attributes may not be
1371 // listed in data-sharing attributes clauses, except for the cases
1372 // listed below. For these exceptions only, listing a predetermined
1373 // variable in a data-sharing attribute clause is allowed and overrides
1374 // the variable's predetermined data-sharing attributes.
1375 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1376 // in a Construct, C/C++, p.2]
1377 // Variables with const-qualified type having no mutable member may be
1378 // listed in a firstprivate clause, even if they are static data members.
1379 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1380 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1381 Diag(ELoc, diag::err_omp_wrong_dsa)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001382 << getOpenMPClauseName(DVar.CKind)
1383 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd195bc32013-10-01 05:32:34 +00001384 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001385 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd195bc32013-10-01 05:32:34 +00001386 continue;
1387 }
1388
1389 // OpenMP [2.9.3.4, Restrictions, p.2]
1390 // A list item that is private within a parallel region must not appear
1391 // in a firstprivate clause on a worksharing construct if any of the
1392 // worksharing regions arising from the worksharing construct ever bind
1393 // to any of the parallel regions arising from the parallel construct.
1394 // OpenMP [2.9.3.4, Restrictions, p.3]
1395 // A list item that appears in a reduction clause of a parallel construct
1396 // must not appear in a firstprivate clause on a worksharing or task
1397 // construct if any of the worksharing or task regions arising from the
1398 // worksharing or task construct ever bind to any of the parallel regions
1399 // arising from the parallel construct.
1400 // OpenMP [2.9.3.4, Restrictions, p.4]
1401 // A list item that appears in a reduction clause in worksharing
1402 // construct must not appear in a firstprivate clause in a task construct
1403 // encountered during execution of any of the worksharing regions arising
1404 // from the worksharing construct.
1405 // TODO:
1406 }
1407
1408 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1409 Vars.push_back(DE);
1410 }
1411
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001412 if (Vars.empty())
1413 return nullptr;
Alexey Bataevd195bc32013-10-01 05:32:34 +00001414
1415 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1416 Vars);
1417}
1418
Alexey Bataev0c018352013-09-06 18:03:48 +00001419OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1420 SourceLocation StartLoc,
1421 SourceLocation LParenLoc,
1422 SourceLocation EndLoc) {
1423 SmallVector<Expr *, 8> Vars;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001424 for (auto &RefExpr : VarList) {
1425 assert(RefExpr && "NULL expr in OpenMP shared clause.");
1426 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev0c018352013-09-06 18:03:48 +00001427 // It will be analyzed later.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001428 Vars.push_back(RefExpr);
Alexey Bataev0c018352013-09-06 18:03:48 +00001429 continue;
1430 }
1431
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001432 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev0c018352013-09-06 18:03:48 +00001433 // OpenMP [2.1, C/C++]
1434 // A list item is a variable name.
Stephen Hines651f13c2014-04-23 16:59:28 -07001435 // OpenMP [2.14.3.2, Restrictions, p.1]
1436 // A variable that is part of another variable (as an array or structure
1437 // element) cannot appear in a shared unless it is a static data member
1438 // of a C++ class.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001439 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev0c018352013-09-06 18:03:48 +00001440 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001441 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev0c018352013-09-06 18:03:48 +00001442 continue;
1443 }
1444 Decl *D = DE->getDecl();
1445 VarDecl *VD = cast<VarDecl>(D);
1446
1447 QualType Type = VD->getType();
1448 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1449 // It will be analyzed later.
1450 Vars.push_back(DE);
1451 continue;
1452 }
1453
1454 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1455 // in a Construct]
1456 // Variables with the predetermined data-sharing attributes may not be
1457 // listed in data-sharing attributes clauses, except for the cases
1458 // listed below. For these exceptions only, listing a predetermined
1459 // variable in a data-sharing attribute clause is allowed and overrides
1460 // the variable's predetermined data-sharing attributes.
1461 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001462 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
1463 DVar.RefExpr) {
1464 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1465 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev0c018352013-09-06 18:03:48 +00001466 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001467 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev0c018352013-09-06 18:03:48 +00001468 continue;
1469 }
1470
1471 DSAStack->addDSA(VD, DE, OMPC_shared);
1472 Vars.push_back(DE);
1473 }
1474
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001475 if (Vars.empty())
1476 return nullptr;
Alexey Bataev0c018352013-09-06 18:03:48 +00001477
1478 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1479}
1480
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001481OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1482 SourceLocation StartLoc,
1483 SourceLocation LParenLoc,
1484 SourceLocation ColonLoc,
1485 SourceLocation EndLoc) {
1486 SmallVector<Expr *, 8> Vars;
1487 for (auto &RefExpr : VarList) {
1488 assert(RefExpr && "NULL expr in OpenMP linear clause.");
1489 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
1490 // It will be analyzed later.
1491 Vars.push_back(RefExpr);
1492 continue;
1493 }
1494
1495 // OpenMP [2.14.3.7, linear clause]
1496 // A list item that appears in a linear clause is subject to the private
1497 // clause semantics described in Section 2.14.3.3 on page 159 except as
1498 // noted. In addition, the value of the new list item on each iteration
1499 // of the associated loop(s) corresponds to the value of the original
1500 // list item before entering the construct plus the logical number of
1501 // the iteration times linear-step.
1502
1503 SourceLocation ELoc = RefExpr->getExprLoc();
1504 // OpenMP [2.1, C/C++]
1505 // A list item is a variable name.
1506 // OpenMP [2.14.3.3, Restrictions, p.1]
1507 // A variable that is part of another variable (as an array or
1508 // structure element) cannot appear in a private clause.
1509 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
1510 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1511 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
1512 continue;
1513 }
1514
1515 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1516
1517 // OpenMP [2.14.3.7, linear clause]
1518 // A list-item cannot appear in more than one linear clause.
1519 // A list-item that appears in a linear clause cannot appear in any
1520 // other data-sharing attribute clause.
1521 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1522 if (DVar.RefExpr) {
1523 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1524 << getOpenMPClauseName(OMPC_linear);
1525 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1526 << getOpenMPClauseName(DVar.CKind);
1527 continue;
1528 }
1529
1530 QualType QType = VD->getType();
1531 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1532 // It will be analyzed later.
1533 Vars.push_back(DE);
1534 continue;
1535 }
1536
1537 // A variable must not have an incomplete type or a reference type.
1538 if (RequireCompleteType(ELoc, QType,
1539 diag::err_omp_linear_incomplete_type)) {
1540 continue;
1541 }
1542 if (QType->isReferenceType()) {
1543 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1544 << getOpenMPClauseName(OMPC_linear) << QType;
1545 bool IsDecl =
1546 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1547 Diag(VD->getLocation(),
1548 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1549 << VD;
1550 continue;
1551 }
1552
1553 // A list item must not be const-qualified.
1554 if (QType.isConstant(Context)) {
1555 Diag(ELoc, diag::err_omp_const_variable)
1556 << getOpenMPClauseName(OMPC_linear);
1557 bool IsDecl =
1558 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1559 Diag(VD->getLocation(),
1560 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1561 << VD;
1562 continue;
1563 }
1564
1565 // A list item must be of integral or pointer type.
1566 QType = QType.getUnqualifiedType().getCanonicalType();
1567 const Type *Ty = QType.getTypePtrOrNull();
1568 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1569 !Ty->isPointerType())) {
1570 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1571 bool IsDecl =
1572 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1573 Diag(VD->getLocation(),
1574 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1575 << VD;
1576 continue;
1577 }
1578
1579 DSAStack->addDSA(VD, DE, OMPC_linear);
1580 Vars.push_back(DE);
1581 }
1582
1583 if (Vars.empty())
1584 return nullptr;
1585
1586 Expr *StepExpr = Step;
1587 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1588 !Step->isInstantiationDependent() &&
1589 !Step->containsUnexpandedParameterPack()) {
1590 SourceLocation StepLoc = Step->getLocStart();
1591 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1592 if (Val.isInvalid())
1593 return nullptr;
1594 StepExpr = Val.take();
1595
1596 // Warn about zero linear step (it would be probably better specified as
1597 // making corresponding variables 'const').
1598 llvm::APSInt Result;
1599 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1600 !Result.isNegative() && !Result.isStrictlyPositive())
1601 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1602 << (Vars.size() > 1);
1603 }
1604
1605 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1606 Vars, StepExpr);
1607}
1608
Stephen Hines651f13c2014-04-23 16:59:28 -07001609OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1610 SourceLocation StartLoc,
1611 SourceLocation LParenLoc,
1612 SourceLocation EndLoc) {
1613 SmallVector<Expr *, 8> Vars;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001614 for (auto &RefExpr : VarList) {
1615 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
1616 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001617 // It will be analyzed later.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001618 Vars.push_back(RefExpr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001619 continue;
1620 }
1621
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001622 SourceLocation ELoc = RefExpr->getExprLoc();
Stephen Hines651f13c2014-04-23 16:59:28 -07001623 // OpenMP [2.1, C/C++]
1624 // A list item is a variable name.
1625 // OpenMP [2.14.4.1, Restrictions, p.1]
1626 // A list item that appears in a copyin clause must be threadprivate.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001627 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001628 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001629 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Stephen Hines651f13c2014-04-23 16:59:28 -07001630 continue;
1631 }
1632
1633 Decl *D = DE->getDecl();
1634 VarDecl *VD = cast<VarDecl>(D);
1635
1636 QualType Type = VD->getType();
1637 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1638 // It will be analyzed later.
1639 Vars.push_back(DE);
1640 continue;
1641 }
1642
1643 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1644 // A list item that appears in a copyin clause must be threadprivate.
1645 if (!DSAStack->isThreadPrivate(VD)) {
1646 Diag(ELoc, diag::err_omp_required_access)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001647 << getOpenMPClauseName(OMPC_copyin)
1648 << getOpenMPDirectiveName(OMPD_threadprivate);
Stephen Hines651f13c2014-04-23 16:59:28 -07001649 continue;
1650 }
1651
1652 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1653 // A variable of class type (or array thereof) that appears in a
1654 // copyin clause requires an accesible, unambiguous copy assignment
1655 // operator for the class type.
1656 Type = Context.getBaseElementType(Type);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001657 CXXRecordDecl *RD =
1658 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001659 if (RD) {
1660 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1661 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001662 if (!MD || CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001663 MD->isDeleted()) {
1664 Diag(ELoc, diag::err_omp_required_method)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001665 << getOpenMPClauseName(OMPC_copyin) << 2;
Stephen Hines651f13c2014-04-23 16:59:28 -07001666 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1667 VarDecl::DeclarationOnly;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001668 Diag(VD->getLocation(),
1669 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1670 << VD;
Stephen Hines651f13c2014-04-23 16:59:28 -07001671 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1672 continue;
1673 }
1674 MarkFunctionReferenced(ELoc, MD);
1675 DiagnoseUseOfDecl(MD, ELoc);
1676 }
1677
1678 DSAStack->addDSA(VD, DE, OMPC_copyin);
1679 Vars.push_back(DE);
1680 }
1681
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001682 if (Vars.empty())
1683 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001684
1685 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1686}
1687
Alexey Bataev0c018352013-09-06 18:03:48 +00001688#undef DSAStack