1 //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \brief This file implements semantic analysis for OpenMP directives and
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/OpenMPKinds.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclOpenMP.h"
19 #include "clang/AST/StmtCXX.h"
20 #include "clang/AST/StmtOpenMP.h"
21 #include "clang/AST/StmtVisitor.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Sema/Initialization.h"
24 #include "clang/Sema/SemaInternal.h"
25 #include "clang/Sema/Lookup.h"
26 #include "clang/Sema/Scope.h"
27 #include "clang/Sema/ScopeInfo.h"
28 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 // Stack of data-sharing attributes for variables
32 //===----------------------------------------------------------------------===//
35 /// \brief Default data sharing attributes, which can be applied to directive.
36 enum 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'.
42 /// \brief Stack for tracking declarations used in OpenMP directives and
43 /// clauses and their data-sharing attributes.
47 OpenMPDirectiveKind DKind;
48 OpenMPClauseKind CKind;
50 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(0) { }
54 OpenMPClauseKind Attributes;
57 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
60 DeclSAMapTy SharingMap;
61 DefaultDataSharingAttributes DefaultAttr;
62 OpenMPDirectiveKind Directive;
63 DeclarationNameInfo DirectiveName;
65 SharingMapTy(OpenMPDirectiveKind DKind,
66 const DeclarationNameInfo &Name,
68 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
69 DirectiveName(Name), CurScope(CurScope) { }
71 : SharingMap(), DefaultAttr(DSA_unspecified),
72 Directive(OMPD_unknown), DirectiveName(),
76 typedef SmallVector<SharingMapTy, 64> StackTy;
78 /// \brief Stack of used declaration and their data-sharing attributes.
82 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
84 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
86 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) { }
88 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
90 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
94 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
98 /// \brief Adds explicit data sharing attribute to the specified declaration.
99 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
101 /// \brief Checks if the variable is a local for OpenMP region.
102 bool isOpenMPLocal(VarDecl *D);
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);
109 /// \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);
115 /// \brief Returns currently analyzed directive.
116 OpenMPDirectiveKind getCurrentDirective() const {
117 return Stack.back().Directive;
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; }
125 DefaultDataSharingAttributes getDefaultDSA() const {
126 return Stack.back().DefaultAttr;
129 Scope *getCurScope() { return Stack.back().CurScope; }
131 } // end anonymous namespace.
133 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
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
143 if (!D->isFunctionOrMethodVarDecl())
144 DVar.CKind = OMPC_shared;
146 // 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;
155 DVar.DKind = Iter->Directive;
156 // Explicitly specified attributes and local variables with predetermined
158 if (Iter->SharingMap.count(D)) {
159 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
160 DVar.CKind = Iter->SharingMap[D].Attributes;
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) {
170 DVar.CKind = OMPC_shared;
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;
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.
190 if (DVar.DKind == OMPD_task) {
192 for (StackTy::reverse_iterator I = Iter + 1,
193 EE = Stack.rend() - 1;
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
200 DVarTemp = getDSA(I, D);
201 if (DVarTemp.CKind != OMPC_shared) {
203 DVar.DKind = OMPD_task;
204 DVar.CKind = OMPC_firstprivate;
207 if (I->Directive == OMPD_parallel) break;
209 DVar.DKind = OMPD_task;
211 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
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
220 return getDSA(Iter + 1, D);
223 void 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;
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;
234 bool 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();
247 isa<CapturedDecl>(D->getDeclContext()) &&
248 CurScope->getFnParent()->getEntity()->Encloses(D->getDeclContext());
250 return isOpenMPLocal;
253 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
256 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
257 // in a Construct, C/C++, predetermined, p.1]
258 // Variables appearing in threadprivate directives are threadprivate.
259 if (D->getTLSKind() != VarDecl::TLS_None) {
260 DVar.CKind = OMPC_threadprivate;
263 if (Stack[0].SharingMap.count(D)) {
264 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
265 DVar.CKind = OMPC_threadprivate;
269 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
270 // in a Construct, C/C++, predetermined, p.1]
271 // Variables with automatic storage duration that are declared in a scope
272 // inside the construct are private.
273 if (isOpenMPLocal(D) && D->isLocalVarDecl() &&
274 (D->getStorageClass() == SC_Auto ||
275 D->getStorageClass() == SC_None)) {
276 DVar.CKind = OMPC_private;
280 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
281 // in a Construct, C/C++, predetermined, p.4]
282 // Static data memebers are shared.
283 if (D->isStaticDataMember()) {
284 // Variables with const-qualified type having no mutable member may be listed
285 // in a firstprivate clause, even if they are static data members.
286 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
287 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
290 DVar.CKind = OMPC_shared;
294 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
295 bool IsConstant = Type.isConstant(Actions.getASTContext());
296 while (Type->isArrayType()) {
297 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
298 Type = ElemType.getNonReferenceType().getCanonicalType();
300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
301 // in a Construct, C/C++, predetermined, p.6]
302 // Variables with const qualified type having no mutable member are
304 CXXRecordDecl *RD = Actions.getLangOpts().CPlusPlus ?
305 Type->getAsCXXRecordDecl() : 0;
307 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
308 // Variables with const-qualified type having no mutable member may be
309 // listed in a firstprivate clause, even if they are static data members.
310 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
311 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
314 DVar.CKind = OMPC_shared;
318 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
319 // in a Construct, C/C++, predetermined, p.7]
320 // Variables with static storage duration that are declared in a scope
321 // inside the construct are shared.
322 if (isOpenMPLocal(D) && D->isStaticLocal()) {
323 DVar.CKind = OMPC_shared;
327 // Explicitly specified attributes and local variables with predetermined
329 if (Stack.back().SharingMap.count(D)) {
330 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
331 DVar.CKind = Stack.back().SharingMap[D].Attributes;
337 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
338 return getDSA(Stack.rbegin() + 1, D);
341 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
342 OpenMPDirectiveKind DKind) {
343 for (StackTy::reverse_iterator I = Stack.rbegin() + 1,
344 E = Stack.rend() - 1;
346 if (DKind != OMPD_unknown && DKind != I->Directive) continue;
347 DSAVarData DVar = getDSA(I, D);
348 if (DVar.CKind == CKind)
354 void Sema::InitDataSharingAttributesStack() {
355 VarDataSharingAttributesStack = new DSAStackTy(*this);
358 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
360 void Sema::DestroyDataSharingAttributesStack() {
364 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
365 const DeclarationNameInfo &DirName,
367 DSAStack->push(DKind, DirName, CurScope);
368 PushExpressionEvaluationContext(PotentiallyEvaluated);
371 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
373 DiscardCleanupsInEvaluationContext();
374 PopExpressionEvaluationContext();
379 class VarDeclFilterCCC : public CorrectionCandidateCallback {
383 VarDeclFilterCCC(Sema &S) : Actions(S) { }
384 virtual bool ValidateCandidate(const TypoCorrection &Candidate) {
385 NamedDecl *ND = Candidate.getCorrectionDecl();
386 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
387 return VD->hasGlobalStorage() &&
388 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
389 Actions.getCurScope());
396 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
397 CXXScopeSpec &ScopeSpec,
398 const DeclarationNameInfo &Id) {
399 LookupResult Lookup(*this, Id, LookupOrdinaryName);
400 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
402 if (Lookup.isAmbiguous())
406 if (!Lookup.isSingleResult()) {
407 VarDeclFilterCCC Validator(*this);
408 if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope,
410 diagnoseTypo(Corrected,
411 PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest
412 : diag::err_omp_expected_var_arg_suggest)
414 VD = Corrected.getCorrectionDeclAs<VarDecl>();
416 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
417 : diag::err_omp_expected_var_arg)
422 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
423 Diag(Id.getLoc(), diag::err_omp_expected_var_arg)
425 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
429 Lookup.suppressDiagnostics();
431 // OpenMP [2.9.2, Syntax, C/C++]
432 // Variables must be file-scope, namespace-scope, or static block-scope.
433 if (!VD->hasGlobalStorage()) {
434 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
435 << getOpenMPDirectiveName(OMPD_threadprivate)
436 << !VD->isStaticLocal();
437 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
438 VarDecl::DeclarationOnly;
439 Diag(VD->getLocation(),
440 IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD;
444 VarDecl *CanonicalVD = VD->getCanonicalDecl();
445 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
446 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
447 // A threadprivate directive for file-scope variables must appear outside
448 // any definition or declaration.
449 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
450 !getCurLexicalContext()->isTranslationUnit()) {
451 Diag(Id.getLoc(), diag::err_omp_var_scope)
452 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
453 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
454 VarDecl::DeclarationOnly;
455 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
456 diag::note_defined_here) << VD;
459 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
460 // A threadprivate directive for static class member variables must appear
461 // in the class definition, in the same scope in which the member
462 // variables are declared.
463 if (CanonicalVD->isStaticDataMember() &&
464 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
465 Diag(Id.getLoc(), diag::err_omp_var_scope)
466 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
467 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
468 VarDecl::DeclarationOnly;
469 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
470 diag::note_defined_here) << VD;
473 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
474 // A threadprivate directive for namespace-scope variables must appear
475 // outside any definition or declaration other than the namespace
476 // definition itself.
477 if (CanonicalVD->getDeclContext()->isNamespace() &&
478 (!getCurLexicalContext()->isFileContext() ||
479 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
480 Diag(Id.getLoc(), diag::err_omp_var_scope)
481 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
482 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
483 VarDecl::DeclarationOnly;
484 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
485 diag::note_defined_here) << VD;
488 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
489 // A threadprivate directive for static block-scope variables must appear
490 // in the scope of the variable and not in a nested scope.
491 if (CanonicalVD->isStaticLocal() && CurScope &&
492 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
493 Diag(Id.getLoc(), diag::err_omp_var_scope)
494 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
495 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
496 VarDecl::DeclarationOnly;
497 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
498 diag::note_defined_here) << VD;
502 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
503 // A threadprivate directive must lexically precede all references to any
504 // of the variables in its list.
506 Diag(Id.getLoc(), diag::err_omp_var_used)
507 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
511 QualType ExprType = VD->getType().getNonReferenceType();
512 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_RValue, Id.getLoc());
513 DSAStack->addDSA(VD, cast<DeclRefExpr>(DE.get()), OMPC_threadprivate);
517 Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective(
519 ArrayRef<Expr *> VarList) {
520 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
521 CurContext->addDecl(D);
522 return DeclGroupPtrTy::make(DeclGroupRef(D));
524 return DeclGroupPtrTy();
527 OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl(
529 ArrayRef<Expr *> VarList) {
530 SmallVector<Expr *, 8> Vars;
531 for (ArrayRef<Expr *>::iterator I = VarList.begin(),
534 DeclRefExpr *DE = cast<DeclRefExpr>(*I);
535 VarDecl *VD = cast<VarDecl>(DE->getDecl());
536 SourceLocation ILoc = DE->getExprLoc();
538 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
539 // A threadprivate variable must not have an incomplete type.
540 if (RequireCompleteType(ILoc, VD->getType(),
541 diag::err_omp_threadprivate_incomplete_type)) {
545 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
546 // A threadprivate variable must not have a reference type.
547 if (VD->getType()->isReferenceType()) {
548 Diag(ILoc, diag::err_omp_ref_type_arg)
549 << getOpenMPDirectiveName(OMPD_threadprivate)
551 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
552 VarDecl::DeclarationOnly;
553 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
554 diag::note_defined_here) << VD;
558 // Check if this is a TLS variable.
559 if (VD->getTLSKind()) {
560 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
561 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
562 VarDecl::DeclarationOnly;
563 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
564 diag::note_defined_here) << VD;
570 return Vars.empty() ?
571 0 : OMPThreadPrivateDecl::Create(Context,
572 getCurLexicalContext(),
577 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
582 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
584 void VisitDeclRefExpr(DeclRefExpr *E) {
585 if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
586 // Skip internally declared variables.
587 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) return;
589 SourceLocation ELoc = E->getExprLoc();
591 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
592 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
593 if (DVar.CKind != OMPC_unknown) {
594 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
595 DVar.CKind != OMPC_threadprivate && !DVar.RefExpr)
596 ImplicitFirstprivate.push_back(DVar.RefExpr);
599 // The default(none) clause requires that each variable that is referenced
600 // in the construct, and does not have a predetermined data-sharing
601 // attribute, must have its data-sharing attribute explicitly determined
602 // by being listed in a data-sharing attribute clause.
603 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
604 (DKind == OMPD_parallel || DKind == OMPD_task)) {
606 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
610 // OpenMP [2.9.3.6, Restrictions, p.2]
611 // A list item that appears in a reduction clause of the innermost
612 // enclosing worksharing or parallel construct may not be accessed in an
616 // Define implicit data-sharing attributes for task.
617 DVar = Stack->getImplicitDSA(VD);
618 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
619 ImplicitFirstprivate.push_back(DVar.RefExpr);
622 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
623 for (ArrayRef<OMPClause *>::iterator I = S->clauses().begin(),
624 E = S->clauses().end();
626 if (OMPClause *C = *I)
627 for (StmtRange R = C->children(); R; ++R)
628 if (Stmt *Child = *R)
631 void VisitStmt(Stmt *S) {
632 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
634 if (Stmt *Child = *I)
635 if (!isa<OMPExecutableDirective>(Child))
639 bool isErrorFound() { return ErrorFound; }
640 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
642 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
643 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { }
647 StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
648 ArrayRef<OMPClause *> Clauses,
650 SourceLocation StartLoc,
651 SourceLocation EndLoc) {
652 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
654 StmtResult Res = StmtError();
656 // Check default data sharing attributes for referenced variables.
657 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
658 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
659 if (DSAChecker.isErrorFound())
661 // Generate list of implicitly defined firstprivate variables.
662 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
663 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
665 bool ErrorFound = false;
666 if (!DSAChecker.getImplicitFirstprivate().empty()) {
667 if (OMPClause *Implicit =
668 ActOnOpenMPFirstprivateClause(DSAChecker.getImplicitFirstprivate(),
669 SourceLocation(), SourceLocation(),
671 ClausesWithImplicit.push_back(Implicit);
672 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
673 DSAChecker.getImplicitFirstprivate().size();
680 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt,
683 case OMPD_threadprivate:
685 llvm_unreachable("OpenMP Directive is not allowed");
687 case NUM_OPENMP_DIRECTIVES:
688 llvm_unreachable("Unknown OpenMP directive");
691 if (ErrorFound) return StmtError();
695 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
697 SourceLocation StartLoc,
698 SourceLocation EndLoc) {
699 getCurFunction()->setHasBranchProtectedScope();
701 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
705 OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
707 SourceLocation ArgumentLoc,
708 SourceLocation StartLoc,
709 SourceLocation LParenLoc,
710 SourceLocation EndLoc) {
715 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
716 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
719 case OMPC_firstprivate:
721 case OMPC_threadprivate:
723 case NUM_OPENMP_CLAUSES:
724 llvm_unreachable("Clause is not allowed.");
729 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
730 SourceLocation KindKwLoc,
731 SourceLocation StartLoc,
732 SourceLocation LParenLoc,
733 SourceLocation EndLoc) {
734 if (Kind == OMPC_DEFAULT_unknown) {
736 std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : "");
737 for (unsigned i = OMPC_DEFAULT_unknown + 1;
738 i < NUM_OPENMP_DEFAULT_KINDS; ++i) {
740 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
743 case NUM_OPENMP_DEFAULT_KINDS - 2:
746 case NUM_OPENMP_DEFAULT_KINDS - 1:
753 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
754 << Values << getOpenMPClauseName(OMPC_default);
758 case OMPC_DEFAULT_none:
759 DSAStack->setDefaultDSANone();
761 case OMPC_DEFAULT_shared:
762 DSAStack->setDefaultDSAShared();
767 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
771 OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
772 ArrayRef<Expr *> VarList,
773 SourceLocation StartLoc,
774 SourceLocation LParenLoc,
775 SourceLocation EndLoc) {
779 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
781 case OMPC_firstprivate:
782 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
785 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
788 case OMPC_threadprivate:
790 case NUM_OPENMP_CLAUSES:
791 llvm_unreachable("Clause is not allowed.");
796 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
797 SourceLocation StartLoc,
798 SourceLocation LParenLoc,
799 SourceLocation EndLoc) {
800 SmallVector<Expr *, 8> Vars;
801 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
803 assert(*I && "NULL expr in OpenMP private clause.");
804 if (isa<DependentScopeDeclRefExpr>(*I)) {
805 // It will be analyzed later.
810 SourceLocation ELoc = (*I)->getExprLoc();
811 // OpenMP [2.1, C/C++]
812 // A list item is a variable name.
813 // OpenMP [2.9.3.3, Restrictions, p.1]
814 // A variable that is part of another variable (as an array or
815 // structure element) cannot appear in a private clause.
816 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
817 if (!DE || !isa<VarDecl>(DE->getDecl())) {
818 Diag(ELoc, diag::err_omp_expected_var_name)
819 << (*I)->getSourceRange();
822 Decl *D = DE->getDecl();
823 VarDecl *VD = cast<VarDecl>(D);
825 QualType Type = VD->getType();
826 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
827 // It will be analyzed later.
832 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
833 // A variable that appears in a private clause must not have an incomplete
834 // type or a reference type.
835 if (RequireCompleteType(ELoc, Type,
836 diag::err_omp_private_incomplete_type)) {
839 if (Type->isReferenceType()) {
840 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
841 << getOpenMPClauseName(OMPC_private) << Type;
842 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
843 VarDecl::DeclarationOnly;
844 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
845 diag::note_defined_here) << VD;
849 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
850 // A variable of class type (or array thereof) that appears in a private
851 // clause requires an accesible, unambiguous default constructor for the
853 while (Type.getNonReferenceType()->isArrayType()) {
854 Type = cast<ArrayType>(
855 Type.getNonReferenceType().getTypePtr())->getElementType();
857 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
858 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
860 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
861 PartialDiagnostic PD =
862 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
864 CheckConstructorAccess(ELoc, CD,
865 InitializedEntity::InitializeTemporary(Type),
866 CD->getAccess(), PD) == AR_inaccessible ||
868 Diag(ELoc, diag::err_omp_required_method)
869 << getOpenMPClauseName(OMPC_private) << 0;
870 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
871 VarDecl::DeclarationOnly;
872 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
873 diag::note_defined_here) << VD;
874 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
877 MarkFunctionReferenced(ELoc, CD);
878 DiagnoseUseOfDecl(CD, ELoc);
880 CXXDestructorDecl *DD = RD->getDestructor();
882 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
884 Diag(ELoc, diag::err_omp_required_method)
885 << getOpenMPClauseName(OMPC_private) << 4;
886 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
887 VarDecl::DeclarationOnly;
888 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
889 diag::note_defined_here) << VD;
890 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
893 MarkFunctionReferenced(ELoc, DD);
894 DiagnoseUseOfDecl(DD, ELoc);
898 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
900 // Variables with the predetermined data-sharing attributes may not be
901 // listed in data-sharing attributes clauses, except for the cases
902 // listed below. For these exceptions only, listing a predetermined
903 // variable in a data-sharing attribute clause is allowed and overrides
904 // the variable's predetermined data-sharing attributes.
905 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
906 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
907 Diag(ELoc, diag::err_omp_wrong_dsa)
908 << getOpenMPClauseName(DVar.CKind)
909 << getOpenMPClauseName(OMPC_private);
911 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
912 << getOpenMPClauseName(DVar.CKind);
914 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
915 << getOpenMPClauseName(DVar.CKind);
920 DSAStack->addDSA(VD, DE, OMPC_private);
924 if (Vars.empty()) return 0;
926 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
929 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
930 SourceLocation StartLoc,
931 SourceLocation LParenLoc,
932 SourceLocation EndLoc) {
933 SmallVector<Expr *, 8> Vars;
934 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
936 assert(*I && "NULL expr in OpenMP firstprivate clause.");
937 if (isa<DependentScopeDeclRefExpr>(*I)) {
938 // It will be analyzed later.
943 SourceLocation ELoc = (*I)->getExprLoc();
944 // OpenMP [2.1, C/C++]
945 // A list item is a variable name.
946 // OpenMP [2.9.3.3, Restrictions, p.1]
947 // A variable that is part of another variable (as an array or
948 // structure element) cannot appear in a private clause.
949 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
950 if (!DE || !isa<VarDecl>(DE->getDecl())) {
951 Diag(ELoc, diag::err_omp_expected_var_name)
952 << (*I)->getSourceRange();
955 Decl *D = DE->getDecl();
956 VarDecl *VD = cast<VarDecl>(D);
958 QualType Type = VD->getType();
959 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
960 // It will be analyzed later.
965 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
966 // A variable that appears in a private clause must not have an incomplete
967 // type or a reference type.
968 if (RequireCompleteType(ELoc, Type,
969 diag::err_omp_firstprivate_incomplete_type)) {
972 if (Type->isReferenceType()) {
973 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
974 << getOpenMPClauseName(OMPC_firstprivate) << Type;
975 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
976 VarDecl::DeclarationOnly;
977 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
978 diag::note_defined_here) << VD;
982 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
983 // A variable of class type (or array thereof) that appears in a private
984 // clause requires an accesible, unambiguous copy constructor for the
986 Type = Context.getBaseElementType(Type);
987 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
988 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
990 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
991 PartialDiagnostic PD =
992 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
994 CheckConstructorAccess(ELoc, CD,
995 InitializedEntity::InitializeTemporary(Type),
996 CD->getAccess(), PD) == AR_inaccessible ||
998 Diag(ELoc, diag::err_omp_required_method)
999 << getOpenMPClauseName(OMPC_firstprivate) << 1;
1000 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1001 VarDecl::DeclarationOnly;
1002 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1003 diag::note_defined_here) << VD;
1004 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1007 MarkFunctionReferenced(ELoc, CD);
1008 DiagnoseUseOfDecl(CD, ELoc);
1010 CXXDestructorDecl *DD = RD->getDestructor();
1012 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1014 Diag(ELoc, diag::err_omp_required_method)
1015 << getOpenMPClauseName(OMPC_firstprivate) << 4;
1016 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1017 VarDecl::DeclarationOnly;
1018 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1019 diag::note_defined_here) << VD;
1020 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1023 MarkFunctionReferenced(ELoc, DD);
1024 DiagnoseUseOfDecl(DD, ELoc);
1028 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1029 // variable and it was checked already.
1030 if (StartLoc.isValid() && EndLoc.isValid()) {
1031 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1032 Type = Type.getNonReferenceType().getCanonicalType();
1033 bool IsConstant = Type.isConstant(Context);
1034 Type = Context.getBaseElementType(Type);
1035 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1036 // A list item that specifies a given variable may not appear in more
1037 // than one clause on the same directive, except that a variable may be
1038 // specified in both firstprivate and lastprivate clauses.
1039 // TODO: add processing for lastprivate.
1040 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1042 Diag(ELoc, diag::err_omp_wrong_dsa)
1043 << getOpenMPClauseName(DVar.CKind)
1044 << getOpenMPClauseName(OMPC_firstprivate);
1045 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1046 << getOpenMPClauseName(DVar.CKind);
1050 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1052 // Variables with the predetermined data-sharing attributes may not be
1053 // listed in data-sharing attributes clauses, except for the cases
1054 // listed below. For these exceptions only, listing a predetermined
1055 // variable in a data-sharing attribute clause is allowed and overrides
1056 // the variable's predetermined data-sharing attributes.
1057 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1058 // in a Construct, C/C++, p.2]
1059 // Variables with const-qualified type having no mutable member may be
1060 // listed in a firstprivate clause, even if they are static data members.
1061 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1062 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1063 Diag(ELoc, diag::err_omp_wrong_dsa)
1064 << getOpenMPClauseName(DVar.CKind)
1065 << getOpenMPClauseName(OMPC_firstprivate);
1066 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1067 << getOpenMPClauseName(DVar.CKind);
1071 // OpenMP [2.9.3.4, Restrictions, p.2]
1072 // A list item that is private within a parallel region must not appear
1073 // in a firstprivate clause on a worksharing construct if any of the
1074 // worksharing regions arising from the worksharing construct ever bind
1075 // to any of the parallel regions arising from the parallel construct.
1076 // OpenMP [2.9.3.4, Restrictions, p.3]
1077 // A list item that appears in a reduction clause of a parallel construct
1078 // must not appear in a firstprivate clause on a worksharing or task
1079 // construct if any of the worksharing or task regions arising from the
1080 // worksharing or task construct ever bind to any of the parallel regions
1081 // arising from the parallel construct.
1082 // OpenMP [2.9.3.4, Restrictions, p.4]
1083 // A list item that appears in a reduction clause in worksharing
1084 // construct must not appear in a firstprivate clause in a task construct
1085 // encountered during execution of any of the worksharing regions arising
1086 // from the worksharing construct.
1090 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1094 if (Vars.empty()) return 0;
1096 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1100 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1101 SourceLocation StartLoc,
1102 SourceLocation LParenLoc,
1103 SourceLocation EndLoc) {
1104 SmallVector<Expr *, 8> Vars;
1105 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1107 assert(*I && "NULL expr in OpenMP shared clause.");
1108 if (isa<DependentScopeDeclRefExpr>(*I)) {
1109 // It will be analyzed later.
1114 SourceLocation ELoc = (*I)->getExprLoc();
1115 // OpenMP [2.1, C/C++]
1116 // A list item is a variable name.
1117 // OpenMP [2.9.3.4, Restrictions, p.1]
1118 // A variable that is part of another variable (as an array or
1119 // structure element) cannot appear in a private clause.
1120 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1121 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1122 Diag(ELoc, diag::err_omp_expected_var_name)
1123 << (*I)->getSourceRange();
1126 Decl *D = DE->getDecl();
1127 VarDecl *VD = cast<VarDecl>(D);
1129 QualType Type = VD->getType();
1130 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1131 // It will be analyzed later.
1136 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1138 // Variables with the predetermined data-sharing attributes may not be
1139 // listed in data-sharing attributes clauses, except for the cases
1140 // listed below. For these exceptions only, listing a predetermined
1141 // variable in a data-sharing attribute clause is allowed and overrides
1142 // the variable's predetermined data-sharing attributes.
1143 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1144 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1145 Diag(ELoc, diag::err_omp_wrong_dsa)
1146 << getOpenMPClauseName(DVar.CKind)
1147 << getOpenMPClauseName(OMPC_shared);
1148 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1149 << getOpenMPClauseName(DVar.CKind);
1153 DSAStack->addDSA(VD, DE, OMPC_shared);
1157 if (Vars.empty()) return 0;
1159 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);