blob: a377815ffaf11196a671f90805af3bb6b3b8429c [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 Bataeva769e072013-03-22 06:34:35 +000022#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000023#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000024#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000025#include "clang/Sema/Scope.h"
26#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000027#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028using namespace clang;
29
Alexey Bataev758e55e2013-09-06 18:03:48 +000030//===----------------------------------------------------------------------===//
31// Stack of data-sharing attributes for variables
32//===----------------------------------------------------------------------===//
33
34namespace {
35/// \brief Default data sharing attributes, which can be applied to directive.
36enum DefaultDataSharingAttributes {
37 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
38 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
39 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
40};
41
42/// \brief Stack for tracking declarations used in OpenMP directives and
43/// clauses and their data-sharing attributes.
44class DSAStackTy {
45public:
46 struct DSAVarData {
47 OpenMPDirectiveKind DKind;
48 OpenMPClauseKind CKind;
49 DeclRefExpr *RefExpr;
50 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(0) { }
51 };
52private:
53 struct DSAInfo {
54 OpenMPClauseKind Attributes;
55 DeclRefExpr *RefExpr;
56 };
57 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
58
59 struct SharingMapTy {
60 DeclSAMapTy SharingMap;
61 DefaultDataSharingAttributes DefaultAttr;
62 OpenMPDirectiveKind Directive;
63 DeclarationNameInfo DirectiveName;
64 Scope *CurScope;
65 SharingMapTy(OpenMPDirectiveKind DKind,
66 const DeclarationNameInfo &Name,
67 Scope *CurScope)
68 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
69 DirectiveName(Name), CurScope(CurScope) { }
70 SharingMapTy()
71 : SharingMap(), DefaultAttr(DSA_unspecified),
72 Directive(OMPD_unknown), DirectiveName(),
73 CurScope(0) { }
74 };
75
76 typedef SmallVector<SharingMapTy, 64> StackTy;
77
78 /// \brief Stack of used declaration and their data-sharing attributes.
79 StackTy Stack;
80 Sema &Actions;
81
82 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
83
84 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +000085
86 /// \brief Checks if the variable is a local for OpenMP region.
87 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataev758e55e2013-09-06 18:03:48 +000088public:
89 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) { }
90
91 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
92 Scope *CurScope) {
93 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
94 }
95
96 void pop() {
97 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
98 Stack.pop_back();
99 }
100
101 /// \brief Adds explicit data sharing attribute to the specified declaration.
102 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
103
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 /// \brief Returns data sharing attributes from top of the stack for the
105 /// specified declaration.
106 DSAVarData getTopDSA(VarDecl *D);
107 /// \brief Returns data-sharing attributes for the specified declaration.
108 DSAVarData getImplicitDSA(VarDecl *D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000109 /// \brief Checks if the specified variables has \a CKind data-sharing
110 /// attribute in \a DKind directive.
111 DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
112 OpenMPDirectiveKind DKind = OMPD_unknown);
113
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114
115 /// \brief Returns currently analyzed directive.
116 OpenMPDirectiveKind getCurrentDirective() const {
117 return Stack.back().Directive;
118 }
119
120 /// \brief Set default data sharing attribute to none.
121 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
122 /// \brief Set default data sharing attribute to shared.
123 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
124
125 DefaultDataSharingAttributes getDefaultDSA() const {
126 return Stack.back().DefaultAttr;
127 }
128
129 Scope *getCurScope() { return Stack.back().CurScope; }
130};
131} // end anonymous namespace.
132
133DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
134 VarDecl *D) {
135 DSAVarData DVar;
136 if (Iter == Stack.rend() - 1) {
137 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
138 // in a region but not in construct]
139 // File-scope or namespace-scope variables referenced in called routines
140 // in the region are shared unless they appear in a threadprivate
141 // directive.
142 // TODO
143 if (!D->isFunctionOrMethodVarDecl())
144 DVar.CKind = OMPC_shared;
145
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000146 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
147 // in a region but not in construct]
148 // Variables with static storage duration that are declared in called
149 // routines in the region are shared.
150 if (D->hasGlobalStorage())
151 DVar.CKind = OMPC_shared;
152
Alexey Bataev758e55e2013-09-06 18:03:48 +0000153 return DVar;
154 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000155
Alexey Bataev758e55e2013-09-06 18:03:48 +0000156 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000157 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
158 // in a Construct, C/C++, predetermined, p.1]
159 // Variables with automatic storage duration that are declared in a scope
160 // inside the construct are private.
161 if (DVar.DKind != OMPD_parallel) {
162 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
163 (D->getStorageClass() == SC_Auto ||
164 D->getStorageClass() == SC_None)) {
165 DVar.CKind = OMPC_private;
166 return DVar;
167 }
168 }
169
Alexey Bataev758e55e2013-09-06 18:03:48 +0000170 // Explicitly specified attributes and local variables with predetermined
171 // attributes.
172 if (Iter->SharingMap.count(D)) {
173 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
174 DVar.CKind = Iter->SharingMap[D].Attributes;
175 return DVar;
176 }
177
178 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
179 // in a Construct, C/C++, implicitly determined, p.1]
180 // In a parallel or task construct, the data-sharing attributes of these
181 // variables are determined by the default clause, if present.
182 switch (Iter->DefaultAttr) {
183 case DSA_shared:
184 DVar.CKind = OMPC_shared;
185 return DVar;
186 case DSA_none:
187 return DVar;
188 case DSA_unspecified:
189 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
190 // in a Construct, implicitly determined, p.2]
191 // In a parallel construct, if no default clause is present, these
192 // variables are shared.
193 if (DVar.DKind == OMPD_parallel) {
194 DVar.CKind = OMPC_shared;
195 return DVar;
196 }
197
198 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
199 // in a Construct, implicitly determined, p.4]
200 // In a task construct, if no default clause is present, a variable that in
201 // the enclosing context is determined to be shared by all implicit tasks
202 // bound to the current team is shared.
203 // TODO
204 if (DVar.DKind == OMPD_task) {
205 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000206 for (StackTy::reverse_iterator I = std::next(Iter),
207 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000208 I != EE; ++I) {
209 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
210 // in a Construct, implicitly determined, p.6]
211 // In a task construct, if no default clause is present, a variable
212 // whose data-sharing attribute is not determined by the rules above is
213 // firstprivate.
214 DVarTemp = getDSA(I, D);
215 if (DVarTemp.CKind != OMPC_shared) {
216 DVar.RefExpr = 0;
217 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000218 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000219 return DVar;
220 }
221 if (I->Directive == OMPD_parallel) break;
222 }
223 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000224 DVar.CKind =
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000225 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000226 return DVar;
227 }
228 }
229 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
230 // in a Construct, implicitly determined, p.3]
231 // For constructs other than task, if no default clause is present, these
232 // variables inherit their data-sharing attributes from the enclosing
233 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000234 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000235}
236
237void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
238 if (A == OMPC_threadprivate) {
239 Stack[0].SharingMap[D].Attributes = A;
240 Stack[0].SharingMap[D].RefExpr = E;
241 } else {
242 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
243 Stack.back().SharingMap[D].Attributes = A;
244 Stack.back().SharingMap[D].RefExpr = E;
245 }
246}
247
Alexey Bataevec3da872014-01-31 05:15:34 +0000248bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
249 if (Stack.size() > 2) {
250 reverse_iterator I = Iter, E = Stack.rend() - 1;
251 Scope *TopScope = 0;
252 while (I != E &&
253 I->Directive != OMPD_parallel) {
254 ++I;
255 }
256 if (I == E) return false;
257 TopScope = I->CurScope ? I->CurScope->getParent() : 0;
258 Scope *CurScope = getCurScope();
259 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000261 }
262 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000263 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000264 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000265}
266
267DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
268 DSAVarData DVar;
269
270 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
271 // in a Construct, C/C++, predetermined, p.1]
272 // Variables appearing in threadprivate directives are threadprivate.
273 if (D->getTLSKind() != VarDecl::TLS_None) {
274 DVar.CKind = OMPC_threadprivate;
275 return DVar;
276 }
277 if (Stack[0].SharingMap.count(D)) {
278 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
279 DVar.CKind = OMPC_threadprivate;
280 return DVar;
281 }
282
283 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
284 // in a Construct, C/C++, predetermined, p.1]
285 // Variables with automatic storage duration that are declared in a scope
286 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000287 OpenMPDirectiveKind Kind = getCurrentDirective();
288 if (Kind != OMPD_parallel) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000289 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataevec3da872014-01-31 05:15:34 +0000290 (D->getStorageClass() == SC_Auto ||
291 D->getStorageClass() == SC_None))
292 DVar.CKind = OMPC_private;
293 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000294 }
295
296 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
297 // in a Construct, C/C++, predetermined, p.4]
298 // Static data memebers are shared.
299 if (D->isStaticDataMember()) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000300 // Variables with const-qualified type having no mutable member may be listed
301 // in a firstprivate clause, even if they are static data members.
302 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
303 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
304 return DVar;
305
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306 DVar.CKind = OMPC_shared;
307 return DVar;
308 }
309
310 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
311 bool IsConstant = Type.isConstant(Actions.getASTContext());
312 while (Type->isArrayType()) {
313 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
314 Type = ElemType.getNonReferenceType().getCanonicalType();
315 }
316 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
317 // in a Construct, C/C++, predetermined, p.6]
318 // Variables with const qualified type having no mutable member are
319 // shared.
320 CXXRecordDecl *RD = Actions.getLangOpts().CPlusPlus ?
321 Type->getAsCXXRecordDecl() : 0;
322 if (IsConstant &&
323 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
324 // Variables with const-qualified type having no mutable member may be
325 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000326 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
327 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
328 return DVar;
329
Alexey Bataev758e55e2013-09-06 18:03:48 +0000330 DVar.CKind = OMPC_shared;
331 return DVar;
332 }
333
334 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
335 // in a Construct, C/C++, predetermined, p.7]
336 // Variables with static storage duration that are declared in a scope
337 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000338 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000339 DVar.CKind = OMPC_shared;
340 return DVar;
341 }
342
343 // Explicitly specified attributes and local variables with predetermined
344 // attributes.
345 if (Stack.back().SharingMap.count(D)) {
346 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
347 DVar.CKind = Stack.back().SharingMap[D].Attributes;
348 }
349
350 return DVar;
351}
352
353DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000354 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000355}
356
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000357DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
358 OpenMPDirectiveKind DKind) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000359 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
360 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000361 I != E; ++I) {
362 if (DKind != OMPD_unknown && DKind != I->Directive) continue;
363 DSAVarData DVar = getDSA(I, D);
364 if (DVar.CKind == CKind)
365 return DVar;
366 }
367 return DSAVarData();
368}
369
Alexey Bataev758e55e2013-09-06 18:03:48 +0000370void Sema::InitDataSharingAttributesStack() {
371 VarDataSharingAttributesStack = new DSAStackTy(*this);
372}
373
374#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
375
376void Sema::DestroyDataSharingAttributesStack() {
377 delete DSAStack;
378}
379
380void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
381 const DeclarationNameInfo &DirName,
382 Scope *CurScope) {
383 DSAStack->push(DKind, DirName, CurScope);
384 PushExpressionEvaluationContext(PotentiallyEvaluated);
385}
386
387void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
388 DSAStack->pop();
389 DiscardCleanupsInEvaluationContext();
390 PopExpressionEvaluationContext();
391}
392
Alexey Bataeva769e072013-03-22 06:34:35 +0000393namespace {
394
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000395class VarDeclFilterCCC : public CorrectionCandidateCallback {
396private:
397 Sema &Actions;
398public:
399 VarDeclFilterCCC(Sema &S) : Actions(S) { }
400 virtual bool ValidateCandidate(const TypoCorrection &Candidate) {
401 NamedDecl *ND = Candidate.getCorrectionDecl();
402 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
403 return VD->hasGlobalStorage() &&
404 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
405 Actions.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000406 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000407 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000408 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000409};
410}
411
412ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
413 CXXScopeSpec &ScopeSpec,
414 const DeclarationNameInfo &Id) {
415 LookupResult Lookup(*this, Id, LookupOrdinaryName);
416 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
417
418 if (Lookup.isAmbiguous())
419 return ExprError();
420
421 VarDecl *VD;
422 if (!Lookup.isSingleResult()) {
423 VarDeclFilterCCC Validator(*this);
Richard Smithf9b15102013-08-17 00:46:16 +0000424 if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope,
425 0, Validator)) {
426 diagnoseTypo(Corrected,
427 PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest
428 : diag::err_omp_expected_var_arg_suggest)
429 << Id.getName());
430 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000431 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000432 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
433 : diag::err_omp_expected_var_arg)
434 << Id.getName();
435 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000436 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000437 } else {
438 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Richard Smithf9b15102013-08-17 00:46:16 +0000439 Diag(Id.getLoc(), diag::err_omp_expected_var_arg)
440 << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000441 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
442 return ExprError();
443 }
444 }
445 Lookup.suppressDiagnostics();
446
447 // OpenMP [2.9.2, Syntax, C/C++]
448 // Variables must be file-scope, namespace-scope, or static block-scope.
449 if (!VD->hasGlobalStorage()) {
450 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
451 << getOpenMPDirectiveName(OMPD_threadprivate)
452 << !VD->isStaticLocal();
453 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
454 VarDecl::DeclarationOnly;
455 Diag(VD->getLocation(),
456 IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD;
457 return ExprError();
458 }
459
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000460 VarDecl *CanonicalVD = VD->getCanonicalDecl();
461 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000462 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
463 // A threadprivate directive for file-scope variables must appear outside
464 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000465 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
466 !getCurLexicalContext()->isTranslationUnit()) {
467 Diag(Id.getLoc(), diag::err_omp_var_scope)
468 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
469 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
470 VarDecl::DeclarationOnly;
471 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
472 diag::note_defined_here) << VD;
473 return ExprError();
474 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000475 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
476 // A threadprivate directive for static class member variables must appear
477 // in the class definition, in the same scope in which the member
478 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000479 if (CanonicalVD->isStaticDataMember() &&
480 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
481 Diag(Id.getLoc(), diag::err_omp_var_scope)
482 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
483 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
484 VarDecl::DeclarationOnly;
485 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
486 diag::note_defined_here) << VD;
487 return ExprError();
488 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000489 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
490 // A threadprivate directive for namespace-scope variables must appear
491 // outside any definition or declaration other than the namespace
492 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000493 if (CanonicalVD->getDeclContext()->isNamespace() &&
494 (!getCurLexicalContext()->isFileContext() ||
495 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
496 Diag(Id.getLoc(), diag::err_omp_var_scope)
497 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
498 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
499 VarDecl::DeclarationOnly;
500 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
501 diag::note_defined_here) << VD;
502 return ExprError();
503 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000504 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
505 // A threadprivate directive for static block-scope variables must appear
506 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000507 if (CanonicalVD->isStaticLocal() && CurScope &&
508 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000509 Diag(Id.getLoc(), diag::err_omp_var_scope)
510 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
511 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
512 VarDecl::DeclarationOnly;
513 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
514 diag::note_defined_here) << VD;
515 return ExprError();
516 }
517
518 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
519 // A threadprivate directive must lexically precede all references to any
520 // of the variables in its list.
521 if (VD->isUsed()) {
522 Diag(Id.getLoc(), diag::err_omp_var_used)
523 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
524 return ExprError();
525 }
526
527 QualType ExprType = VD->getType().getNonReferenceType();
528 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_RValue, Id.getLoc());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000529 DSAStack->addDSA(VD, cast<DeclRefExpr>(DE.get()), OMPC_threadprivate);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000530 return DE;
531}
532
533Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective(
534 SourceLocation Loc,
535 ArrayRef<Expr *> VarList) {
536 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000537 CurContext->addDecl(D);
538 return DeclGroupPtrTy::make(DeclGroupRef(D));
539 }
540 return DeclGroupPtrTy();
541}
542
543OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl(
544 SourceLocation Loc,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000545 ArrayRef<Expr *> VarList) {
546 SmallVector<Expr *, 8> Vars;
547 for (ArrayRef<Expr *>::iterator I = VarList.begin(),
Alexey Bataeva769e072013-03-22 06:34:35 +0000548 E = VarList.end();
549 I != E; ++I) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000550 DeclRefExpr *DE = cast<DeclRefExpr>(*I);
551 VarDecl *VD = cast<VarDecl>(DE->getDecl());
552 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000553
554 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
555 // A threadprivate variable must not have an incomplete type.
556 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000557 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000558 continue;
559 }
560
561 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
562 // A threadprivate variable must not have a reference type.
563 if (VD->getType()->isReferenceType()) {
564 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000565 << getOpenMPDirectiveName(OMPD_threadprivate)
566 << VD->getType();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000567 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
568 VarDecl::DeclarationOnly;
569 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
570 diag::note_defined_here) << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000571 continue;
572 }
573
Richard Smithfd3834f2013-04-13 02:43:54 +0000574 // Check if this is a TLS variable.
575 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000576 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000577 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
578 VarDecl::DeclarationOnly;
579 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
580 diag::note_defined_here) << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000581 continue;
582 }
583
584 Vars.push_back(*I);
585 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000586 OMPThreadPrivateDecl *D = 0;
587 if (!Vars.empty()) {
588 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
589 Vars);
590 D->setAccess(AS_public);
591 }
592 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000593}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000594
Alexey Bataev758e55e2013-09-06 18:03:48 +0000595namespace {
596class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
597 DSAStackTy *Stack;
598 Sema &Actions;
599 bool ErrorFound;
600 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000601 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000602public:
603 void VisitDeclRefExpr(DeclRefExpr *E) {
604 if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000605 // Skip internally declared variables.
606 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) return;
607
608 SourceLocation ELoc = E->getExprLoc();
609
610 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
611 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
612 if (DVar.CKind != OMPC_unknown) {
613 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000614 DVar.CKind != OMPC_threadprivate && !DVar.RefExpr)
615 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000616 return;
617 }
618 // The default(none) clause requires that each variable that is referenced
619 // in the construct, and does not have a predetermined data-sharing
620 // attribute, must have its data-sharing attribute explicitly determined
621 // by being listed in a data-sharing attribute clause.
622 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
623 (DKind == OMPD_parallel || DKind == OMPD_task)) {
624 ErrorFound = true;
625 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
626 return;
627 }
628
629 // OpenMP [2.9.3.6, Restrictions, p.2]
630 // A list item that appears in a reduction clause of the innermost
631 // enclosing worksharing or parallel construct may not be accessed in an
632 // explicit task.
633 // TODO:
634
635 // Define implicit data-sharing attributes for task.
636 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000637 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
638 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000639 }
640 }
641 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
642 for (ArrayRef<OMPClause *>::iterator I = S->clauses().begin(),
643 E = S->clauses().end();
644 I != E; ++I)
645 if (OMPClause *C = *I)
646 for (StmtRange R = C->children(); R; ++R)
647 if (Stmt *Child = *R)
648 Visit(Child);
649 }
650 void VisitStmt(Stmt *S) {
651 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
652 I != E; ++I)
653 if (Stmt *Child = *I)
654 if (!isa<OMPExecutableDirective>(Child))
655 Visit(Child);
656 }
657
658 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000659 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000660
661 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
662 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { }
663};
664}
665
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000666StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
667 ArrayRef<OMPClause *> Clauses,
668 Stmt *AStmt,
669 SourceLocation StartLoc,
670 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000671 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
672
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000673 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000674
675 // Check default data sharing attributes for referenced variables.
676 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
677 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
678 if (DSAChecker.isErrorFound())
679 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000680 // Generate list of implicitly defined firstprivate variables.
681 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
682 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
683
684 bool ErrorFound = false;
685 if (!DSAChecker.getImplicitFirstprivate().empty()) {
686 if (OMPClause *Implicit =
687 ActOnOpenMPFirstprivateClause(DSAChecker.getImplicitFirstprivate(),
688 SourceLocation(), SourceLocation(),
689 SourceLocation())) {
690 ClausesWithImplicit.push_back(Implicit);
691 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
692 DSAChecker.getImplicitFirstprivate().size();
693 } else
694 ErrorFound = true;
695 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000696
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000697 switch (Kind) {
698 case OMPD_parallel:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000699 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt,
700 StartLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000701 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000702 case OMPD_simd:
703 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt,
704 StartLoc, EndLoc);
705 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000706 case OMPD_threadprivate:
707 case OMPD_task:
708 llvm_unreachable("OpenMP Directive is not allowed");
709 case OMPD_unknown:
710 case NUM_OPENMP_DIRECTIVES:
711 llvm_unreachable("Unknown OpenMP directive");
712 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000713
714 if (ErrorFound) return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000715 return Res;
716}
717
718StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
719 Stmt *AStmt,
720 SourceLocation StartLoc,
721 SourceLocation EndLoc) {
722 getCurFunction()->setHasBranchProtectedScope();
723
724 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
725 Clauses, AStmt));
726}
727
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000728StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
729 Stmt *AStmt,
730 SourceLocation StartLoc,
731 SourceLocation EndLoc) {
732 Stmt *CStmt = AStmt;
733 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt))
734 CStmt = CS->getCapturedStmt();
735 while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt))
736 CStmt = AS->getSubStmt();
737 ForStmt *For = dyn_cast<ForStmt>(CStmt);
738 if (!For) {
739 Diag(CStmt->getLocStart(), diag::err_omp_not_for)
740 << getOpenMPDirectiveName(OMPD_simd);
741 return StmtError();
742 }
743
744 // FIXME: Checking loop canonical form, collapsing etc.
745
746 getCurFunction()->setHasBranchProtectedScope();
747 return Owned(OMPSimdDirective::Create(Context, StartLoc, EndLoc,
748 Clauses, AStmt));
749}
750
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000751OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
752 Expr *Expr,
753 SourceLocation StartLoc,
754 SourceLocation LParenLoc,
755 SourceLocation EndLoc) {
756 OMPClause *Res = 0;
757 switch (Kind) {
758 case OMPC_if:
759 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
760 break;
Alexey Bataev568a8332014-03-06 06:15:19 +0000761 case OMPC_num_threads:
762 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
763 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000764 case OMPC_default:
765 case OMPC_private:
766 case OMPC_firstprivate:
767 case OMPC_shared:
768 case OMPC_threadprivate:
769 case OMPC_unknown:
770 case NUM_OPENMP_CLAUSES:
771 llvm_unreachable("Clause is not allowed.");
772 }
773 return Res;
774}
775
776OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition,
777 SourceLocation StartLoc,
778 SourceLocation LParenLoc,
779 SourceLocation EndLoc) {
780 Expr *ValExpr = Condition;
781 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
782 !Condition->isInstantiationDependent() &&
783 !Condition->containsUnexpandedParameterPack()) {
784 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
785 Condition->getExprLoc(),
786 Condition);
787 if (Val.isInvalid())
788 return 0;
789
790 ValExpr = Val.take();
791 }
792
793 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
794}
795
Alexey Bataev568a8332014-03-06 06:15:19 +0000796ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
797 Expr *Op) {
798 if (!Op)
799 return ExprError();
800
801 class IntConvertDiagnoser : public ICEConvertDiagnoser {
802 public:
803 IntConvertDiagnoser()
804 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false,
805 false, true) {}
806 virtual SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
807 QualType T) {
808 return S.Diag(Loc, diag::err_omp_not_integral) << T;
809 }
810 virtual SemaDiagnosticBuilder diagnoseIncomplete(
811 Sema &S, SourceLocation Loc, QualType T) {
812 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
813 }
814 virtual SemaDiagnosticBuilder diagnoseExplicitConv(
815 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) {
816 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
817 }
818 virtual SemaDiagnosticBuilder noteExplicitConv(
819 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) {
820 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
821 << ConvTy->isEnumeralType() << ConvTy;
822 }
823 virtual SemaDiagnosticBuilder diagnoseAmbiguous(
824 Sema &S, SourceLocation Loc, QualType T) {
825 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
826 }
827 virtual SemaDiagnosticBuilder noteAmbiguous(
828 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) {
829 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
830 << ConvTy->isEnumeralType() << ConvTy;
831 }
832 virtual SemaDiagnosticBuilder diagnoseConversion(
833 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) {
834 llvm_unreachable("conversion functions are permitted");
835 }
836 } ConvertDiagnoser;
837 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
838}
839
840OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
841 SourceLocation StartLoc,
842 SourceLocation LParenLoc,
843 SourceLocation EndLoc) {
844 Expr *ValExpr = NumThreads;
845 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
846 !NumThreads->isInstantiationDependent() &&
847 !NumThreads->containsUnexpandedParameterPack()) {
848 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
849 ExprResult Val =
850 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
851 if (Val.isInvalid())
852 return 0;
853
854 ValExpr = Val.take();
855
856 // OpenMP [2.5, Restrictions]
857 // The num_threads expression must evaluate to a positive integer value.
858 llvm::APSInt Result;
859 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
860 Result.isSigned() && !Result.isStrictlyPositive()) {
861 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
862 << "num_threads" << NumThreads->getSourceRange();
863 return 0;
864 }
865 }
866
867 return new (Context) OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc,
868 EndLoc);
869}
870
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000871OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
872 unsigned Argument,
873 SourceLocation ArgumentLoc,
874 SourceLocation StartLoc,
875 SourceLocation LParenLoc,
876 SourceLocation EndLoc) {
877 OMPClause *Res = 0;
878 switch (Kind) {
879 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000880 Res =
881 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
882 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000883 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000884 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000885 case OMPC_num_threads:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000886 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000887 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000888 case OMPC_shared:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000889 case OMPC_threadprivate:
890 case OMPC_unknown:
891 case NUM_OPENMP_CLAUSES:
892 llvm_unreachable("Clause is not allowed.");
893 }
894 return Res;
895}
896
897OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
898 SourceLocation KindKwLoc,
899 SourceLocation StartLoc,
900 SourceLocation LParenLoc,
901 SourceLocation EndLoc) {
902 if (Kind == OMPC_DEFAULT_unknown) {
903 std::string Values;
904 std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : "");
905 for (unsigned i = OMPC_DEFAULT_unknown + 1;
906 i < NUM_OPENMP_DEFAULT_KINDS; ++i) {
907 Values += "'";
908 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
909 Values += "'";
910 switch (i) {
911 case NUM_OPENMP_DEFAULT_KINDS - 2:
912 Values += " or ";
913 break;
914 case NUM_OPENMP_DEFAULT_KINDS - 1:
915 break;
916 default:
917 Values += Sep;
918 break;
919 }
920 }
921 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
922 << Values << getOpenMPClauseName(OMPC_default);
923 return 0;
924 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000925 switch (Kind) {
926 case OMPC_DEFAULT_none:
927 DSAStack->setDefaultDSANone();
928 break;
929 case OMPC_DEFAULT_shared:
930 DSAStack->setDefaultDSAShared();
931 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000932 case OMPC_DEFAULT_unknown:
933 case NUM_OPENMP_DEFAULT_KINDS:
934 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +0000935 break;
936 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000937 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
938 EndLoc);
939}
940
941OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
942 ArrayRef<Expr *> VarList,
943 SourceLocation StartLoc,
944 SourceLocation LParenLoc,
945 SourceLocation EndLoc) {
946 OMPClause *Res = 0;
947 switch (Kind) {
948 case OMPC_private:
949 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
950 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000951 case OMPC_firstprivate:
952 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
953 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000954 case OMPC_shared:
955 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
956 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000957 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000958 case OMPC_num_threads:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000959 case OMPC_default:
960 case OMPC_threadprivate:
961 case OMPC_unknown:
962 case NUM_OPENMP_CLAUSES:
963 llvm_unreachable("Clause is not allowed.");
964 }
965 return Res;
966}
967
968OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
969 SourceLocation StartLoc,
970 SourceLocation LParenLoc,
971 SourceLocation EndLoc) {
972 SmallVector<Expr *, 8> Vars;
973 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
974 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +0000975 assert(*I && "NULL expr in OpenMP private clause.");
976 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000977 // It will be analyzed later.
978 Vars.push_back(*I);
979 continue;
980 }
981
982 SourceLocation ELoc = (*I)->getExprLoc();
983 // OpenMP [2.1, C/C++]
984 // A list item is a variable name.
985 // OpenMP [2.9.3.3, Restrictions, p.1]
986 // A variable that is part of another variable (as an array or
987 // structure element) cannot appear in a private clause.
988 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
989 if (!DE || !isa<VarDecl>(DE->getDecl())) {
990 Diag(ELoc, diag::err_omp_expected_var_name)
991 << (*I)->getSourceRange();
992 continue;
993 }
994 Decl *D = DE->getDecl();
995 VarDecl *VD = cast<VarDecl>(D);
996
997 QualType Type = VD->getType();
998 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
999 // It will be analyzed later.
1000 Vars.push_back(DE);
1001 continue;
1002 }
1003
1004 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1005 // A variable that appears in a private clause must not have an incomplete
1006 // type or a reference type.
1007 if (RequireCompleteType(ELoc, Type,
1008 diag::err_omp_private_incomplete_type)) {
1009 continue;
1010 }
1011 if (Type->isReferenceType()) {
1012 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1013 << getOpenMPClauseName(OMPC_private) << Type;
1014 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1015 VarDecl::DeclarationOnly;
1016 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1017 diag::note_defined_here) << VD;
1018 continue;
1019 }
1020
1021 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1022 // A variable of class type (or array thereof) that appears in a private
1023 // clause requires an accesible, unambiguous default constructor for the
1024 // class type.
1025 while (Type.getNonReferenceType()->isArrayType()) {
1026 Type = cast<ArrayType>(
1027 Type.getNonReferenceType().getTypePtr())->getElementType();
1028 }
1029 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1030 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
1031 if (RD) {
1032 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1033 PartialDiagnostic PD =
1034 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1035 if (!CD ||
1036 CheckConstructorAccess(ELoc, CD,
1037 InitializedEntity::InitializeTemporary(Type),
1038 CD->getAccess(), PD) == AR_inaccessible ||
1039 CD->isDeleted()) {
1040 Diag(ELoc, diag::err_omp_required_method)
1041 << getOpenMPClauseName(OMPC_private) << 0;
1042 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1043 VarDecl::DeclarationOnly;
1044 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1045 diag::note_defined_here) << VD;
1046 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1047 continue;
1048 }
1049 MarkFunctionReferenced(ELoc, CD);
1050 DiagnoseUseOfDecl(CD, ELoc);
1051
1052 CXXDestructorDecl *DD = RD->getDestructor();
1053 if (DD) {
1054 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1055 DD->isDeleted()) {
1056 Diag(ELoc, diag::err_omp_required_method)
1057 << getOpenMPClauseName(OMPC_private) << 4;
1058 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1059 VarDecl::DeclarationOnly;
1060 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1061 diag::note_defined_here) << VD;
1062 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1063 continue;
1064 }
1065 MarkFunctionReferenced(ELoc, DD);
1066 DiagnoseUseOfDecl(DD, ELoc);
1067 }
1068 }
1069
Alexey Bataev758e55e2013-09-06 18:03:48 +00001070 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1071 // in a Construct]
1072 // Variables with the predetermined data-sharing attributes may not be
1073 // listed in data-sharing attributes clauses, except for the cases
1074 // listed below. For these exceptions only, listing a predetermined
1075 // variable in a data-sharing attribute clause is allowed and overrides
1076 // the variable's predetermined data-sharing attributes.
1077 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1078 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
1079 Diag(ELoc, diag::err_omp_wrong_dsa)
1080 << getOpenMPClauseName(DVar.CKind)
1081 << getOpenMPClauseName(OMPC_private);
1082 if (DVar.RefExpr) {
1083 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1084 << getOpenMPClauseName(DVar.CKind);
1085 } else {
1086 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1087 << getOpenMPClauseName(DVar.CKind);
1088 }
1089 continue;
1090 }
1091
1092 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001093 Vars.push_back(DE);
1094 }
1095
1096 if (Vars.empty()) return 0;
1097
1098 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1099}
1100
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001101OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1102 SourceLocation StartLoc,
1103 SourceLocation LParenLoc,
1104 SourceLocation EndLoc) {
1105 SmallVector<Expr *, 8> Vars;
1106 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1107 I != E; ++I) {
1108 assert(*I && "NULL expr in OpenMP firstprivate clause.");
1109 if (isa<DependentScopeDeclRefExpr>(*I)) {
1110 // It will be analyzed later.
1111 Vars.push_back(*I);
1112 continue;
1113 }
1114
1115 SourceLocation ELoc = (*I)->getExprLoc();
1116 // OpenMP [2.1, C/C++]
1117 // A list item is a variable name.
1118 // OpenMP [2.9.3.3, Restrictions, p.1]
1119 // A variable that is part of another variable (as an array or
1120 // structure element) cannot appear in a private clause.
1121 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1122 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1123 Diag(ELoc, diag::err_omp_expected_var_name)
1124 << (*I)->getSourceRange();
1125 continue;
1126 }
1127 Decl *D = DE->getDecl();
1128 VarDecl *VD = cast<VarDecl>(D);
1129
1130 QualType Type = VD->getType();
1131 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1132 // It will be analyzed later.
1133 Vars.push_back(DE);
1134 continue;
1135 }
1136
1137 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1138 // A variable that appears in a private clause must not have an incomplete
1139 // type or a reference type.
1140 if (RequireCompleteType(ELoc, Type,
1141 diag::err_omp_firstprivate_incomplete_type)) {
1142 continue;
1143 }
1144 if (Type->isReferenceType()) {
1145 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1146 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1147 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1148 VarDecl::DeclarationOnly;
1149 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1150 diag::note_defined_here) << VD;
1151 continue;
1152 }
1153
1154 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1155 // A variable of class type (or array thereof) that appears in a private
1156 // clause requires an accesible, unambiguous copy constructor for the
1157 // class type.
1158 Type = Context.getBaseElementType(Type);
1159 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1160 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
1161 if (RD) {
1162 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1163 PartialDiagnostic PD =
1164 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1165 if (!CD ||
1166 CheckConstructorAccess(ELoc, CD,
1167 InitializedEntity::InitializeTemporary(Type),
1168 CD->getAccess(), PD) == AR_inaccessible ||
1169 CD->isDeleted()) {
1170 Diag(ELoc, diag::err_omp_required_method)
1171 << getOpenMPClauseName(OMPC_firstprivate) << 1;
1172 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1173 VarDecl::DeclarationOnly;
1174 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1175 diag::note_defined_here) << VD;
1176 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1177 continue;
1178 }
1179 MarkFunctionReferenced(ELoc, CD);
1180 DiagnoseUseOfDecl(CD, ELoc);
1181
1182 CXXDestructorDecl *DD = RD->getDestructor();
1183 if (DD) {
1184 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1185 DD->isDeleted()) {
1186 Diag(ELoc, diag::err_omp_required_method)
1187 << getOpenMPClauseName(OMPC_firstprivate) << 4;
1188 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1189 VarDecl::DeclarationOnly;
1190 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1191 diag::note_defined_here) << VD;
1192 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1193 continue;
1194 }
1195 MarkFunctionReferenced(ELoc, DD);
1196 DiagnoseUseOfDecl(DD, ELoc);
1197 }
1198 }
1199
1200 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1201 // variable and it was checked already.
1202 if (StartLoc.isValid() && EndLoc.isValid()) {
1203 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1204 Type = Type.getNonReferenceType().getCanonicalType();
1205 bool IsConstant = Type.isConstant(Context);
1206 Type = Context.getBaseElementType(Type);
1207 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1208 // A list item that specifies a given variable may not appear in more
1209 // than one clause on the same directive, except that a variable may be
1210 // specified in both firstprivate and lastprivate clauses.
1211 // TODO: add processing for lastprivate.
1212 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1213 DVar.RefExpr) {
1214 Diag(ELoc, diag::err_omp_wrong_dsa)
1215 << getOpenMPClauseName(DVar.CKind)
1216 << getOpenMPClauseName(OMPC_firstprivate);
1217 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1218 << getOpenMPClauseName(DVar.CKind);
1219 continue;
1220 }
1221
1222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1223 // in a Construct]
1224 // Variables with the predetermined data-sharing attributes may not be
1225 // listed in data-sharing attributes clauses, except for the cases
1226 // listed below. For these exceptions only, listing a predetermined
1227 // variable in a data-sharing attribute clause is allowed and overrides
1228 // the variable's predetermined data-sharing attributes.
1229 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1230 // in a Construct, C/C++, p.2]
1231 // Variables with const-qualified type having no mutable member may be
1232 // listed in a firstprivate clause, even if they are static data members.
1233 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1234 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1235 Diag(ELoc, diag::err_omp_wrong_dsa)
1236 << getOpenMPClauseName(DVar.CKind)
1237 << getOpenMPClauseName(OMPC_firstprivate);
1238 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1239 << getOpenMPClauseName(DVar.CKind);
1240 continue;
1241 }
1242
1243 // OpenMP [2.9.3.4, Restrictions, p.2]
1244 // A list item that is private within a parallel region must not appear
1245 // in a firstprivate clause on a worksharing construct if any of the
1246 // worksharing regions arising from the worksharing construct ever bind
1247 // to any of the parallel regions arising from the parallel construct.
1248 // OpenMP [2.9.3.4, Restrictions, p.3]
1249 // A list item that appears in a reduction clause of a parallel construct
1250 // must not appear in a firstprivate clause on a worksharing or task
1251 // construct if any of the worksharing or task regions arising from the
1252 // worksharing or task construct ever bind to any of the parallel regions
1253 // arising from the parallel construct.
1254 // OpenMP [2.9.3.4, Restrictions, p.4]
1255 // A list item that appears in a reduction clause in worksharing
1256 // construct must not appear in a firstprivate clause in a task construct
1257 // encountered during execution of any of the worksharing regions arising
1258 // from the worksharing construct.
1259 // TODO:
1260 }
1261
1262 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1263 Vars.push_back(DE);
1264 }
1265
1266 if (Vars.empty()) return 0;
1267
1268 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1269 Vars);
1270}
1271
Alexey Bataev758e55e2013-09-06 18:03:48 +00001272OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1273 SourceLocation StartLoc,
1274 SourceLocation LParenLoc,
1275 SourceLocation EndLoc) {
1276 SmallVector<Expr *, 8> Vars;
1277 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1278 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001279 assert(*I && "NULL expr in OpenMP shared clause.");
1280 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001281 // It will be analyzed later.
1282 Vars.push_back(*I);
1283 continue;
1284 }
1285
1286 SourceLocation ELoc = (*I)->getExprLoc();
1287 // OpenMP [2.1, C/C++]
1288 // A list item is a variable name.
1289 // OpenMP [2.9.3.4, Restrictions, p.1]
1290 // A variable that is part of another variable (as an array or
1291 // structure element) cannot appear in a private clause.
1292 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1293 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1294 Diag(ELoc, diag::err_omp_expected_var_name)
1295 << (*I)->getSourceRange();
1296 continue;
1297 }
1298 Decl *D = DE->getDecl();
1299 VarDecl *VD = cast<VarDecl>(D);
1300
1301 QualType Type = VD->getType();
1302 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1303 // It will be analyzed later.
1304 Vars.push_back(DE);
1305 continue;
1306 }
1307
1308 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1309 // in a Construct]
1310 // Variables with the predetermined data-sharing attributes may not be
1311 // listed in data-sharing attributes clauses, except for the cases
1312 // listed below. For these exceptions only, listing a predetermined
1313 // variable in a data-sharing attribute clause is allowed and overrides
1314 // the variable's predetermined data-sharing attributes.
1315 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1316 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1317 Diag(ELoc, diag::err_omp_wrong_dsa)
1318 << getOpenMPClauseName(DVar.CKind)
1319 << getOpenMPClauseName(OMPC_shared);
1320 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1321 << getOpenMPClauseName(DVar.CKind);
1322 continue;
1323 }
1324
1325 DSAStack->addDSA(VD, DE, OMPC_shared);
1326 Vars.push_back(DE);
1327 }
1328
1329 if (Vars.empty()) return 0;
1330
1331 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1332}
1333
1334#undef DSAStack