blob: d5778bc636762476b7b2646c62c3bbcf6eb8a6d6 [file] [log] [blame]
Alexey Bataevc6400582013-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 Bataev6af701f2013-05-13 04:18:18 +000011/// clauses.
Alexey Bataevc6400582013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "clang/Basic/OpenMPKinds.h"
16#include "clang/AST/Decl.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000022#include "clang/Lex/Preprocessor.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000023#include "clang/Sema/Initialization.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000024#include "clang/Sema/SemaInternal.h"
25#include "clang/Sema/Lookup.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000028using namespace clang;
29
Alexey Bataev0c018352013-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);
85public:
86 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) { }
87
88 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
89 Scope *CurScope) {
90 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
91 }
92
93 void pop() {
94 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
95 Stack.pop_back();
96 }
97
98 /// \brief Adds explicit data sharing attribute to the specified declaration.
99 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
100
101 /// \brief Checks if the variable is a local for OpenMP region.
102 bool isOpenMPLocal(VarDecl *D);
103
104 /// \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 Bataevd195bc32013-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 Bataev0c018352013-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 Bataevd195bc32013-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 Bataev0c018352013-09-06 18:03:48 +0000153 return DVar;
154 }
155 DVar.DKind = Iter->Directive;
156 // Explicitly specified attributes and local variables with predetermined
157 // attributes.
158 if (Iter->SharingMap.count(D)) {
159 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
160 DVar.CKind = Iter->SharingMap[D].Attributes;
161 return DVar;
162 }
163
164 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
165 // in a Construct, C/C++, implicitly determined, p.1]
166 // In a parallel or task construct, the data-sharing attributes of these
167 // variables are determined by the default clause, if present.
168 switch (Iter->DefaultAttr) {
169 case DSA_shared:
170 DVar.CKind = OMPC_shared;
171 return DVar;
172 case DSA_none:
173 return DVar;
174 case DSA_unspecified:
175 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
176 // in a Construct, implicitly determined, p.2]
177 // In a parallel construct, if no default clause is present, these
178 // variables are shared.
179 if (DVar.DKind == OMPD_parallel) {
180 DVar.CKind = OMPC_shared;
181 return DVar;
182 }
183
184 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
185 // in a Construct, implicitly determined, p.4]
186 // In a task construct, if no default clause is present, a variable that in
187 // the enclosing context is determined to be shared by all implicit tasks
188 // bound to the current team is shared.
189 // TODO
190 if (DVar.DKind == OMPD_task) {
191 DSAVarData DVarTemp;
192 for (StackTy::reverse_iterator I = Iter + 1,
193 EE = Stack.rend() - 1;
194 I != EE; ++I) {
195 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
196 // in a Construct, implicitly determined, p.6]
197 // In a task construct, if no default clause is present, a variable
198 // whose data-sharing attribute is not determined by the rules above is
199 // firstprivate.
200 DVarTemp = getDSA(I, D);
201 if (DVarTemp.CKind != OMPC_shared) {
202 DVar.RefExpr = 0;
203 DVar.DKind = OMPD_task;
Alexey Bataevd195bc32013-10-01 05:32:34 +0000204 DVar.CKind = OMPC_firstprivate;
Alexey Bataev0c018352013-09-06 18:03:48 +0000205 return DVar;
206 }
207 if (I->Directive == OMPD_parallel) break;
208 }
209 DVar.DKind = OMPD_task;
Alexey Bataev0c018352013-09-06 18:03:48 +0000210 DVar.CKind =
Alexey Bataevd195bc32013-10-01 05:32:34 +0000211 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev0c018352013-09-06 18:03:48 +0000212 return DVar;
213 }
214 }
215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
216 // in a Construct, implicitly determined, p.3]
217 // For constructs other than task, if no default clause is present, these
218 // variables inherit their data-sharing attributes from the enclosing
219 // context.
220 return getDSA(Iter + 1, D);
221}
222
223void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
224 if (A == OMPC_threadprivate) {
225 Stack[0].SharingMap[D].Attributes = A;
226 Stack[0].SharingMap[D].RefExpr = E;
227 } else {
228 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
229 Stack.back().SharingMap[D].Attributes = A;
230 Stack.back().SharingMap[D].RefExpr = E;
231 }
232}
233
234bool DSAStackTy::isOpenMPLocal(VarDecl *D) {
235 Scope *CurScope = getCurScope();
236 while (CurScope && !CurScope->isDeclScope(D))
237 CurScope = CurScope->getParent();
238 while (CurScope && !CurScope->isOpenMPDirectiveScope())
239 CurScope = CurScope->getParent();
240 bool isOpenMPLocal = !!CurScope;
241 if (!isOpenMPLocal) {
242 CurScope = getCurScope();
243 while (CurScope && !CurScope->isOpenMPDirectiveScope())
244 CurScope = CurScope->getParent();
245 isOpenMPLocal =
246 CurScope &&
247 isa<CapturedDecl>(D->getDeclContext()) &&
248 static_cast<DeclContext *>(
249 CurScope->getFnParent()->getEntity())->Encloses(D->getDeclContext());
250 }
251 return isOpenMPLocal;
252}
253
254DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
255 DSAVarData DVar;
256
257 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
258 // in a Construct, C/C++, predetermined, p.1]
259 // Variables appearing in threadprivate directives are threadprivate.
260 if (D->getTLSKind() != VarDecl::TLS_None) {
261 DVar.CKind = OMPC_threadprivate;
262 return DVar;
263 }
264 if (Stack[0].SharingMap.count(D)) {
265 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
266 DVar.CKind = OMPC_threadprivate;
267 return DVar;
268 }
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 with automatic storage duration that are declared in a scope
273 // inside the construct are private.
274 if (isOpenMPLocal(D) && D->isLocalVarDecl() &&
275 (D->getStorageClass() == SC_Auto ||
276 D->getStorageClass() == SC_None)) {
277 DVar.CKind = OMPC_private;
278 return DVar;
279 }
280
281 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
282 // in a Construct, C/C++, predetermined, p.4]
283 // Static data memebers are shared.
284 if (D->isStaticDataMember()) {
Alexey Bataevd195bc32013-10-01 05:32:34 +0000285 // Variables with const-qualified type having no mutable member may be listed
286 // in a firstprivate clause, even if they are static data members.
287 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
288 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
289 return DVar;
290
Alexey Bataev0c018352013-09-06 18:03:48 +0000291 DVar.CKind = OMPC_shared;
292 return DVar;
293 }
294
295 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
296 bool IsConstant = Type.isConstant(Actions.getASTContext());
297 while (Type->isArrayType()) {
298 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
299 Type = ElemType.getNonReferenceType().getCanonicalType();
300 }
301 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
302 // in a Construct, C/C++, predetermined, p.6]
303 // Variables with const qualified type having no mutable member are
304 // shared.
305 CXXRecordDecl *RD = Actions.getLangOpts().CPlusPlus ?
306 Type->getAsCXXRecordDecl() : 0;
307 if (IsConstant &&
308 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
309 // Variables with const-qualified type having no mutable member may be
310 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd195bc32013-10-01 05:32:34 +0000311 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
312 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
313 return DVar;
314
Alexey Bataev0c018352013-09-06 18:03:48 +0000315 DVar.CKind = OMPC_shared;
316 return DVar;
317 }
318
319 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
320 // in a Construct, C/C++, predetermined, p.7]
321 // Variables with static storage duration that are declared in a scope
322 // inside the construct are shared.
323 if (isOpenMPLocal(D) && D->isStaticLocal()) {
324 DVar.CKind = OMPC_shared;
325 return DVar;
326 }
327
328 // Explicitly specified attributes and local variables with predetermined
329 // attributes.
330 if (Stack.back().SharingMap.count(D)) {
331 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
332 DVar.CKind = Stack.back().SharingMap[D].Attributes;
333 }
334
335 return DVar;
336}
337
338DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
339 return getDSA(Stack.rbegin() + 1, D);
340}
341
Alexey Bataevd195bc32013-10-01 05:32:34 +0000342DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
343 OpenMPDirectiveKind DKind) {
344 for (StackTy::reverse_iterator I = Stack.rbegin() + 1,
345 E = Stack.rend() - 1;
346 I != E; ++I) {
347 if (DKind != OMPD_unknown && DKind != I->Directive) continue;
348 DSAVarData DVar = getDSA(I, D);
349 if (DVar.CKind == CKind)
350 return DVar;
351 }
352 return DSAVarData();
353}
354
Alexey Bataev0c018352013-09-06 18:03:48 +0000355void Sema::InitDataSharingAttributesStack() {
356 VarDataSharingAttributesStack = new DSAStackTy(*this);
357}
358
359#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
360
361void Sema::DestroyDataSharingAttributesStack() {
362 delete DSAStack;
363}
364
365void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
366 const DeclarationNameInfo &DirName,
367 Scope *CurScope) {
368 DSAStack->push(DKind, DirName, CurScope);
369 PushExpressionEvaluationContext(PotentiallyEvaluated);
370}
371
372void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
373 DSAStack->pop();
374 DiscardCleanupsInEvaluationContext();
375 PopExpressionEvaluationContext();
376}
377
Alexey Bataevc6400582013-03-22 06:34:35 +0000378namespace {
379
Alexey Bataev6af701f2013-05-13 04:18:18 +0000380class VarDeclFilterCCC : public CorrectionCandidateCallback {
381private:
382 Sema &Actions;
383public:
384 VarDeclFilterCCC(Sema &S) : Actions(S) { }
385 virtual bool ValidateCandidate(const TypoCorrection &Candidate) {
386 NamedDecl *ND = Candidate.getCorrectionDecl();
387 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
388 return VD->hasGlobalStorage() &&
389 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
390 Actions.getCurScope());
Alexey Bataevc6400582013-03-22 06:34:35 +0000391 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000392 return false;
Alexey Bataevc6400582013-03-22 06:34:35 +0000393 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000394};
395}
396
397ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
398 CXXScopeSpec &ScopeSpec,
399 const DeclarationNameInfo &Id) {
400 LookupResult Lookup(*this, Id, LookupOrdinaryName);
401 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
402
403 if (Lookup.isAmbiguous())
404 return ExprError();
405
406 VarDecl *VD;
407 if (!Lookup.isSingleResult()) {
408 VarDeclFilterCCC Validator(*this);
Richard Smith2d670972013-08-17 00:46:16 +0000409 if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope,
410 0, Validator)) {
411 diagnoseTypo(Corrected,
412 PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest
413 : diag::err_omp_expected_var_arg_suggest)
414 << Id.getName());
415 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6af701f2013-05-13 04:18:18 +0000416 } else {
Richard Smith2d670972013-08-17 00:46:16 +0000417 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
418 : diag::err_omp_expected_var_arg)
419 << Id.getName();
420 return ExprError();
Alexey Bataev6af701f2013-05-13 04:18:18 +0000421 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000422 } else {
423 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Richard Smith2d670972013-08-17 00:46:16 +0000424 Diag(Id.getLoc(), diag::err_omp_expected_var_arg)
425 << Id.getName();
Alexey Bataev6af701f2013-05-13 04:18:18 +0000426 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
427 return ExprError();
428 }
429 }
430 Lookup.suppressDiagnostics();
431
432 // OpenMP [2.9.2, Syntax, C/C++]
433 // Variables must be file-scope, namespace-scope, or static block-scope.
434 if (!VD->hasGlobalStorage()) {
435 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
436 << getOpenMPDirectiveName(OMPD_threadprivate)
437 << !VD->isStaticLocal();
438 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
439 VarDecl::DeclarationOnly;
440 Diag(VD->getLocation(),
441 IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD;
442 return ExprError();
443 }
444
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000445 VarDecl *CanonicalVD = VD->getCanonicalDecl();
446 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6af701f2013-05-13 04:18:18 +0000447 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
448 // A threadprivate directive for file-scope variables must appear outside
449 // any definition or declaration.
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000450 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
451 !getCurLexicalContext()->isTranslationUnit()) {
452 Diag(Id.getLoc(), diag::err_omp_var_scope)
453 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
454 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
455 VarDecl::DeclarationOnly;
456 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
457 diag::note_defined_here) << VD;
458 return ExprError();
459 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000460 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
461 // A threadprivate directive for static class member variables must appear
462 // in the class definition, in the same scope in which the member
463 // variables are declared.
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000464 if (CanonicalVD->isStaticDataMember() &&
465 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
466 Diag(Id.getLoc(), diag::err_omp_var_scope)
467 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
468 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
469 VarDecl::DeclarationOnly;
470 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
471 diag::note_defined_here) << VD;
472 return ExprError();
473 }
Alexey Bataev6af701f2013-05-13 04:18:18 +0000474 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
475 // A threadprivate directive for namespace-scope variables must appear
476 // outside any definition or declaration other than the namespace
477 // definition itself.
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000478 if (CanonicalVD->getDeclContext()->isNamespace() &&
479 (!getCurLexicalContext()->isFileContext() ||
480 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
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 Bataev6af701f2013-05-13 04:18:18 +0000489 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
490 // A threadprivate directive for static block-scope variables must appear
491 // in the scope of the variable and not in a nested scope.
Alexey Bataevd0dbb7e2013-09-26 03:24:06 +0000492 if (CanonicalVD->isStaticLocal() && CurScope &&
493 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6af701f2013-05-13 04:18:18 +0000494 Diag(Id.getLoc(), diag::err_omp_var_scope)
495 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
496 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
497 VarDecl::DeclarationOnly;
498 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
499 diag::note_defined_here) << VD;
500 return ExprError();
501 }
502
503 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
504 // A threadprivate directive must lexically precede all references to any
505 // of the variables in its list.
506 if (VD->isUsed()) {
507 Diag(Id.getLoc(), diag::err_omp_var_used)
508 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
509 return ExprError();
510 }
511
512 QualType ExprType = VD->getType().getNonReferenceType();
513 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_RValue, Id.getLoc());
Alexey Bataev0c018352013-09-06 18:03:48 +0000514 DSAStack->addDSA(VD, cast<DeclRefExpr>(DE.get()), OMPC_threadprivate);
Alexey Bataev6af701f2013-05-13 04:18:18 +0000515 return DE;
516}
517
518Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective(
519 SourceLocation Loc,
520 ArrayRef<Expr *> VarList) {
521 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000522 CurContext->addDecl(D);
523 return DeclGroupPtrTy::make(DeclGroupRef(D));
524 }
525 return DeclGroupPtrTy();
526}
527
528OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl(
529 SourceLocation Loc,
Alexey Bataev6af701f2013-05-13 04:18:18 +0000530 ArrayRef<Expr *> VarList) {
531 SmallVector<Expr *, 8> Vars;
532 for (ArrayRef<Expr *>::iterator I = VarList.begin(),
Alexey Bataevc6400582013-03-22 06:34:35 +0000533 E = VarList.end();
534 I != E; ++I) {
Alexey Bataev6af701f2013-05-13 04:18:18 +0000535 DeclRefExpr *DE = cast<DeclRefExpr>(*I);
536 VarDecl *VD = cast<VarDecl>(DE->getDecl());
537 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataevc6400582013-03-22 06:34:35 +0000538
539 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
540 // A threadprivate variable must not have an incomplete type.
541 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6af701f2013-05-13 04:18:18 +0000542 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000543 continue;
544 }
545
546 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
547 // A threadprivate variable must not have a reference type.
548 if (VD->getType()->isReferenceType()) {
549 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000550 << getOpenMPDirectiveName(OMPD_threadprivate)
551 << VD->getType();
Alexey Bataev6af701f2013-05-13 04:18:18 +0000552 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
553 VarDecl::DeclarationOnly;
554 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
555 diag::note_defined_here) << VD;
Alexey Bataevc6400582013-03-22 06:34:35 +0000556 continue;
557 }
558
Richard Smith38afbc72013-04-13 02:43:54 +0000559 // Check if this is a TLS variable.
560 if (VD->getTLSKind()) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000561 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000562 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
563 VarDecl::DeclarationOnly;
564 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
565 diag::note_defined_here) << VD;
Alexey Bataevc6400582013-03-22 06:34:35 +0000566 continue;
567 }
568
569 Vars.push_back(*I);
570 }
571 return Vars.empty() ?
572 0 : OMPThreadPrivateDecl::Create(Context,
573 getCurLexicalContext(),
574 Loc, Vars);
575}
Alexey Bataev6af701f2013-05-13 04:18:18 +0000576
Alexey Bataev0c018352013-09-06 18:03:48 +0000577namespace {
578class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
579 DSAStackTy *Stack;
580 Sema &Actions;
581 bool ErrorFound;
582 CapturedStmt *CS;
Alexey Bataevd195bc32013-10-01 05:32:34 +0000583 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev0c018352013-09-06 18:03:48 +0000584public:
585 void VisitDeclRefExpr(DeclRefExpr *E) {
586 if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0c018352013-09-06 18:03:48 +0000587 // Skip internally declared variables.
588 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) return;
589
590 SourceLocation ELoc = E->getExprLoc();
591
592 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
593 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
594 if (DVar.CKind != OMPC_unknown) {
595 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd195bc32013-10-01 05:32:34 +0000596 DVar.CKind != OMPC_threadprivate && !DVar.RefExpr)
597 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev0c018352013-09-06 18:03:48 +0000598 return;
599 }
600 // The default(none) clause requires that each variable that is referenced
601 // in the construct, and does not have a predetermined data-sharing
602 // attribute, must have its data-sharing attribute explicitly determined
603 // by being listed in a data-sharing attribute clause.
604 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
605 (DKind == OMPD_parallel || DKind == OMPD_task)) {
606 ErrorFound = true;
607 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
608 return;
609 }
610
611 // OpenMP [2.9.3.6, Restrictions, p.2]
612 // A list item that appears in a reduction clause of the innermost
613 // enclosing worksharing or parallel construct may not be accessed in an
614 // explicit task.
615 // TODO:
616
617 // Define implicit data-sharing attributes for task.
618 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd195bc32013-10-01 05:32:34 +0000619 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
620 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev0c018352013-09-06 18:03:48 +0000621 }
622 }
623 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
624 for (ArrayRef<OMPClause *>::iterator I = S->clauses().begin(),
625 E = S->clauses().end();
626 I != E; ++I)
627 if (OMPClause *C = *I)
628 for (StmtRange R = C->children(); R; ++R)
629 if (Stmt *Child = *R)
630 Visit(Child);
631 }
632 void VisitStmt(Stmt *S) {
633 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
634 I != E; ++I)
635 if (Stmt *Child = *I)
636 if (!isa<OMPExecutableDirective>(Child))
637 Visit(Child);
638 }
639
640 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd195bc32013-10-01 05:32:34 +0000641 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev0c018352013-09-06 18:03:48 +0000642
643 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
644 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { }
645};
646}
647
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000648StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
649 ArrayRef<OMPClause *> Clauses,
650 Stmt *AStmt,
651 SourceLocation StartLoc,
652 SourceLocation EndLoc) {
Alexey Bataev0c018352013-09-06 18:03:48 +0000653 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
654
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000655 StmtResult Res = StmtError();
Alexey Bataev0c018352013-09-06 18:03:48 +0000656
657 // Check default data sharing attributes for referenced variables.
658 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
659 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
660 if (DSAChecker.isErrorFound())
661 return StmtError();
Alexey Bataevd195bc32013-10-01 05:32:34 +0000662 // Generate list of implicitly defined firstprivate variables.
663 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
664 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
665
666 bool ErrorFound = false;
667 if (!DSAChecker.getImplicitFirstprivate().empty()) {
668 if (OMPClause *Implicit =
669 ActOnOpenMPFirstprivateClause(DSAChecker.getImplicitFirstprivate(),
670 SourceLocation(), SourceLocation(),
671 SourceLocation())) {
672 ClausesWithImplicit.push_back(Implicit);
673 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
674 DSAChecker.getImplicitFirstprivate().size();
675 } else
676 ErrorFound = true;
677 }
Alexey Bataev0c018352013-09-06 18:03:48 +0000678
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000679 switch (Kind) {
680 case OMPD_parallel:
Alexey Bataevd195bc32013-10-01 05:32:34 +0000681 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt,
682 StartLoc, EndLoc);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000683 break;
684 case OMPD_threadprivate:
685 case OMPD_task:
686 llvm_unreachable("OpenMP Directive is not allowed");
687 case OMPD_unknown:
688 case NUM_OPENMP_DIRECTIVES:
689 llvm_unreachable("Unknown OpenMP directive");
690 }
Alexey Bataevd195bc32013-10-01 05:32:34 +0000691
692 if (ErrorFound) return StmtError();
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000693 return Res;
694}
695
696StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
697 Stmt *AStmt,
698 SourceLocation StartLoc,
699 SourceLocation EndLoc) {
700 getCurFunction()->setHasBranchProtectedScope();
701
702 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
703 Clauses, AStmt));
704}
705
706OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
707 unsigned Argument,
708 SourceLocation ArgumentLoc,
709 SourceLocation StartLoc,
710 SourceLocation LParenLoc,
711 SourceLocation EndLoc) {
712 OMPClause *Res = 0;
713 switch (Kind) {
714 case OMPC_default:
Alexey Bataev0c018352013-09-06 18:03:48 +0000715 Res =
716 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
717 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000718 break;
719 case OMPC_private:
Alexey Bataevd195bc32013-10-01 05:32:34 +0000720 case OMPC_firstprivate:
Alexey Bataev0c018352013-09-06 18:03:48 +0000721 case OMPC_shared:
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000722 case OMPC_threadprivate:
723 case OMPC_unknown:
724 case NUM_OPENMP_CLAUSES:
725 llvm_unreachable("Clause is not allowed.");
726 }
727 return Res;
728}
729
730OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
731 SourceLocation KindKwLoc,
732 SourceLocation StartLoc,
733 SourceLocation LParenLoc,
734 SourceLocation EndLoc) {
735 if (Kind == OMPC_DEFAULT_unknown) {
736 std::string Values;
737 std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : "");
738 for (unsigned i = OMPC_DEFAULT_unknown + 1;
739 i < NUM_OPENMP_DEFAULT_KINDS; ++i) {
740 Values += "'";
741 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
742 Values += "'";
743 switch (i) {
744 case NUM_OPENMP_DEFAULT_KINDS - 2:
745 Values += " or ";
746 break;
747 case NUM_OPENMP_DEFAULT_KINDS - 1:
748 break;
749 default:
750 Values += Sep;
751 break;
752 }
753 }
754 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
755 << Values << getOpenMPClauseName(OMPC_default);
756 return 0;
757 }
Alexey Bataev0c018352013-09-06 18:03:48 +0000758 switch (Kind) {
759 case OMPC_DEFAULT_none:
760 DSAStack->setDefaultDSANone();
761 break;
762 case OMPC_DEFAULT_shared:
763 DSAStack->setDefaultDSAShared();
764 break;
765 default:
766 break;
767 }
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000768 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
769 EndLoc);
770}
771
772OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
773 ArrayRef<Expr *> VarList,
774 SourceLocation StartLoc,
775 SourceLocation LParenLoc,
776 SourceLocation EndLoc) {
777 OMPClause *Res = 0;
778 switch (Kind) {
779 case OMPC_private:
780 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
781 break;
Alexey Bataevd195bc32013-10-01 05:32:34 +0000782 case OMPC_firstprivate:
783 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
784 break;
Alexey Bataev0c018352013-09-06 18:03:48 +0000785 case OMPC_shared:
786 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
787 break;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000788 case OMPC_default:
789 case OMPC_threadprivate:
790 case OMPC_unknown:
791 case NUM_OPENMP_CLAUSES:
792 llvm_unreachable("Clause is not allowed.");
793 }
794 return Res;
795}
796
797OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
798 SourceLocation StartLoc,
799 SourceLocation LParenLoc,
800 SourceLocation EndLoc) {
801 SmallVector<Expr *, 8> Vars;
802 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
803 I != E; ++I) {
Alexey Bataev543c4ae2013-09-24 03:17:45 +0000804 assert(*I && "NULL expr in OpenMP private clause.");
805 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000806 // It will be analyzed later.
807 Vars.push_back(*I);
808 continue;
809 }
810
811 SourceLocation ELoc = (*I)->getExprLoc();
812 // OpenMP [2.1, C/C++]
813 // A list item is a variable name.
814 // OpenMP [2.9.3.3, Restrictions, p.1]
815 // A variable that is part of another variable (as an array or
816 // structure element) cannot appear in a private clause.
817 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
818 if (!DE || !isa<VarDecl>(DE->getDecl())) {
819 Diag(ELoc, diag::err_omp_expected_var_name)
820 << (*I)->getSourceRange();
821 continue;
822 }
823 Decl *D = DE->getDecl();
824 VarDecl *VD = cast<VarDecl>(D);
825
826 QualType Type = VD->getType();
827 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
828 // It will be analyzed later.
829 Vars.push_back(DE);
830 continue;
831 }
832
833 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
834 // A variable that appears in a private clause must not have an incomplete
835 // type or a reference type.
836 if (RequireCompleteType(ELoc, Type,
837 diag::err_omp_private_incomplete_type)) {
838 continue;
839 }
840 if (Type->isReferenceType()) {
841 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
842 << getOpenMPClauseName(OMPC_private) << Type;
843 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
844 VarDecl::DeclarationOnly;
845 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
846 diag::note_defined_here) << VD;
847 continue;
848 }
849
850 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
851 // A variable of class type (or array thereof) that appears in a private
852 // clause requires an accesible, unambiguous default constructor for the
853 // class type.
854 while (Type.getNonReferenceType()->isArrayType()) {
855 Type = cast<ArrayType>(
856 Type.getNonReferenceType().getTypePtr())->getElementType();
857 }
858 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
859 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
860 if (RD) {
861 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
862 PartialDiagnostic PD =
863 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
864 if (!CD ||
865 CheckConstructorAccess(ELoc, CD,
866 InitializedEntity::InitializeTemporary(Type),
867 CD->getAccess(), PD) == AR_inaccessible ||
868 CD->isDeleted()) {
869 Diag(ELoc, diag::err_omp_required_method)
870 << getOpenMPClauseName(OMPC_private) << 0;
871 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
872 VarDecl::DeclarationOnly;
873 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
874 diag::note_defined_here) << VD;
875 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
876 continue;
877 }
878 MarkFunctionReferenced(ELoc, CD);
879 DiagnoseUseOfDecl(CD, ELoc);
880
881 CXXDestructorDecl *DD = RD->getDestructor();
882 if (DD) {
883 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
884 DD->isDeleted()) {
885 Diag(ELoc, diag::err_omp_required_method)
886 << getOpenMPClauseName(OMPC_private) << 4;
887 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
888 VarDecl::DeclarationOnly;
889 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
890 diag::note_defined_here) << VD;
891 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
892 continue;
893 }
894 MarkFunctionReferenced(ELoc, DD);
895 DiagnoseUseOfDecl(DD, ELoc);
896 }
897 }
898
Alexey Bataev0c018352013-09-06 18:03:48 +0000899 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
900 // in a Construct]
901 // Variables with the predetermined data-sharing attributes may not be
902 // listed in data-sharing attributes clauses, except for the cases
903 // listed below. For these exceptions only, listing a predetermined
904 // variable in a data-sharing attribute clause is allowed and overrides
905 // the variable's predetermined data-sharing attributes.
906 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
907 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
908 Diag(ELoc, diag::err_omp_wrong_dsa)
909 << getOpenMPClauseName(DVar.CKind)
910 << getOpenMPClauseName(OMPC_private);
911 if (DVar.RefExpr) {
912 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
913 << getOpenMPClauseName(DVar.CKind);
914 } else {
915 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
916 << getOpenMPClauseName(DVar.CKind);
917 }
918 continue;
919 }
920
921 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000922 Vars.push_back(DE);
923 }
924
925 if (Vars.empty()) return 0;
926
927 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
928}
929
Alexey Bataevd195bc32013-10-01 05:32:34 +0000930OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
931 SourceLocation StartLoc,
932 SourceLocation LParenLoc,
933 SourceLocation EndLoc) {
934 SmallVector<Expr *, 8> Vars;
935 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
936 I != E; ++I) {
937 assert(*I && "NULL expr in OpenMP firstprivate clause.");
938 if (isa<DependentScopeDeclRefExpr>(*I)) {
939 // It will be analyzed later.
940 Vars.push_back(*I);
941 continue;
942 }
943
944 SourceLocation ELoc = (*I)->getExprLoc();
945 // OpenMP [2.1, C/C++]
946 // A list item is a variable name.
947 // OpenMP [2.9.3.3, Restrictions, p.1]
948 // A variable that is part of another variable (as an array or
949 // structure element) cannot appear in a private clause.
950 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
951 if (!DE || !isa<VarDecl>(DE->getDecl())) {
952 Diag(ELoc, diag::err_omp_expected_var_name)
953 << (*I)->getSourceRange();
954 continue;
955 }
956 Decl *D = DE->getDecl();
957 VarDecl *VD = cast<VarDecl>(D);
958
959 QualType Type = VD->getType();
960 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
961 // It will be analyzed later.
962 Vars.push_back(DE);
963 continue;
964 }
965
966 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
967 // A variable that appears in a private clause must not have an incomplete
968 // type or a reference type.
969 if (RequireCompleteType(ELoc, Type,
970 diag::err_omp_firstprivate_incomplete_type)) {
971 continue;
972 }
973 if (Type->isReferenceType()) {
974 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
975 << getOpenMPClauseName(OMPC_firstprivate) << Type;
976 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
977 VarDecl::DeclarationOnly;
978 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
979 diag::note_defined_here) << VD;
980 continue;
981 }
982
983 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
984 // A variable of class type (or array thereof) that appears in a private
985 // clause requires an accesible, unambiguous copy constructor for the
986 // class type.
987 Type = Context.getBaseElementType(Type);
988 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
989 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
990 if (RD) {
991 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
992 PartialDiagnostic PD =
993 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
994 if (!CD ||
995 CheckConstructorAccess(ELoc, CD,
996 InitializedEntity::InitializeTemporary(Type),
997 CD->getAccess(), PD) == AR_inaccessible ||
998 CD->isDeleted()) {
999 Diag(ELoc, diag::err_omp_required_method)
1000 << getOpenMPClauseName(OMPC_firstprivate) << 1;
1001 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1002 VarDecl::DeclarationOnly;
1003 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1004 diag::note_defined_here) << VD;
1005 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1006 continue;
1007 }
1008 MarkFunctionReferenced(ELoc, CD);
1009 DiagnoseUseOfDecl(CD, ELoc);
1010
1011 CXXDestructorDecl *DD = RD->getDestructor();
1012 if (DD) {
1013 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1014 DD->isDeleted()) {
1015 Diag(ELoc, diag::err_omp_required_method)
1016 << getOpenMPClauseName(OMPC_firstprivate) << 4;
1017 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1018 VarDecl::DeclarationOnly;
1019 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1020 diag::note_defined_here) << VD;
1021 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1022 continue;
1023 }
1024 MarkFunctionReferenced(ELoc, DD);
1025 DiagnoseUseOfDecl(DD, ELoc);
1026 }
1027 }
1028
1029 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1030 // variable and it was checked already.
1031 if (StartLoc.isValid() && EndLoc.isValid()) {
1032 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1033 Type = Type.getNonReferenceType().getCanonicalType();
1034 bool IsConstant = Type.isConstant(Context);
1035 Type = Context.getBaseElementType(Type);
1036 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1037 // A list item that specifies a given variable may not appear in more
1038 // than one clause on the same directive, except that a variable may be
1039 // specified in both firstprivate and lastprivate clauses.
1040 // TODO: add processing for lastprivate.
1041 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1042 DVar.RefExpr) {
1043 Diag(ELoc, diag::err_omp_wrong_dsa)
1044 << getOpenMPClauseName(DVar.CKind)
1045 << getOpenMPClauseName(OMPC_firstprivate);
1046 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1047 << getOpenMPClauseName(DVar.CKind);
1048 continue;
1049 }
1050
1051 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1052 // in a Construct]
1053 // Variables with the predetermined data-sharing attributes may not be
1054 // listed in data-sharing attributes clauses, except for the cases
1055 // listed below. For these exceptions only, listing a predetermined
1056 // variable in a data-sharing attribute clause is allowed and overrides
1057 // the variable's predetermined data-sharing attributes.
1058 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1059 // in a Construct, C/C++, p.2]
1060 // Variables with const-qualified type having no mutable member may be
1061 // listed in a firstprivate clause, even if they are static data members.
1062 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1063 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1064 Diag(ELoc, diag::err_omp_wrong_dsa)
1065 << getOpenMPClauseName(DVar.CKind)
1066 << getOpenMPClauseName(OMPC_firstprivate);
1067 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1068 << getOpenMPClauseName(DVar.CKind);
1069 continue;
1070 }
1071
1072 // OpenMP [2.9.3.4, Restrictions, p.2]
1073 // A list item that is private within a parallel region must not appear
1074 // in a firstprivate clause on a worksharing construct if any of the
1075 // worksharing regions arising from the worksharing construct ever bind
1076 // to any of the parallel regions arising from the parallel construct.
1077 // OpenMP [2.9.3.4, Restrictions, p.3]
1078 // A list item that appears in a reduction clause of a parallel construct
1079 // must not appear in a firstprivate clause on a worksharing or task
1080 // construct if any of the worksharing or task regions arising from the
1081 // worksharing or task construct ever bind to any of the parallel regions
1082 // arising from the parallel construct.
1083 // OpenMP [2.9.3.4, Restrictions, p.4]
1084 // A list item that appears in a reduction clause in worksharing
1085 // construct must not appear in a firstprivate clause in a task construct
1086 // encountered during execution of any of the worksharing regions arising
1087 // from the worksharing construct.
1088 // TODO:
1089 }
1090
1091 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1092 Vars.push_back(DE);
1093 }
1094
1095 if (Vars.empty()) return 0;
1096
1097 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1098 Vars);
1099}
1100
Alexey Bataev0c018352013-09-06 18:03:48 +00001101OMPClause *Sema::ActOnOpenMPSharedClause(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) {
Alexey Bataev543c4ae2013-09-24 03:17:45 +00001108 assert(*I && "NULL expr in OpenMP shared clause.");
1109 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev0c018352013-09-06 18:03:48 +00001110 // 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.4, 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<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.1.1, Data-sharing Attribute Rules for Variables Referenced
1138 // in a Construct]
1139 // Variables with the predetermined data-sharing attributes may not be
1140 // listed in data-sharing attributes clauses, except for the cases
1141 // listed below. For these exceptions only, listing a predetermined
1142 // variable in a data-sharing attribute clause is allowed and overrides
1143 // the variable's predetermined data-sharing attributes.
1144 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1145 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1146 Diag(ELoc, diag::err_omp_wrong_dsa)
1147 << getOpenMPClauseName(DVar.CKind)
1148 << getOpenMPClauseName(OMPC_shared);
1149 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1150 << getOpenMPClauseName(DVar.CKind);
1151 continue;
1152 }
1153
1154 DSAStack->addDSA(VD, DE, OMPC_shared);
1155 Vars.push_back(DE);
1156 }
1157
1158 if (Vars.empty()) return 0;
1159
1160 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1161}
1162
1163#undef DSAStack