blob: 441fc56a7fca6c8f51f34030c4d1fd59129e0e63 [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:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000774 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000775 case OMPC_private:
776 case OMPC_firstprivate:
777 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000778 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000779 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000780 case OMPC_threadprivate:
781 case OMPC_unknown:
782 case NUM_OPENMP_CLAUSES:
783 llvm_unreachable("Clause is not allowed.");
784 }
785 return Res;
786}
787
788OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition,
789 SourceLocation StartLoc,
790 SourceLocation LParenLoc,
791 SourceLocation EndLoc) {
792 Expr *ValExpr = Condition;
793 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
794 !Condition->isInstantiationDependent() &&
795 !Condition->containsUnexpandedParameterPack()) {
796 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
797 Condition->getExprLoc(),
798 Condition);
799 if (Val.isInvalid())
800 return 0;
801
802 ValExpr = Val.take();
803 }
804
805 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
806}
807
Alexey Bataev568a8332014-03-06 06:15:19 +0000808ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
809 Expr *Op) {
810 if (!Op)
811 return ExprError();
812
813 class IntConvertDiagnoser : public ICEConvertDiagnoser {
814 public:
815 IntConvertDiagnoser()
816 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false,
817 false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000818 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
819 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000820 return S.Diag(Loc, diag::err_omp_not_integral) << T;
821 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000822 SemaDiagnosticBuilder diagnoseIncomplete(
823 Sema &S, SourceLocation Loc, QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000824 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
825 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000826 SemaDiagnosticBuilder diagnoseExplicitConv(
827 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000828 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
829 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000830 SemaDiagnosticBuilder noteExplicitConv(
831 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000832 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
833 << ConvTy->isEnumeralType() << ConvTy;
834 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000835 SemaDiagnosticBuilder diagnoseAmbiguous(
836 Sema &S, SourceLocation Loc, QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000837 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
838 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000839 SemaDiagnosticBuilder noteAmbiguous(
840 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000841 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
842 << ConvTy->isEnumeralType() << ConvTy;
843 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000844 SemaDiagnosticBuilder diagnoseConversion(
845 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000846 llvm_unreachable("conversion functions are permitted");
847 }
848 } ConvertDiagnoser;
849 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
850}
851
852OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
853 SourceLocation StartLoc,
854 SourceLocation LParenLoc,
855 SourceLocation EndLoc) {
856 Expr *ValExpr = NumThreads;
857 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
858 !NumThreads->isInstantiationDependent() &&
859 !NumThreads->containsUnexpandedParameterPack()) {
860 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
861 ExprResult Val =
862 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
863 if (Val.isInvalid())
864 return 0;
865
866 ValExpr = Val.take();
867
868 // OpenMP [2.5, Restrictions]
869 // The num_threads expression must evaluate to a positive integer value.
870 llvm::APSInt Result;
871 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
872 Result.isSigned() && !Result.isStrictlyPositive()) {
873 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
874 << "num_threads" << NumThreads->getSourceRange();
875 return 0;
876 }
877 }
878
879 return new (Context) OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc,
880 EndLoc);
881}
882
Alexey Bataev62c87d22014-03-21 04:51:18 +0000883ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
884 OpenMPClauseKind CKind) {
885 if (!E)
886 return ExprError();
887 if (E->isValueDependent() || E->isTypeDependent() ||
888 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
889 return Owned(E);
890 llvm::APSInt Result;
891 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
892 if (ICE.isInvalid())
893 return ExprError();
894 if (!Result.isStrictlyPositive()) {
895 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
896 << getOpenMPClauseName(CKind) << E->getSourceRange();
897 return ExprError();
898 }
899 return ICE;
900}
901
902OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
903 SourceLocation LParenLoc,
904 SourceLocation EndLoc) {
905 // OpenMP [2.8.1, simd construct, Description]
906 // The parameter of the safelen clause must be a constant
907 // positive integer expression.
908 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
909 if (Safelen.isInvalid())
910 return 0;
911 return new (Context)
912 OMPSafelenClause(Safelen.take(), StartLoc, LParenLoc, EndLoc);
913}
914
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000915OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
916 unsigned Argument,
917 SourceLocation ArgumentLoc,
918 SourceLocation StartLoc,
919 SourceLocation LParenLoc,
920 SourceLocation EndLoc) {
921 OMPClause *Res = 0;
922 switch (Kind) {
923 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000924 Res =
925 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
926 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000927 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000928 case OMPC_proc_bind:
929 Res =
930 ActOnOpenMPProcBindClause(static_cast<OpenMPProcBindClauseKind>(Argument),
931 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
932 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000933 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000934 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000935 case OMPC_safelen:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000936 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000937 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000938 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000939 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000940 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000941 case OMPC_threadprivate:
942 case OMPC_unknown:
943 case NUM_OPENMP_CLAUSES:
944 llvm_unreachable("Clause is not allowed.");
945 }
946 return Res;
947}
948
949OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
950 SourceLocation KindKwLoc,
951 SourceLocation StartLoc,
952 SourceLocation LParenLoc,
953 SourceLocation EndLoc) {
954 if (Kind == OMPC_DEFAULT_unknown) {
955 std::string Values;
Ted Kremenek725a0972014-03-21 17:34:28 +0000956 static_assert(NUM_OPENMP_DEFAULT_KINDS > 1,
957 "NUM_OPENMP_DEFAULT_KINDS not greater than 1");
958 std::string Sep(", ");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000959 for (unsigned i = OMPC_DEFAULT_unknown + 1;
960 i < NUM_OPENMP_DEFAULT_KINDS; ++i) {
961 Values += "'";
962 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
963 Values += "'";
964 switch (i) {
965 case NUM_OPENMP_DEFAULT_KINDS - 2:
966 Values += " or ";
967 break;
968 case NUM_OPENMP_DEFAULT_KINDS - 1:
969 break;
970 default:
971 Values += Sep;
972 break;
973 }
974 }
975 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
976 << Values << getOpenMPClauseName(OMPC_default);
977 return 0;
978 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000979 switch (Kind) {
980 case OMPC_DEFAULT_none:
981 DSAStack->setDefaultDSANone();
982 break;
983 case OMPC_DEFAULT_shared:
984 DSAStack->setDefaultDSAShared();
985 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000986 case OMPC_DEFAULT_unknown:
987 case NUM_OPENMP_DEFAULT_KINDS:
988 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +0000989 break;
990 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000991 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
992 EndLoc);
993}
994
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000995OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
996 SourceLocation KindKwLoc,
997 SourceLocation StartLoc,
998 SourceLocation LParenLoc,
999 SourceLocation EndLoc) {
1000 if (Kind == OMPC_PROC_BIND_unknown) {
1001 std::string Values;
1002 std::string Sep(", ");
1003 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1004 Values += "'";
1005 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1006 Values += "'";
1007 switch (i) {
1008 case OMPC_PROC_BIND_unknown - 2:
1009 Values += " or ";
1010 break;
1011 case OMPC_PROC_BIND_unknown - 1:
1012 break;
1013 default:
1014 Values += Sep;
1015 break;
1016 }
1017 }
1018 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
1019 << Values << getOpenMPClauseName(OMPC_proc_bind);
1020 return 0;
1021 }
1022 return new (Context) OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc,
1023 EndLoc);
1024}
1025
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001026OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
1027 ArrayRef<Expr *> VarList,
Alexander Musman8dba6642014-04-22 13:09:42 +00001028 Expr *TailExpr,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001029 SourceLocation StartLoc,
1030 SourceLocation LParenLoc,
Alexander Musman8dba6642014-04-22 13:09:42 +00001031 SourceLocation ColonLoc,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001032 SourceLocation EndLoc) {
1033 OMPClause *Res = 0;
1034 switch (Kind) {
1035 case OMPC_private:
1036 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1037 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001038 case OMPC_firstprivate:
1039 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1040 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001041 case OMPC_shared:
1042 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1043 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001044 case OMPC_linear:
1045 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1046 ColonLoc, EndLoc);
1047 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001048 case OMPC_copyin:
1049 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1050 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001051 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001052 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001053 case OMPC_safelen:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001054 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001055 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001056 case OMPC_threadprivate:
1057 case OMPC_unknown:
1058 case NUM_OPENMP_CLAUSES:
1059 llvm_unreachable("Clause is not allowed.");
1060 }
1061 return Res;
1062}
1063
1064OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1065 SourceLocation StartLoc,
1066 SourceLocation LParenLoc,
1067 SourceLocation EndLoc) {
1068 SmallVector<Expr *, 8> Vars;
1069 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1070 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001071 assert(*I && "NULL expr in OpenMP private clause.");
1072 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001073 // It will be analyzed later.
1074 Vars.push_back(*I);
1075 continue;
1076 }
1077
1078 SourceLocation ELoc = (*I)->getExprLoc();
1079 // OpenMP [2.1, C/C++]
1080 // A list item is a variable name.
1081 // OpenMP [2.9.3.3, Restrictions, p.1]
1082 // A variable that is part of another variable (as an array or
1083 // structure element) cannot appear in a private clause.
1084 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1085 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1086 Diag(ELoc, diag::err_omp_expected_var_name)
1087 << (*I)->getSourceRange();
1088 continue;
1089 }
1090 Decl *D = DE->getDecl();
1091 VarDecl *VD = cast<VarDecl>(D);
1092
1093 QualType Type = VD->getType();
1094 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1095 // It will be analyzed later.
1096 Vars.push_back(DE);
1097 continue;
1098 }
1099
1100 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1101 // A variable that appears in a private clause must not have an incomplete
1102 // type or a reference type.
1103 if (RequireCompleteType(ELoc, Type,
1104 diag::err_omp_private_incomplete_type)) {
1105 continue;
1106 }
1107 if (Type->isReferenceType()) {
1108 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1109 << getOpenMPClauseName(OMPC_private) << Type;
1110 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1111 VarDecl::DeclarationOnly;
1112 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1113 diag::note_defined_here) << VD;
1114 continue;
1115 }
1116
1117 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1118 // A variable of class type (or array thereof) that appears in a private
1119 // clause requires an accesible, unambiguous default constructor for the
1120 // class type.
1121 while (Type.getNonReferenceType()->isArrayType()) {
1122 Type = cast<ArrayType>(
1123 Type.getNonReferenceType().getTypePtr())->getElementType();
1124 }
1125 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1126 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
1127 if (RD) {
1128 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1129 PartialDiagnostic PD =
1130 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1131 if (!CD ||
1132 CheckConstructorAccess(ELoc, CD,
1133 InitializedEntity::InitializeTemporary(Type),
1134 CD->getAccess(), PD) == AR_inaccessible ||
1135 CD->isDeleted()) {
1136 Diag(ELoc, diag::err_omp_required_method)
1137 << getOpenMPClauseName(OMPC_private) << 0;
1138 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1139 VarDecl::DeclarationOnly;
1140 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1141 diag::note_defined_here) << VD;
1142 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1143 continue;
1144 }
1145 MarkFunctionReferenced(ELoc, CD);
1146 DiagnoseUseOfDecl(CD, ELoc);
1147
1148 CXXDestructorDecl *DD = RD->getDestructor();
1149 if (DD) {
1150 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1151 DD->isDeleted()) {
1152 Diag(ELoc, diag::err_omp_required_method)
1153 << getOpenMPClauseName(OMPC_private) << 4;
1154 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1155 VarDecl::DeclarationOnly;
1156 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1157 diag::note_defined_here) << VD;
1158 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1159 continue;
1160 }
1161 MarkFunctionReferenced(ELoc, DD);
1162 DiagnoseUseOfDecl(DD, ELoc);
1163 }
1164 }
1165
Alexey Bataev758e55e2013-09-06 18:03:48 +00001166 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1167 // in a Construct]
1168 // Variables with the predetermined data-sharing attributes may not be
1169 // listed in data-sharing attributes clauses, except for the cases
1170 // listed below. For these exceptions only, listing a predetermined
1171 // variable in a data-sharing attribute clause is allowed and overrides
1172 // the variable's predetermined data-sharing attributes.
1173 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1174 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
1175 Diag(ELoc, diag::err_omp_wrong_dsa)
1176 << getOpenMPClauseName(DVar.CKind)
1177 << getOpenMPClauseName(OMPC_private);
1178 if (DVar.RefExpr) {
1179 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1180 << getOpenMPClauseName(DVar.CKind);
1181 } else {
1182 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1183 << getOpenMPClauseName(DVar.CKind);
1184 }
1185 continue;
1186 }
1187
1188 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001189 Vars.push_back(DE);
1190 }
1191
1192 if (Vars.empty()) return 0;
1193
1194 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1195}
1196
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001197OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1198 SourceLocation StartLoc,
1199 SourceLocation LParenLoc,
1200 SourceLocation EndLoc) {
1201 SmallVector<Expr *, 8> Vars;
1202 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1203 I != E; ++I) {
1204 assert(*I && "NULL expr in OpenMP firstprivate clause.");
1205 if (isa<DependentScopeDeclRefExpr>(*I)) {
1206 // It will be analyzed later.
1207 Vars.push_back(*I);
1208 continue;
1209 }
1210
1211 SourceLocation ELoc = (*I)->getExprLoc();
1212 // OpenMP [2.1, C/C++]
1213 // A list item is a variable name.
1214 // OpenMP [2.9.3.3, Restrictions, p.1]
1215 // A variable that is part of another variable (as an array or
1216 // structure element) cannot appear in a private clause.
1217 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1218 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1219 Diag(ELoc, diag::err_omp_expected_var_name)
1220 << (*I)->getSourceRange();
1221 continue;
1222 }
1223 Decl *D = DE->getDecl();
1224 VarDecl *VD = cast<VarDecl>(D);
1225
1226 QualType Type = VD->getType();
1227 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1228 // It will be analyzed later.
1229 Vars.push_back(DE);
1230 continue;
1231 }
1232
1233 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1234 // A variable that appears in a private clause must not have an incomplete
1235 // type or a reference type.
1236 if (RequireCompleteType(ELoc, Type,
1237 diag::err_omp_firstprivate_incomplete_type)) {
1238 continue;
1239 }
1240 if (Type->isReferenceType()) {
1241 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1242 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1243 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1244 VarDecl::DeclarationOnly;
1245 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1246 diag::note_defined_here) << VD;
1247 continue;
1248 }
1249
1250 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1251 // A variable of class type (or array thereof) that appears in a private
1252 // clause requires an accesible, unambiguous copy constructor for the
1253 // class type.
1254 Type = Context.getBaseElementType(Type);
1255 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1256 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
1257 if (RD) {
1258 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1259 PartialDiagnostic PD =
1260 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1261 if (!CD ||
1262 CheckConstructorAccess(ELoc, CD,
1263 InitializedEntity::InitializeTemporary(Type),
1264 CD->getAccess(), PD) == AR_inaccessible ||
1265 CD->isDeleted()) {
1266 Diag(ELoc, diag::err_omp_required_method)
1267 << getOpenMPClauseName(OMPC_firstprivate) << 1;
1268 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1269 VarDecl::DeclarationOnly;
1270 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1271 diag::note_defined_here) << VD;
1272 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1273 continue;
1274 }
1275 MarkFunctionReferenced(ELoc, CD);
1276 DiagnoseUseOfDecl(CD, ELoc);
1277
1278 CXXDestructorDecl *DD = RD->getDestructor();
1279 if (DD) {
1280 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1281 DD->isDeleted()) {
1282 Diag(ELoc, diag::err_omp_required_method)
1283 << getOpenMPClauseName(OMPC_firstprivate) << 4;
1284 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1285 VarDecl::DeclarationOnly;
1286 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1287 diag::note_defined_here) << VD;
1288 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1289 continue;
1290 }
1291 MarkFunctionReferenced(ELoc, DD);
1292 DiagnoseUseOfDecl(DD, ELoc);
1293 }
1294 }
1295
1296 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1297 // variable and it was checked already.
1298 if (StartLoc.isValid() && EndLoc.isValid()) {
1299 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1300 Type = Type.getNonReferenceType().getCanonicalType();
1301 bool IsConstant = Type.isConstant(Context);
1302 Type = Context.getBaseElementType(Type);
1303 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1304 // A list item that specifies a given variable may not appear in more
1305 // than one clause on the same directive, except that a variable may be
1306 // specified in both firstprivate and lastprivate clauses.
1307 // TODO: add processing for lastprivate.
1308 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1309 DVar.RefExpr) {
1310 Diag(ELoc, diag::err_omp_wrong_dsa)
1311 << getOpenMPClauseName(DVar.CKind)
1312 << getOpenMPClauseName(OMPC_firstprivate);
1313 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1314 << getOpenMPClauseName(DVar.CKind);
1315 continue;
1316 }
1317
1318 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1319 // in a Construct]
1320 // Variables with the predetermined data-sharing attributes may not be
1321 // listed in data-sharing attributes clauses, except for the cases
1322 // listed below. For these exceptions only, listing a predetermined
1323 // variable in a data-sharing attribute clause is allowed and overrides
1324 // the variable's predetermined data-sharing attributes.
1325 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1326 // in a Construct, C/C++, p.2]
1327 // Variables with const-qualified type having no mutable member may be
1328 // listed in a firstprivate clause, even if they are static data members.
1329 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1330 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1331 Diag(ELoc, diag::err_omp_wrong_dsa)
1332 << getOpenMPClauseName(DVar.CKind)
1333 << getOpenMPClauseName(OMPC_firstprivate);
1334 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1335 << getOpenMPClauseName(DVar.CKind);
1336 continue;
1337 }
1338
1339 // OpenMP [2.9.3.4, Restrictions, p.2]
1340 // A list item that is private within a parallel region must not appear
1341 // in a firstprivate clause on a worksharing construct if any of the
1342 // worksharing regions arising from the worksharing construct ever bind
1343 // to any of the parallel regions arising from the parallel construct.
1344 // OpenMP [2.9.3.4, Restrictions, p.3]
1345 // A list item that appears in a reduction clause of a parallel construct
1346 // must not appear in a firstprivate clause on a worksharing or task
1347 // construct if any of the worksharing or task regions arising from the
1348 // worksharing or task construct ever bind to any of the parallel regions
1349 // arising from the parallel construct.
1350 // OpenMP [2.9.3.4, Restrictions, p.4]
1351 // A list item that appears in a reduction clause in worksharing
1352 // construct must not appear in a firstprivate clause in a task construct
1353 // encountered during execution of any of the worksharing regions arising
1354 // from the worksharing construct.
1355 // TODO:
1356 }
1357
1358 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1359 Vars.push_back(DE);
1360 }
1361
1362 if (Vars.empty()) return 0;
1363
1364 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1365 Vars);
1366}
1367
Alexey Bataev758e55e2013-09-06 18:03:48 +00001368OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1369 SourceLocation StartLoc,
1370 SourceLocation LParenLoc,
1371 SourceLocation EndLoc) {
1372 SmallVector<Expr *, 8> Vars;
1373 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1374 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001375 assert(*I && "NULL expr in OpenMP shared clause.");
1376 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377 // It will be analyzed later.
1378 Vars.push_back(*I);
1379 continue;
1380 }
1381
1382 SourceLocation ELoc = (*I)->getExprLoc();
1383 // OpenMP [2.1, C/C++]
1384 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001385 // OpenMP [2.14.3.2, Restrictions, p.1]
1386 // A variable that is part of another variable (as an array or structure
1387 // element) cannot appear in a shared unless it is a static data member
1388 // of a C++ class.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001389 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1390 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1391 Diag(ELoc, diag::err_omp_expected_var_name)
1392 << (*I)->getSourceRange();
1393 continue;
1394 }
1395 Decl *D = DE->getDecl();
1396 VarDecl *VD = cast<VarDecl>(D);
1397
1398 QualType Type = VD->getType();
1399 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1400 // It will be analyzed later.
1401 Vars.push_back(DE);
1402 continue;
1403 }
1404
1405 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1406 // in a Construct]
1407 // Variables with the predetermined data-sharing attributes may not be
1408 // listed in data-sharing attributes clauses, except for the cases
1409 // listed below. For these exceptions only, listing a predetermined
1410 // variable in a data-sharing attribute clause is allowed and overrides
1411 // the variable's predetermined data-sharing attributes.
1412 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1413 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1414 Diag(ELoc, diag::err_omp_wrong_dsa)
1415 << getOpenMPClauseName(DVar.CKind)
1416 << getOpenMPClauseName(OMPC_shared);
1417 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1418 << getOpenMPClauseName(DVar.CKind);
1419 continue;
1420 }
1421
1422 DSAStack->addDSA(VD, DE, OMPC_shared);
1423 Vars.push_back(DE);
1424 }
1425
1426 if (Vars.empty()) return 0;
1427
1428 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1429}
1430
Alexander Musman8dba6642014-04-22 13:09:42 +00001431OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1432 SourceLocation StartLoc,
1433 SourceLocation LParenLoc,
1434 SourceLocation ColonLoc,
1435 SourceLocation EndLoc) {
1436 SmallVector<Expr *, 8> Vars;
1437 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1438 I != E; ++I) {
1439 assert(*I && "NULL expr in OpenMP linear clause.");
1440 if (isa<DependentScopeDeclRefExpr>(*I)) {
1441 // It will be analyzed later.
1442 Vars.push_back(*I);
1443 continue;
1444 }
1445
1446 // OpenMP [2.14.3.7, linear clause]
1447 // A list item that appears in a linear clause is subject to the private
1448 // clause semantics described in Section 2.14.3.3 on page 159 except as
1449 // noted. In addition, the value of the new list item on each iteration
1450 // of the associated loop(s) corresponds to the value of the original
1451 // list item before entering the construct plus the logical number of
1452 // the iteration times linear-step.
1453
1454 SourceLocation ELoc = (*I)->getExprLoc();
1455 // OpenMP [2.1, C/C++]
1456 // A list item is a variable name.
1457 // OpenMP [2.14.3.3, Restrictions, p.1]
1458 // A variable that is part of another variable (as an array or
1459 // structure element) cannot appear in a private clause.
1460 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1461 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1462 Diag(ELoc, diag::err_omp_expected_var_name) << (*I)->getSourceRange();
1463 continue;
1464 }
1465
1466 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1467
1468 // OpenMP [2.14.3.7, linear clause]
1469 // A list-item cannot appear in more than one linear clause.
1470 // A list-item that appears in a linear clause cannot appear in any
1471 // other data-sharing attribute clause.
1472 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1473 if (DVar.RefExpr) {
1474 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1475 << getOpenMPClauseName(OMPC_linear);
1476 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1477 << getOpenMPClauseName(DVar.CKind);
1478 continue;
1479 }
1480
1481 QualType QType = VD->getType();
1482 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1483 // It will be analyzed later.
1484 Vars.push_back(DE);
1485 continue;
1486 }
1487
1488 // A variable must not have an incomplete type or a reference type.
1489 if (RequireCompleteType(ELoc, QType,
1490 diag::err_omp_linear_incomplete_type)) {
1491 continue;
1492 }
1493 if (QType->isReferenceType()) {
1494 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1495 << getOpenMPClauseName(OMPC_linear) << QType;
1496 bool IsDecl =
1497 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1498 Diag(VD->getLocation(),
1499 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1500 << VD;
1501 continue;
1502 }
1503
1504 // A list item must not be const-qualified.
1505 if (QType.isConstant(Context)) {
1506 Diag(ELoc, diag::err_omp_const_variable)
1507 << getOpenMPClauseName(OMPC_linear);
1508 bool IsDecl =
1509 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1510 Diag(VD->getLocation(),
1511 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1512 << VD;
1513 continue;
1514 }
1515
1516 // A list item must be of integral or pointer type.
1517 QType = QType.getUnqualifiedType().getCanonicalType();
1518 const Type *Ty = QType.getTypePtrOrNull();
1519 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1520 !Ty->isPointerType())) {
1521 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1522 bool IsDecl =
1523 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1524 Diag(VD->getLocation(),
1525 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1526 << VD;
1527 continue;
1528 }
1529
1530 DSAStack->addDSA(VD, DE, OMPC_linear);
1531 Vars.push_back(DE);
1532 }
1533
1534 if (Vars.empty())
1535 return 0;
1536
1537 Expr *StepExpr = Step;
1538 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1539 !Step->isInstantiationDependent() &&
1540 !Step->containsUnexpandedParameterPack()) {
1541 SourceLocation StepLoc = Step->getLocStart();
1542 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1543 if (Val.isInvalid())
1544 return 0;
1545 StepExpr = Val.take();
1546
1547 // Warn about zero linear step (it would be probably better specified as
1548 // making corresponding variables 'const').
1549 llvm::APSInt Result;
1550 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1551 !Result.isNegative() && !Result.isStrictlyPositive())
1552 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1553 << (Vars.size() > 1);
1554 }
1555
1556 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1557 Vars, StepExpr);
1558}
1559
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001560OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1561 SourceLocation StartLoc,
1562 SourceLocation LParenLoc,
1563 SourceLocation EndLoc) {
1564 SmallVector<Expr *, 8> Vars;
1565 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1566 I != E; ++I) {
1567 assert(*I && "NULL expr in OpenMP copyin clause.");
1568 if (isa<DependentScopeDeclRefExpr>(*I)) {
1569 // It will be analyzed later.
1570 Vars.push_back(*I);
1571 continue;
1572 }
1573
1574 SourceLocation ELoc = (*I)->getExprLoc();
1575 // OpenMP [2.1, C/C++]
1576 // A list item is a variable name.
1577 // OpenMP [2.14.4.1, Restrictions, p.1]
1578 // A list item that appears in a copyin clause must be threadprivate.
1579 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1580 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1581 Diag(ELoc, diag::err_omp_expected_var_name)
1582 << (*I)->getSourceRange();
1583 continue;
1584 }
1585
1586 Decl *D = DE->getDecl();
1587 VarDecl *VD = cast<VarDecl>(D);
1588
1589 QualType Type = VD->getType();
1590 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1591 // It will be analyzed later.
1592 Vars.push_back(DE);
1593 continue;
1594 }
1595
1596 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1597 // A list item that appears in a copyin clause must be threadprivate.
1598 if (!DSAStack->isThreadPrivate(VD)) {
1599 Diag(ELoc, diag::err_omp_required_access)
1600 << getOpenMPClauseName(OMPC_copyin)
1601 << getOpenMPDirectiveName(OMPD_threadprivate);
1602 continue;
1603 }
1604
1605 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1606 // A variable of class type (or array thereof) that appears in a
1607 // copyin clause requires an accesible, unambiguous copy assignment
1608 // operator for the class type.
1609 Type = Context.getBaseElementType(Type);
1610 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1611 Type->getAsCXXRecordDecl() : 0;
1612 if (RD) {
1613 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1614 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
1615 if (!MD ||
1616 CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
1617 MD->isDeleted()) {
1618 Diag(ELoc, diag::err_omp_required_method)
1619 << getOpenMPClauseName(OMPC_copyin) << 2;
1620 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1621 VarDecl::DeclarationOnly;
1622 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1623 diag::note_defined_here) << VD;
1624 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1625 continue;
1626 }
1627 MarkFunctionReferenced(ELoc, MD);
1628 DiagnoseUseOfDecl(MD, ELoc);
1629 }
1630
1631 DSAStack->addDSA(VD, DE, OMPC_copyin);
1632 Vars.push_back(DE);
1633 }
1634
1635 if (Vars.empty()) return 0;
1636
1637 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1638}
1639
Alexey Bataev758e55e2013-09-06 18:03:48 +00001640#undef DSAStack