blob: b41f7e35d14dfd77b58473756dd2bd7effaf490a [file] [log] [blame]
Alexey Bataeva769e072013-03-22 06:34:35 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ----------===//
2//
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
15#include "clang/Basic/OpenMPKinds.h"
16#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 Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000023#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/Sema/Scope.h"
25#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000026#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000027using namespace clang;
28
Alexey Bataev758e55e2013-09-06 18:03:48 +000029//===----------------------------------------------------------------------===//
30// Stack of data-sharing attributes for variables
31//===----------------------------------------------------------------------===//
32
33namespace {
34/// \brief Default data sharing attributes, which can be applied to directive.
35enum DefaultDataSharingAttributes {
36 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
37 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
38 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
39};
40
41/// \brief Stack for tracking declarations used in OpenMP directives and
42/// clauses and their data-sharing attributes.
43class DSAStackTy {
44public:
45 struct DSAVarData {
46 OpenMPDirectiveKind DKind;
47 OpenMPClauseKind CKind;
48 DeclRefExpr *RefExpr;
49 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(0) { }
50 };
51private:
52 struct DSAInfo {
53 OpenMPClauseKind Attributes;
54 DeclRefExpr *RefExpr;
55 };
56 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
57
58 struct SharingMapTy {
59 DeclSAMapTy SharingMap;
60 DefaultDataSharingAttributes DefaultAttr;
61 OpenMPDirectiveKind Directive;
62 DeclarationNameInfo DirectiveName;
63 Scope *CurScope;
64 SharingMapTy(OpenMPDirectiveKind DKind,
65 const DeclarationNameInfo &Name,
66 Scope *CurScope)
67 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
68 DirectiveName(Name), CurScope(CurScope) { }
69 SharingMapTy()
70 : SharingMap(), DefaultAttr(DSA_unspecified),
71 Directive(OMPD_unknown), DirectiveName(),
72 CurScope(0) { }
73 };
74
75 typedef SmallVector<SharingMapTy, 64> StackTy;
76
77 /// \brief Stack of used declaration and their data-sharing attributes.
78 StackTy Stack;
79 Sema &Actions;
80
81 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
82
83 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +000084
85 /// \brief Checks if the variable is a local for OpenMP region.
86 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataev758e55e2013-09-06 18:03:48 +000087public:
88 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) { }
89
90 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
91 Scope *CurScope) {
92 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
93 }
94
95 void pop() {
96 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
97 Stack.pop_back();
98 }
99
100 /// \brief Adds explicit data sharing attribute to the specified declaration.
101 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
102
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 /// \brief Returns data sharing attributes from top of the stack for the
104 /// specified declaration.
105 DSAVarData getTopDSA(VarDecl *D);
106 /// \brief Returns data-sharing attributes for the specified declaration.
107 DSAVarData getImplicitDSA(VarDecl *D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000108 /// \brief Checks if the specified variables has \a CKind data-sharing
109 /// attribute in \a DKind directive.
110 DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
111 OpenMPDirectiveKind DKind = OMPD_unknown);
112
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113 /// \brief Returns currently analyzed directive.
114 OpenMPDirectiveKind getCurrentDirective() const {
115 return Stack.back().Directive;
116 }
117
118 /// \brief Set default data sharing attribute to none.
119 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
120 /// \brief Set default data sharing attribute to shared.
121 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
122
123 DefaultDataSharingAttributes getDefaultDSA() const {
124 return Stack.back().DefaultAttr;
125 }
126
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000127 /// \brief Checks if the spewcified variable is threadprivate.
128 bool isThreadPrivate(VarDecl *D) {
129 DSAVarData DVar = getTopDSA(D);
130 return (DVar.CKind == OMPC_threadprivate || DVar.CKind == OMPC_copyin);
131 }
132
133 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000134 Scope *getCurScope() { return Stack.back().CurScope; }
135};
136} // end anonymous namespace.
137
138DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
139 VarDecl *D) {
140 DSAVarData DVar;
141 if (Iter == Stack.rend() - 1) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000142 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
143 // in a region but not in construct]
144 // File-scope or namespace-scope variables referenced in called routines
145 // in the region are shared unless they appear in a threadprivate
146 // directive.
147 // TODO
148 if (!D->isFunctionOrMethodVarDecl())
149 DVar.CKind = OMPC_shared;
150
151 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
152 // in a region but not in construct]
153 // Variables with static storage duration that are declared in called
154 // routines in the region are shared.
155 if (D->hasGlobalStorage())
156 DVar.CKind = OMPC_shared;
157
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158 return DVar;
159 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000160
Alexey Bataev758e55e2013-09-06 18:03:48 +0000161 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000162 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
163 // in a Construct, C/C++, predetermined, p.1]
164 // Variables with automatic storage duration that are declared in a scope
165 // inside the construct are private.
166 if (DVar.DKind != OMPD_parallel) {
167 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
168 (D->getStorageClass() == SC_Auto ||
169 D->getStorageClass() == SC_None)) {
170 DVar.CKind = OMPC_private;
171 return DVar;
172 }
173 }
174
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175 // Explicitly specified attributes and local variables with predetermined
176 // attributes.
177 if (Iter->SharingMap.count(D)) {
178 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
179 DVar.CKind = Iter->SharingMap[D].Attributes;
180 return DVar;
181 }
182
183 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
184 // in a Construct, C/C++, implicitly determined, p.1]
185 // In a parallel or task construct, the data-sharing attributes of these
186 // variables are determined by the default clause, if present.
187 switch (Iter->DefaultAttr) {
188 case DSA_shared:
189 DVar.CKind = OMPC_shared;
190 return DVar;
191 case DSA_none:
192 return DVar;
193 case DSA_unspecified:
194 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
195 // in a Construct, implicitly determined, p.2]
196 // In a parallel construct, if no default clause is present, these
197 // variables are shared.
198 if (DVar.DKind == OMPD_parallel) {
199 DVar.CKind = OMPC_shared;
200 return DVar;
201 }
202
203 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
204 // in a Construct, implicitly determined, p.4]
205 // In a task construct, if no default clause is present, a variable that in
206 // the enclosing context is determined to be shared by all implicit tasks
207 // bound to the current team is shared.
208 // TODO
209 if (DVar.DKind == OMPD_task) {
210 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000211 for (StackTy::reverse_iterator I = std::next(Iter),
212 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000213 I != EE; ++I) {
214 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
215 // in a Construct, implicitly determined, p.6]
216 // In a task construct, if no default clause is present, a variable
217 // whose data-sharing attribute is not determined by the rules above is
218 // firstprivate.
219 DVarTemp = getDSA(I, D);
220 if (DVarTemp.CKind != OMPC_shared) {
221 DVar.RefExpr = 0;
222 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000223 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000224 return DVar;
225 }
226 if (I->Directive == OMPD_parallel) break;
227 }
228 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000229 DVar.CKind =
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000230 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000231 return DVar;
232 }
233 }
234 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
235 // in a Construct, implicitly determined, p.3]
236 // For constructs other than task, if no default clause is present, these
237 // variables inherit their data-sharing attributes from the enclosing
238 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000239 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240}
241
242void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
243 if (A == OMPC_threadprivate) {
244 Stack[0].SharingMap[D].Attributes = A;
245 Stack[0].SharingMap[D].RefExpr = E;
246 } else {
247 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
248 Stack.back().SharingMap[D].Attributes = A;
249 Stack.back().SharingMap[D].RefExpr = E;
250 }
251}
252
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000253bool
254DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000255 if (Stack.size() > 2) {
256 reverse_iterator I = Iter, E = Stack.rend() - 1;
257 Scope *TopScope = 0;
Fraser Cormack111023c2014-04-15 08:59:09 +0000258 while (I != E && I->Directive != OMPD_parallel) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000259 ++I;
260 }
261 if (I == E) return false;
262 TopScope = I->CurScope ? I->CurScope->getParent() : 0;
263 Scope *CurScope = getCurScope();
264 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000265 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000266 }
267 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000268 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000269 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270}
271
272DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
273 DSAVarData DVar;
274
275 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
276 // in a Construct, C/C++, predetermined, p.1]
277 // Variables appearing in threadprivate directives are threadprivate.
278 if (D->getTLSKind() != VarDecl::TLS_None) {
279 DVar.CKind = OMPC_threadprivate;
280 return DVar;
281 }
282 if (Stack[0].SharingMap.count(D)) {
283 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
284 DVar.CKind = OMPC_threadprivate;
285 return DVar;
286 }
287
288 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
289 // in a Construct, C/C++, predetermined, p.1]
290 // Variables with automatic storage duration that are declared in a scope
291 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000292 OpenMPDirectiveKind Kind = getCurrentDirective();
293 if (Kind != OMPD_parallel) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000294 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataevec3da872014-01-31 05:15:34 +0000295 (D->getStorageClass() == SC_Auto ||
Alexander Musman8dba6642014-04-22 13:09:42 +0000296 D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000297 DVar.CKind = OMPC_private;
298 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000299 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000300 }
301
302 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
303 // in a Construct, C/C++, predetermined, p.4]
304 // Static data memebers are shared.
305 if (D->isStaticDataMember()) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000306 // Variables with const-qualified type having no mutable member may be listed
307 // in a firstprivate clause, even if they are static data members.
308 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
309 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
310 return DVar;
311
Alexey Bataev758e55e2013-09-06 18:03:48 +0000312 DVar.CKind = OMPC_shared;
313 return DVar;
314 }
315
316 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
317 bool IsConstant = Type.isConstant(Actions.getASTContext());
318 while (Type->isArrayType()) {
319 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
320 Type = ElemType.getNonReferenceType().getCanonicalType();
321 }
322 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
323 // in a Construct, C/C++, predetermined, p.6]
324 // Variables with const qualified type having no mutable member are
325 // shared.
326 CXXRecordDecl *RD = Actions.getLangOpts().CPlusPlus ?
327 Type->getAsCXXRecordDecl() : 0;
328 if (IsConstant &&
329 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
330 // Variables with const-qualified type having no mutable member may be
331 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000332 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
333 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
334 return DVar;
335
Alexey Bataev758e55e2013-09-06 18:03:48 +0000336 DVar.CKind = OMPC_shared;
337 return DVar;
338 }
339
340 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
341 // in a Construct, C/C++, predetermined, p.7]
342 // Variables with static storage duration that are declared in a scope
343 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000344 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345 DVar.CKind = OMPC_shared;
346 return DVar;
347 }
348
349 // Explicitly specified attributes and local variables with predetermined
350 // attributes.
351 if (Stack.back().SharingMap.count(D)) {
352 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
353 DVar.CKind = Stack.back().SharingMap[D].Attributes;
354 }
355
356 return DVar;
357}
358
359DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000360 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000361}
362
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000363DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
364 OpenMPDirectiveKind DKind) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000365 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
366 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000367 I != E; ++I) {
368 if (DKind != OMPD_unknown && DKind != I->Directive) continue;
369 DSAVarData DVar = getDSA(I, D);
370 if (DVar.CKind == CKind)
371 return DVar;
372 }
373 return DSAVarData();
374}
375
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376void Sema::InitDataSharingAttributesStack() {
377 VarDataSharingAttributesStack = new DSAStackTy(*this);
378}
379
380#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
381
382void Sema::DestroyDataSharingAttributesStack() {
383 delete DSAStack;
384}
385
386void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
387 const DeclarationNameInfo &DirName,
388 Scope *CurScope) {
389 DSAStack->push(DKind, DirName, CurScope);
390 PushExpressionEvaluationContext(PotentiallyEvaluated);
391}
392
393void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
394 DSAStack->pop();
395 DiscardCleanupsInEvaluationContext();
396 PopExpressionEvaluationContext();
397}
398
Alexey Bataeva769e072013-03-22 06:34:35 +0000399namespace {
400
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000401class VarDeclFilterCCC : public CorrectionCandidateCallback {
402private:
403 Sema &Actions;
404public:
405 VarDeclFilterCCC(Sema &S) : Actions(S) { }
Craig Toppere14c0f82014-03-12 04:55:44 +0000406 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000407 NamedDecl *ND = Candidate.getCorrectionDecl();
408 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
409 return VD->hasGlobalStorage() &&
410 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
411 Actions.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000412 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000413 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000414 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000415};
416}
417
418ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
419 CXXScopeSpec &ScopeSpec,
420 const DeclarationNameInfo &Id) {
421 LookupResult Lookup(*this, Id, LookupOrdinaryName);
422 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
423
424 if (Lookup.isAmbiguous())
425 return ExprError();
426
427 VarDecl *VD;
428 if (!Lookup.isSingleResult()) {
429 VarDeclFilterCCC Validator(*this);
Richard Smithf9b15102013-08-17 00:46:16 +0000430 if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope,
John Thompson2255f2c2014-04-23 12:57:01 +0000431 0, Validator, CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000432 diagnoseTypo(Corrected,
433 PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest
434 : diag::err_omp_expected_var_arg_suggest)
435 << Id.getName());
436 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000437 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000438 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
439 : diag::err_omp_expected_var_arg)
440 << Id.getName();
441 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000442 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000443 } else {
444 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Richard Smithf9b15102013-08-17 00:46:16 +0000445 Diag(Id.getLoc(), diag::err_omp_expected_var_arg)
446 << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000447 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
448 return ExprError();
449 }
450 }
451 Lookup.suppressDiagnostics();
452
453 // OpenMP [2.9.2, Syntax, C/C++]
454 // Variables must be file-scope, namespace-scope, or static block-scope.
455 if (!VD->hasGlobalStorage()) {
456 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
457 << getOpenMPDirectiveName(OMPD_threadprivate)
458 << !VD->isStaticLocal();
459 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
460 VarDecl::DeclarationOnly;
461 Diag(VD->getLocation(),
462 IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD;
463 return ExprError();
464 }
465
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000466 VarDecl *CanonicalVD = VD->getCanonicalDecl();
467 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000468 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
469 // A threadprivate directive for file-scope variables must appear outside
470 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000471 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
472 !getCurLexicalContext()->isTranslationUnit()) {
473 Diag(Id.getLoc(), diag::err_omp_var_scope)
474 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
475 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
476 VarDecl::DeclarationOnly;
477 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
478 diag::note_defined_here) << VD;
479 return ExprError();
480 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000481 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
482 // A threadprivate directive for static class member variables must appear
483 // in the class definition, in the same scope in which the member
484 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000485 if (CanonicalVD->isStaticDataMember() &&
486 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
487 Diag(Id.getLoc(), diag::err_omp_var_scope)
488 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
489 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
490 VarDecl::DeclarationOnly;
491 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
492 diag::note_defined_here) << VD;
493 return ExprError();
494 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000495 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
496 // A threadprivate directive for namespace-scope variables must appear
497 // outside any definition or declaration other than the namespace
498 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000499 if (CanonicalVD->getDeclContext()->isNamespace() &&
500 (!getCurLexicalContext()->isFileContext() ||
501 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
502 Diag(Id.getLoc(), diag::err_omp_var_scope)
503 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
504 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
505 VarDecl::DeclarationOnly;
506 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
507 diag::note_defined_here) << VD;
508 return ExprError();
509 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000510 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
511 // A threadprivate directive for static block-scope variables must appear
512 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000513 if (CanonicalVD->isStaticLocal() && CurScope &&
514 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000515 Diag(Id.getLoc(), diag::err_omp_var_scope)
516 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
517 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
518 VarDecl::DeclarationOnly;
519 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
520 diag::note_defined_here) << VD;
521 return ExprError();
522 }
523
524 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
525 // A threadprivate directive must lexically precede all references to any
526 // of the variables in its list.
527 if (VD->isUsed()) {
528 Diag(Id.getLoc(), diag::err_omp_var_used)
529 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
530 return ExprError();
531 }
532
533 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000534 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000535 return DE;
536}
537
538Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective(
539 SourceLocation Loc,
540 ArrayRef<Expr *> VarList) {
541 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000542 CurContext->addDecl(D);
543 return DeclGroupPtrTy::make(DeclGroupRef(D));
544 }
545 return DeclGroupPtrTy();
546}
547
548OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl(
549 SourceLocation Loc,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000550 ArrayRef<Expr *> VarList) {
551 SmallVector<Expr *, 8> Vars;
552 for (ArrayRef<Expr *>::iterator I = VarList.begin(),
Alexey Bataeva769e072013-03-22 06:34:35 +0000553 E = VarList.end();
554 I != E; ++I) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000555 DeclRefExpr *DE = cast<DeclRefExpr>(*I);
556 VarDecl *VD = cast<VarDecl>(DE->getDecl());
557 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000558
559 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
560 // A threadprivate variable must not have an incomplete type.
561 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000562 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000563 continue;
564 }
565
566 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
567 // A threadprivate variable must not have a reference type.
568 if (VD->getType()->isReferenceType()) {
569 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000570 << getOpenMPDirectiveName(OMPD_threadprivate)
571 << VD->getType();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000572 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
573 VarDecl::DeclarationOnly;
574 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
575 diag::note_defined_here) << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000576 continue;
577 }
578
Richard Smithfd3834f2013-04-13 02:43:54 +0000579 // Check if this is a TLS variable.
580 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000581 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000582 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
583 VarDecl::DeclarationOnly;
584 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
585 diag::note_defined_here) << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000586 continue;
587 }
588
589 Vars.push_back(*I);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000590 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000591 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000592 OMPThreadPrivateDecl *D = 0;
593 if (!Vars.empty()) {
594 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
595 Vars);
596 D->setAccess(AS_public);
597 }
598 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000599}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000600
Alexey Bataev758e55e2013-09-06 18:03:48 +0000601namespace {
602class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
603 DSAStackTy *Stack;
604 Sema &Actions;
605 bool ErrorFound;
606 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000607 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000608public:
609 void VisitDeclRefExpr(DeclRefExpr *E) {
610 if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000611 // Skip internally declared variables.
612 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) return;
613
614 SourceLocation ELoc = E->getExprLoc();
615
616 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
617 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
618 if (DVar.CKind != OMPC_unknown) {
619 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000620 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000621 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000622 return;
623 }
624 // The default(none) clause requires that each variable that is referenced
625 // in the construct, and does not have a predetermined data-sharing
626 // attribute, must have its data-sharing attribute explicitly determined
627 // by being listed in a data-sharing attribute clause.
628 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
629 (DKind == OMPD_parallel || DKind == OMPD_task)) {
630 ErrorFound = true;
631 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
632 return;
633 }
634
635 // OpenMP [2.9.3.6, Restrictions, p.2]
636 // A list item that appears in a reduction clause of the innermost
637 // enclosing worksharing or parallel construct may not be accessed in an
638 // explicit task.
639 // TODO:
640
641 // Define implicit data-sharing attributes for task.
642 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000643 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
644 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000645 }
646 }
647 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
648 for (ArrayRef<OMPClause *>::iterator I = S->clauses().begin(),
649 E = S->clauses().end();
650 I != E; ++I)
651 if (OMPClause *C = *I)
652 for (StmtRange R = C->children(); R; ++R)
653 if (Stmt *Child = *R)
654 Visit(Child);
655 }
656 void VisitStmt(Stmt *S) {
657 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
658 I != E; ++I)
659 if (Stmt *Child = *I)
660 if (!isa<OMPExecutableDirective>(Child))
661 Visit(Child);
662 }
663
664 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000665 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000666
667 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
668 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { }
669};
670}
671
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000672StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
673 ArrayRef<OMPClause *> Clauses,
674 Stmt *AStmt,
675 SourceLocation StartLoc,
676 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000677 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
678
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000679 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000680
681 // Check default data sharing attributes for referenced variables.
682 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
683 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
684 if (DSAChecker.isErrorFound())
685 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000686 // Generate list of implicitly defined firstprivate variables.
687 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
688 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
689
690 bool ErrorFound = false;
691 if (!DSAChecker.getImplicitFirstprivate().empty()) {
692 if (OMPClause *Implicit =
693 ActOnOpenMPFirstprivateClause(DSAChecker.getImplicitFirstprivate(),
694 SourceLocation(), SourceLocation(),
695 SourceLocation())) {
696 ClausesWithImplicit.push_back(Implicit);
697 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
698 DSAChecker.getImplicitFirstprivate().size();
699 } else
700 ErrorFound = true;
701 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000703 switch (Kind) {
704 case OMPD_parallel:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000705 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt,
706 StartLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000707 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000708 case OMPD_simd:
709 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt,
710 StartLoc, EndLoc);
711 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000712 case OMPD_threadprivate:
713 case OMPD_task:
714 llvm_unreachable("OpenMP Directive is not allowed");
715 case OMPD_unknown:
716 case NUM_OPENMP_DIRECTIVES:
717 llvm_unreachable("Unknown OpenMP directive");
718 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000719
720 if (ErrorFound) return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000721 return Res;
722}
723
724StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
725 Stmt *AStmt,
726 SourceLocation StartLoc,
727 SourceLocation EndLoc) {
728 getCurFunction()->setHasBranchProtectedScope();
729
730 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
731 Clauses, AStmt));
732}
733
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000734StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
735 Stmt *AStmt,
736 SourceLocation StartLoc,
737 SourceLocation EndLoc) {
738 Stmt *CStmt = AStmt;
739 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt))
740 CStmt = CS->getCapturedStmt();
741 while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt))
742 CStmt = AS->getSubStmt();
743 ForStmt *For = dyn_cast<ForStmt>(CStmt);
744 if (!For) {
745 Diag(CStmt->getLocStart(), diag::err_omp_not_for)
746 << getOpenMPDirectiveName(OMPD_simd);
747 return StmtError();
748 }
749
750 // FIXME: Checking loop canonical form, collapsing etc.
751
752 getCurFunction()->setHasBranchProtectedScope();
753 return Owned(OMPSimdDirective::Create(Context, StartLoc, EndLoc,
754 Clauses, AStmt));
755}
756
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000757OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
758 Expr *Expr,
759 SourceLocation StartLoc,
760 SourceLocation LParenLoc,
761 SourceLocation EndLoc) {
762 OMPClause *Res = 0;
763 switch (Kind) {
764 case OMPC_if:
765 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
766 break;
Alexey Bataev568a8332014-03-06 06:15:19 +0000767 case OMPC_num_threads:
768 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
769 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000770 case OMPC_safelen:
771 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
772 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000773 case OMPC_default:
774 case OMPC_private:
775 case OMPC_firstprivate:
776 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000777 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000778 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000779 case OMPC_threadprivate:
780 case OMPC_unknown:
781 case NUM_OPENMP_CLAUSES:
782 llvm_unreachable("Clause is not allowed.");
783 }
784 return Res;
785}
786
787OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition,
788 SourceLocation StartLoc,
789 SourceLocation LParenLoc,
790 SourceLocation EndLoc) {
791 Expr *ValExpr = Condition;
792 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
793 !Condition->isInstantiationDependent() &&
794 !Condition->containsUnexpandedParameterPack()) {
795 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
796 Condition->getExprLoc(),
797 Condition);
798 if (Val.isInvalid())
799 return 0;
800
801 ValExpr = Val.take();
802 }
803
804 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
805}
806
Alexey Bataev568a8332014-03-06 06:15:19 +0000807ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
808 Expr *Op) {
809 if (!Op)
810 return ExprError();
811
812 class IntConvertDiagnoser : public ICEConvertDiagnoser {
813 public:
814 IntConvertDiagnoser()
815 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false,
816 false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000817 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
818 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000819 return S.Diag(Loc, diag::err_omp_not_integral) << T;
820 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000821 SemaDiagnosticBuilder diagnoseIncomplete(
822 Sema &S, SourceLocation Loc, QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000823 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
824 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000825 SemaDiagnosticBuilder diagnoseExplicitConv(
826 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000827 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
828 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000829 SemaDiagnosticBuilder noteExplicitConv(
830 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000831 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
832 << ConvTy->isEnumeralType() << ConvTy;
833 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000834 SemaDiagnosticBuilder diagnoseAmbiguous(
835 Sema &S, SourceLocation Loc, QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000836 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
837 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000838 SemaDiagnosticBuilder noteAmbiguous(
839 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000840 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
841 << ConvTy->isEnumeralType() << ConvTy;
842 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000843 SemaDiagnosticBuilder diagnoseConversion(
844 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000845 llvm_unreachable("conversion functions are permitted");
846 }
847 } ConvertDiagnoser;
848 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
849}
850
851OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
852 SourceLocation StartLoc,
853 SourceLocation LParenLoc,
854 SourceLocation EndLoc) {
855 Expr *ValExpr = NumThreads;
856 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
857 !NumThreads->isInstantiationDependent() &&
858 !NumThreads->containsUnexpandedParameterPack()) {
859 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
860 ExprResult Val =
861 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
862 if (Val.isInvalid())
863 return 0;
864
865 ValExpr = Val.take();
866
867 // OpenMP [2.5, Restrictions]
868 // The num_threads expression must evaluate to a positive integer value.
869 llvm::APSInt Result;
870 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
871 Result.isSigned() && !Result.isStrictlyPositive()) {
872 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
873 << "num_threads" << NumThreads->getSourceRange();
874 return 0;
875 }
876 }
877
878 return new (Context) OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc,
879 EndLoc);
880}
881
Alexey Bataev62c87d22014-03-21 04:51:18 +0000882ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
883 OpenMPClauseKind CKind) {
884 if (!E)
885 return ExprError();
886 if (E->isValueDependent() || E->isTypeDependent() ||
887 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
888 return Owned(E);
889 llvm::APSInt Result;
890 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
891 if (ICE.isInvalid())
892 return ExprError();
893 if (!Result.isStrictlyPositive()) {
894 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
895 << getOpenMPClauseName(CKind) << E->getSourceRange();
896 return ExprError();
897 }
898 return ICE;
899}
900
901OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
902 SourceLocation LParenLoc,
903 SourceLocation EndLoc) {
904 // OpenMP [2.8.1, simd construct, Description]
905 // The parameter of the safelen clause must be a constant
906 // positive integer expression.
907 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
908 if (Safelen.isInvalid())
909 return 0;
910 return new (Context)
911 OMPSafelenClause(Safelen.take(), StartLoc, LParenLoc, EndLoc);
912}
913
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000914OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
915 unsigned Argument,
916 SourceLocation ArgumentLoc,
917 SourceLocation StartLoc,
918 SourceLocation LParenLoc,
919 SourceLocation EndLoc) {
920 OMPClause *Res = 0;
921 switch (Kind) {
922 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000923 Res =
924 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
925 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000926 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000927 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000928 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000929 case OMPC_safelen:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000930 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000931 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000932 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000933 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000934 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000935 case OMPC_threadprivate:
936 case OMPC_unknown:
937 case NUM_OPENMP_CLAUSES:
938 llvm_unreachable("Clause is not allowed.");
939 }
940 return Res;
941}
942
943OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
944 SourceLocation KindKwLoc,
945 SourceLocation StartLoc,
946 SourceLocation LParenLoc,
947 SourceLocation EndLoc) {
948 if (Kind == OMPC_DEFAULT_unknown) {
949 std::string Values;
Ted Kremenek725a0972014-03-21 17:34:28 +0000950 static_assert(NUM_OPENMP_DEFAULT_KINDS > 1,
951 "NUM_OPENMP_DEFAULT_KINDS not greater than 1");
952 std::string Sep(", ");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000953 for (unsigned i = OMPC_DEFAULT_unknown + 1;
954 i < NUM_OPENMP_DEFAULT_KINDS; ++i) {
955 Values += "'";
956 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
957 Values += "'";
958 switch (i) {
959 case NUM_OPENMP_DEFAULT_KINDS - 2:
960 Values += " or ";
961 break;
962 case NUM_OPENMP_DEFAULT_KINDS - 1:
963 break;
964 default:
965 Values += Sep;
966 break;
967 }
968 }
969 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
970 << Values << getOpenMPClauseName(OMPC_default);
971 return 0;
972 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000973 switch (Kind) {
974 case OMPC_DEFAULT_none:
975 DSAStack->setDefaultDSANone();
976 break;
977 case OMPC_DEFAULT_shared:
978 DSAStack->setDefaultDSAShared();
979 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000980 case OMPC_DEFAULT_unknown:
981 case NUM_OPENMP_DEFAULT_KINDS:
982 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +0000983 break;
984 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000985 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
986 EndLoc);
987}
988
989OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
990 ArrayRef<Expr *> VarList,
Alexander Musman8dba6642014-04-22 13:09:42 +0000991 Expr *TailExpr,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000992 SourceLocation StartLoc,
993 SourceLocation LParenLoc,
Alexander Musman8dba6642014-04-22 13:09:42 +0000994 SourceLocation ColonLoc,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000995 SourceLocation EndLoc) {
996 OMPClause *Res = 0;
997 switch (Kind) {
998 case OMPC_private:
999 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1000 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001001 case OMPC_firstprivate:
1002 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1003 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001004 case OMPC_shared:
1005 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1006 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001007 case OMPC_linear:
1008 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1009 ColonLoc, EndLoc);
1010 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001011 case OMPC_copyin:
1012 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1013 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001014 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001015 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001016 case OMPC_safelen:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001017 case OMPC_default:
1018 case OMPC_threadprivate:
1019 case OMPC_unknown:
1020 case NUM_OPENMP_CLAUSES:
1021 llvm_unreachable("Clause is not allowed.");
1022 }
1023 return Res;
1024}
1025
1026OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1027 SourceLocation StartLoc,
1028 SourceLocation LParenLoc,
1029 SourceLocation EndLoc) {
1030 SmallVector<Expr *, 8> Vars;
1031 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1032 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001033 assert(*I && "NULL expr in OpenMP private clause.");
1034 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001035 // It will be analyzed later.
1036 Vars.push_back(*I);
1037 continue;
1038 }
1039
1040 SourceLocation ELoc = (*I)->getExprLoc();
1041 // OpenMP [2.1, C/C++]
1042 // A list item is a variable name.
1043 // OpenMP [2.9.3.3, Restrictions, p.1]
1044 // A variable that is part of another variable (as an array or
1045 // structure element) cannot appear in a private clause.
1046 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1047 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1048 Diag(ELoc, diag::err_omp_expected_var_name)
1049 << (*I)->getSourceRange();
1050 continue;
1051 }
1052 Decl *D = DE->getDecl();
1053 VarDecl *VD = cast<VarDecl>(D);
1054
1055 QualType Type = VD->getType();
1056 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1057 // It will be analyzed later.
1058 Vars.push_back(DE);
1059 continue;
1060 }
1061
1062 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1063 // A variable that appears in a private clause must not have an incomplete
1064 // type or a reference type.
1065 if (RequireCompleteType(ELoc, Type,
1066 diag::err_omp_private_incomplete_type)) {
1067 continue;
1068 }
1069 if (Type->isReferenceType()) {
1070 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1071 << getOpenMPClauseName(OMPC_private) << Type;
1072 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1073 VarDecl::DeclarationOnly;
1074 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1075 diag::note_defined_here) << VD;
1076 continue;
1077 }
1078
1079 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1080 // A variable of class type (or array thereof) that appears in a private
1081 // clause requires an accesible, unambiguous default constructor for the
1082 // class type.
1083 while (Type.getNonReferenceType()->isArrayType()) {
1084 Type = cast<ArrayType>(
1085 Type.getNonReferenceType().getTypePtr())->getElementType();
1086 }
1087 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1088 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
1089 if (RD) {
1090 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1091 PartialDiagnostic PD =
1092 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1093 if (!CD ||
1094 CheckConstructorAccess(ELoc, CD,
1095 InitializedEntity::InitializeTemporary(Type),
1096 CD->getAccess(), PD) == AR_inaccessible ||
1097 CD->isDeleted()) {
1098 Diag(ELoc, diag::err_omp_required_method)
1099 << getOpenMPClauseName(OMPC_private) << 0;
1100 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1101 VarDecl::DeclarationOnly;
1102 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1103 diag::note_defined_here) << VD;
1104 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1105 continue;
1106 }
1107 MarkFunctionReferenced(ELoc, CD);
1108 DiagnoseUseOfDecl(CD, ELoc);
1109
1110 CXXDestructorDecl *DD = RD->getDestructor();
1111 if (DD) {
1112 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1113 DD->isDeleted()) {
1114 Diag(ELoc, diag::err_omp_required_method)
1115 << getOpenMPClauseName(OMPC_private) << 4;
1116 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1117 VarDecl::DeclarationOnly;
1118 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1119 diag::note_defined_here) << VD;
1120 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1121 continue;
1122 }
1123 MarkFunctionReferenced(ELoc, DD);
1124 DiagnoseUseOfDecl(DD, ELoc);
1125 }
1126 }
1127
Alexey Bataev758e55e2013-09-06 18:03:48 +00001128 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1129 // in a Construct]
1130 // Variables with the predetermined data-sharing attributes may not be
1131 // listed in data-sharing attributes clauses, except for the cases
1132 // listed below. For these exceptions only, listing a predetermined
1133 // variable in a data-sharing attribute clause is allowed and overrides
1134 // the variable's predetermined data-sharing attributes.
1135 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1136 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
1137 Diag(ELoc, diag::err_omp_wrong_dsa)
1138 << getOpenMPClauseName(DVar.CKind)
1139 << getOpenMPClauseName(OMPC_private);
1140 if (DVar.RefExpr) {
1141 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1142 << getOpenMPClauseName(DVar.CKind);
1143 } else {
1144 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1145 << getOpenMPClauseName(DVar.CKind);
1146 }
1147 continue;
1148 }
1149
1150 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001151 Vars.push_back(DE);
1152 }
1153
1154 if (Vars.empty()) return 0;
1155
1156 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1157}
1158
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001159OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1160 SourceLocation StartLoc,
1161 SourceLocation LParenLoc,
1162 SourceLocation EndLoc) {
1163 SmallVector<Expr *, 8> Vars;
1164 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1165 I != E; ++I) {
1166 assert(*I && "NULL expr in OpenMP firstprivate clause.");
1167 if (isa<DependentScopeDeclRefExpr>(*I)) {
1168 // It will be analyzed later.
1169 Vars.push_back(*I);
1170 continue;
1171 }
1172
1173 SourceLocation ELoc = (*I)->getExprLoc();
1174 // OpenMP [2.1, C/C++]
1175 // A list item is a variable name.
1176 // OpenMP [2.9.3.3, Restrictions, p.1]
1177 // A variable that is part of another variable (as an array or
1178 // structure element) cannot appear in a private clause.
1179 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1180 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1181 Diag(ELoc, diag::err_omp_expected_var_name)
1182 << (*I)->getSourceRange();
1183 continue;
1184 }
1185 Decl *D = DE->getDecl();
1186 VarDecl *VD = cast<VarDecl>(D);
1187
1188 QualType Type = VD->getType();
1189 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1190 // It will be analyzed later.
1191 Vars.push_back(DE);
1192 continue;
1193 }
1194
1195 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1196 // A variable that appears in a private clause must not have an incomplete
1197 // type or a reference type.
1198 if (RequireCompleteType(ELoc, Type,
1199 diag::err_omp_firstprivate_incomplete_type)) {
1200 continue;
1201 }
1202 if (Type->isReferenceType()) {
1203 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1204 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1205 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1206 VarDecl::DeclarationOnly;
1207 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1208 diag::note_defined_here) << VD;
1209 continue;
1210 }
1211
1212 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1213 // A variable of class type (or array thereof) that appears in a private
1214 // clause requires an accesible, unambiguous copy constructor for the
1215 // class type.
1216 Type = Context.getBaseElementType(Type);
1217 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1218 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
1219 if (RD) {
1220 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1221 PartialDiagnostic PD =
1222 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1223 if (!CD ||
1224 CheckConstructorAccess(ELoc, CD,
1225 InitializedEntity::InitializeTemporary(Type),
1226 CD->getAccess(), PD) == AR_inaccessible ||
1227 CD->isDeleted()) {
1228 Diag(ELoc, diag::err_omp_required_method)
1229 << getOpenMPClauseName(OMPC_firstprivate) << 1;
1230 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1231 VarDecl::DeclarationOnly;
1232 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1233 diag::note_defined_here) << VD;
1234 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1235 continue;
1236 }
1237 MarkFunctionReferenced(ELoc, CD);
1238 DiagnoseUseOfDecl(CD, ELoc);
1239
1240 CXXDestructorDecl *DD = RD->getDestructor();
1241 if (DD) {
1242 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1243 DD->isDeleted()) {
1244 Diag(ELoc, diag::err_omp_required_method)
1245 << getOpenMPClauseName(OMPC_firstprivate) << 4;
1246 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1247 VarDecl::DeclarationOnly;
1248 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1249 diag::note_defined_here) << VD;
1250 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1251 continue;
1252 }
1253 MarkFunctionReferenced(ELoc, DD);
1254 DiagnoseUseOfDecl(DD, ELoc);
1255 }
1256 }
1257
1258 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1259 // variable and it was checked already.
1260 if (StartLoc.isValid() && EndLoc.isValid()) {
1261 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1262 Type = Type.getNonReferenceType().getCanonicalType();
1263 bool IsConstant = Type.isConstant(Context);
1264 Type = Context.getBaseElementType(Type);
1265 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1266 // A list item that specifies a given variable may not appear in more
1267 // than one clause on the same directive, except that a variable may be
1268 // specified in both firstprivate and lastprivate clauses.
1269 // TODO: add processing for lastprivate.
1270 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1271 DVar.RefExpr) {
1272 Diag(ELoc, diag::err_omp_wrong_dsa)
1273 << getOpenMPClauseName(DVar.CKind)
1274 << getOpenMPClauseName(OMPC_firstprivate);
1275 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1276 << getOpenMPClauseName(DVar.CKind);
1277 continue;
1278 }
1279
1280 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1281 // in a Construct]
1282 // Variables with the predetermined data-sharing attributes may not be
1283 // listed in data-sharing attributes clauses, except for the cases
1284 // listed below. For these exceptions only, listing a predetermined
1285 // variable in a data-sharing attribute clause is allowed and overrides
1286 // the variable's predetermined data-sharing attributes.
1287 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1288 // in a Construct, C/C++, p.2]
1289 // Variables with const-qualified type having no mutable member may be
1290 // listed in a firstprivate clause, even if they are static data members.
1291 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1292 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1293 Diag(ELoc, diag::err_omp_wrong_dsa)
1294 << getOpenMPClauseName(DVar.CKind)
1295 << getOpenMPClauseName(OMPC_firstprivate);
1296 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1297 << getOpenMPClauseName(DVar.CKind);
1298 continue;
1299 }
1300
1301 // OpenMP [2.9.3.4, Restrictions, p.2]
1302 // A list item that is private within a parallel region must not appear
1303 // in a firstprivate clause on a worksharing construct if any of the
1304 // worksharing regions arising from the worksharing construct ever bind
1305 // to any of the parallel regions arising from the parallel construct.
1306 // OpenMP [2.9.3.4, Restrictions, p.3]
1307 // A list item that appears in a reduction clause of a parallel construct
1308 // must not appear in a firstprivate clause on a worksharing or task
1309 // construct if any of the worksharing or task regions arising from the
1310 // worksharing or task construct ever bind to any of the parallel regions
1311 // arising from the parallel construct.
1312 // OpenMP [2.9.3.4, Restrictions, p.4]
1313 // A list item that appears in a reduction clause in worksharing
1314 // construct must not appear in a firstprivate clause in a task construct
1315 // encountered during execution of any of the worksharing regions arising
1316 // from the worksharing construct.
1317 // TODO:
1318 }
1319
1320 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1321 Vars.push_back(DE);
1322 }
1323
1324 if (Vars.empty()) return 0;
1325
1326 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1327 Vars);
1328}
1329
Alexey Bataev758e55e2013-09-06 18:03:48 +00001330OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1331 SourceLocation StartLoc,
1332 SourceLocation LParenLoc,
1333 SourceLocation EndLoc) {
1334 SmallVector<Expr *, 8> Vars;
1335 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1336 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001337 assert(*I && "NULL expr in OpenMP shared clause.");
1338 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001339 // It will be analyzed later.
1340 Vars.push_back(*I);
1341 continue;
1342 }
1343
1344 SourceLocation ELoc = (*I)->getExprLoc();
1345 // OpenMP [2.1, C/C++]
1346 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001347 // OpenMP [2.14.3.2, Restrictions, p.1]
1348 // A variable that is part of another variable (as an array or structure
1349 // element) cannot appear in a shared unless it is a static data member
1350 // of a C++ class.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001351 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1352 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1353 Diag(ELoc, diag::err_omp_expected_var_name)
1354 << (*I)->getSourceRange();
1355 continue;
1356 }
1357 Decl *D = DE->getDecl();
1358 VarDecl *VD = cast<VarDecl>(D);
1359
1360 QualType Type = VD->getType();
1361 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1362 // It will be analyzed later.
1363 Vars.push_back(DE);
1364 continue;
1365 }
1366
1367 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1368 // in a Construct]
1369 // Variables with the predetermined data-sharing attributes may not be
1370 // listed in data-sharing attributes clauses, except for the cases
1371 // listed below. For these exceptions only, listing a predetermined
1372 // variable in a data-sharing attribute clause is allowed and overrides
1373 // the variable's predetermined data-sharing attributes.
1374 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1375 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1376 Diag(ELoc, diag::err_omp_wrong_dsa)
1377 << getOpenMPClauseName(DVar.CKind)
1378 << getOpenMPClauseName(OMPC_shared);
1379 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1380 << getOpenMPClauseName(DVar.CKind);
1381 continue;
1382 }
1383
1384 DSAStack->addDSA(VD, DE, OMPC_shared);
1385 Vars.push_back(DE);
1386 }
1387
1388 if (Vars.empty()) return 0;
1389
1390 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1391}
1392
Alexander Musman8dba6642014-04-22 13:09:42 +00001393OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1394 SourceLocation StartLoc,
1395 SourceLocation LParenLoc,
1396 SourceLocation ColonLoc,
1397 SourceLocation EndLoc) {
1398 SmallVector<Expr *, 8> Vars;
1399 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1400 I != E; ++I) {
1401 assert(*I && "NULL expr in OpenMP linear clause.");
1402 if (isa<DependentScopeDeclRefExpr>(*I)) {
1403 // It will be analyzed later.
1404 Vars.push_back(*I);
1405 continue;
1406 }
1407
1408 // OpenMP [2.14.3.7, linear clause]
1409 // A list item that appears in a linear clause is subject to the private
1410 // clause semantics described in Section 2.14.3.3 on page 159 except as
1411 // noted. In addition, the value of the new list item on each iteration
1412 // of the associated loop(s) corresponds to the value of the original
1413 // list item before entering the construct plus the logical number of
1414 // the iteration times linear-step.
1415
1416 SourceLocation ELoc = (*I)->getExprLoc();
1417 // OpenMP [2.1, C/C++]
1418 // A list item is a variable name.
1419 // OpenMP [2.14.3.3, Restrictions, p.1]
1420 // A variable that is part of another variable (as an array or
1421 // structure element) cannot appear in a private clause.
1422 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1423 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1424 Diag(ELoc, diag::err_omp_expected_var_name) << (*I)->getSourceRange();
1425 continue;
1426 }
1427
1428 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1429
1430 // OpenMP [2.14.3.7, linear clause]
1431 // A list-item cannot appear in more than one linear clause.
1432 // A list-item that appears in a linear clause cannot appear in any
1433 // other data-sharing attribute clause.
1434 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1435 if (DVar.RefExpr) {
1436 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1437 << getOpenMPClauseName(OMPC_linear);
1438 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1439 << getOpenMPClauseName(DVar.CKind);
1440 continue;
1441 }
1442
1443 QualType QType = VD->getType();
1444 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1445 // It will be analyzed later.
1446 Vars.push_back(DE);
1447 continue;
1448 }
1449
1450 // A variable must not have an incomplete type or a reference type.
1451 if (RequireCompleteType(ELoc, QType,
1452 diag::err_omp_linear_incomplete_type)) {
1453 continue;
1454 }
1455 if (QType->isReferenceType()) {
1456 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1457 << getOpenMPClauseName(OMPC_linear) << QType;
1458 bool IsDecl =
1459 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1460 Diag(VD->getLocation(),
1461 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1462 << VD;
1463 continue;
1464 }
1465
1466 // A list item must not be const-qualified.
1467 if (QType.isConstant(Context)) {
1468 Diag(ELoc, diag::err_omp_const_variable)
1469 << getOpenMPClauseName(OMPC_linear);
1470 bool IsDecl =
1471 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1472 Diag(VD->getLocation(),
1473 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1474 << VD;
1475 continue;
1476 }
1477
1478 // A list item must be of integral or pointer type.
1479 QType = QType.getUnqualifiedType().getCanonicalType();
1480 const Type *Ty = QType.getTypePtrOrNull();
1481 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1482 !Ty->isPointerType())) {
1483 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1484 bool IsDecl =
1485 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1486 Diag(VD->getLocation(),
1487 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1488 << VD;
1489 continue;
1490 }
1491
1492 DSAStack->addDSA(VD, DE, OMPC_linear);
1493 Vars.push_back(DE);
1494 }
1495
1496 if (Vars.empty())
1497 return 0;
1498
1499 Expr *StepExpr = Step;
1500 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1501 !Step->isInstantiationDependent() &&
1502 !Step->containsUnexpandedParameterPack()) {
1503 SourceLocation StepLoc = Step->getLocStart();
1504 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1505 if (Val.isInvalid())
1506 return 0;
1507 StepExpr = Val.take();
1508
1509 // Warn about zero linear step (it would be probably better specified as
1510 // making corresponding variables 'const').
1511 llvm::APSInt Result;
1512 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1513 !Result.isNegative() && !Result.isStrictlyPositive())
1514 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1515 << (Vars.size() > 1);
1516 }
1517
1518 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1519 Vars, StepExpr);
1520}
1521
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001522OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1523 SourceLocation StartLoc,
1524 SourceLocation LParenLoc,
1525 SourceLocation EndLoc) {
1526 SmallVector<Expr *, 8> Vars;
1527 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1528 I != E; ++I) {
1529 assert(*I && "NULL expr in OpenMP copyin clause.");
1530 if (isa<DependentScopeDeclRefExpr>(*I)) {
1531 // It will be analyzed later.
1532 Vars.push_back(*I);
1533 continue;
1534 }
1535
1536 SourceLocation ELoc = (*I)->getExprLoc();
1537 // OpenMP [2.1, C/C++]
1538 // A list item is a variable name.
1539 // OpenMP [2.14.4.1, Restrictions, p.1]
1540 // A list item that appears in a copyin clause must be threadprivate.
1541 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1542 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1543 Diag(ELoc, diag::err_omp_expected_var_name)
1544 << (*I)->getSourceRange();
1545 continue;
1546 }
1547
1548 Decl *D = DE->getDecl();
1549 VarDecl *VD = cast<VarDecl>(D);
1550
1551 QualType Type = VD->getType();
1552 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1553 // It will be analyzed later.
1554 Vars.push_back(DE);
1555 continue;
1556 }
1557
1558 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1559 // A list item that appears in a copyin clause must be threadprivate.
1560 if (!DSAStack->isThreadPrivate(VD)) {
1561 Diag(ELoc, diag::err_omp_required_access)
1562 << getOpenMPClauseName(OMPC_copyin)
1563 << getOpenMPDirectiveName(OMPD_threadprivate);
1564 continue;
1565 }
1566
1567 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1568 // A variable of class type (or array thereof) that appears in a
1569 // copyin clause requires an accesible, unambiguous copy assignment
1570 // operator for the class type.
1571 Type = Context.getBaseElementType(Type);
1572 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1573 Type->getAsCXXRecordDecl() : 0;
1574 if (RD) {
1575 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1576 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
1577 if (!MD ||
1578 CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
1579 MD->isDeleted()) {
1580 Diag(ELoc, diag::err_omp_required_method)
1581 << getOpenMPClauseName(OMPC_copyin) << 2;
1582 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1583 VarDecl::DeclarationOnly;
1584 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1585 diag::note_defined_here) << VD;
1586 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1587 continue;
1588 }
1589 MarkFunctionReferenced(ELoc, MD);
1590 DiagnoseUseOfDecl(MD, ELoc);
1591 }
1592
1593 DSAStack->addDSA(VD, DE, OMPC_copyin);
1594 Vars.push_back(DE);
1595 }
1596
1597 if (Vars.empty()) return 0;
1598
1599 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1600}
1601
Alexey Bataev758e55e2013-09-06 18:03:48 +00001602#undef DSAStack