1 //===--- 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 11 /// clauses. 12 /// 13 //===----------------------------------------------------------------------===// 14 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/Lookup.h" 25 #include "clang/Sema/Scope.h" 26 #include "clang/Sema/ScopeInfo.h" 27 #include "clang/Sema/SemaInternal.h" 28 using namespace clang; 29 30 //===----------------------------------------------------------------------===// 31 // Stack of data-sharing attributes for variables 32 //===----------------------------------------------------------------------===// 33 34 namespace { 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'. 40 }; 41 42 /// \brief Stack for tracking declarations used in OpenMP directives and 43 /// clauses and their data-sharing attributes. 44 class DSAStackTy { 45 public: 46 struct DSAVarData { 47 OpenMPDirectiveKind DKind; 48 OpenMPClauseKind CKind; 49 DeclRefExpr *RefExpr; 50 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(0) { } 51 }; 52 private: 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); 85 86 /// \brief Checks if the variable is a local for OpenMP region. 87 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); 88 public: 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 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); 113 114 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 133 DSAStackTy::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 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; 152 153 return DVar; 154 } 155 156 DVar.DKind = Iter->Directive; 157 // 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 170 // 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; 206 for (StackTy::reverse_iterator I = llvm::next(Iter), 207 EE = llvm::prior(Stack.rend()); 208 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; 218 DVar.CKind = OMPC_firstprivate; 219 return DVar; 220 } 221 if (I->Directive == OMPD_parallel) break; 222 } 223 DVar.DKind = OMPD_task; 224 DVar.CKind = 225 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 226 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. 234 return getDSA(llvm::next(Iter), D); 235 } 236 237 void 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 248 bool 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)) { 260 CurScope = CurScope->getParent(); 261 } 262 return CurScope != TopScope; 263 } 264 return false; 265 } 266 267 DSAStackTy::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. 287 OpenMPDirectiveKind Kind = getCurrentDirective(); 288 if (Kind != OMPD_parallel) { 289 if (isOpenMPLocal(D, llvm::next(Stack.rbegin())) && D->isLocalVarDecl() && 290 (D->getStorageClass() == SC_Auto || 291 D->getStorageClass() == SC_None)) 292 DVar.CKind = OMPC_private; 293 return DVar; 294 } 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()) { 300 // 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 306 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. 326 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate); 327 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 328 return DVar; 329 330 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. 338 if (D->isStaticLocal()) { 339 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 353 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) { 354 return getDSA(llvm::next(Stack.rbegin()), D); 355 } 356 357 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind, 358 OpenMPDirectiveKind DKind) { 359 for (StackTy::reverse_iterator I = llvm::next(Stack.rbegin()), 360 E = llvm::prior(Stack.rend()); 361 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 370 void Sema::InitDataSharingAttributesStack() { 371 VarDataSharingAttributesStack = new DSAStackTy(*this); 372 } 373 374 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 375 376 void Sema::DestroyDataSharingAttributesStack() { 377 delete DSAStack; 378 } 379 380 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 381 const DeclarationNameInfo &DirName, 382 Scope *CurScope) { 383 DSAStack->push(DKind, DirName, CurScope); 384 PushExpressionEvaluationContext(PotentiallyEvaluated); 385 } 386 387 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 388 DSAStack->pop(); 389 DiscardCleanupsInEvaluationContext(); 390 PopExpressionEvaluationContext(); 391 } 392 393 namespace { 394 395 class VarDeclFilterCCC : public CorrectionCandidateCallback { 396 private: 397 Sema &Actions; 398 public: 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()); 406 } 407 return false; 408 } 409 }; 410 } 411 412 ExprResult 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); 424 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>(); 431 } else { 432 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 433 : diag::err_omp_expected_var_arg) 434 << Id.getName(); 435 return ExprError(); 436 } 437 } else { 438 if (!(VD = Lookup.getAsSingle<VarDecl>())) { 439 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) 440 << Id.getName(); 441 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 460 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 461 NamedDecl *ND = cast<NamedDecl>(CanonicalVD); 462 // 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. 465 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 } 475 // 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. 479 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 } 489 // 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. 493 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 } 504 // 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. 507 if (CanonicalVD->isStaticLocal() && CurScope && 508 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 509 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()); 529 DSAStack->addDSA(VD, cast<DeclRefExpr>(DE.get()), OMPC_threadprivate); 530 return DE; 531 } 532 533 Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective( 534 SourceLocation Loc, 535 ArrayRef<Expr *> VarList) { 536 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 537 CurContext->addDecl(D); 538 return DeclGroupPtrTy::make(DeclGroupRef(D)); 539 } 540 return DeclGroupPtrTy(); 541 } 542 543 OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl( 544 SourceLocation Loc, 545 ArrayRef<Expr *> VarList) { 546 SmallVector<Expr *, 8> Vars; 547 for (ArrayRef<Expr *>::iterator I = VarList.begin(), 548 E = VarList.end(); 549 I != E; ++I) { 550 DeclRefExpr *DE = cast<DeclRefExpr>(*I); 551 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 552 SourceLocation ILoc = DE->getExprLoc(); 553 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(), 557 diag::err_omp_threadprivate_incomplete_type)) { 558 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) 565 << getOpenMPDirectiveName(OMPD_threadprivate) 566 << VD->getType(); 567 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 568 VarDecl::DeclarationOnly; 569 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 570 diag::note_defined_here) << VD; 571 continue; 572 } 573 574 // Check if this is a TLS variable. 575 if (VD->getTLSKind()) { 576 Diag(ILoc, diag::err_omp_var_thread_local) << VD; 577 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 578 VarDecl::DeclarationOnly; 579 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 580 diag::note_defined_here) << VD; 581 continue; 582 } 583 584 Vars.push_back(*I); 585 } 586 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; 593 } 594 595 namespace { 596 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { 597 DSAStackTy *Stack; 598 Sema &Actions; 599 bool ErrorFound; 600 CapturedStmt *CS; 601 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; 602 public: 603 void VisitDeclRefExpr(DeclRefExpr *E) { 604 if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 605 // 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 && 614 DVar.CKind != OMPC_threadprivate && !DVar.RefExpr) 615 ImplicitFirstprivate.push_back(DVar.RefExpr); 616 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); 637 if (DKind == OMPD_task && DVar.CKind != OMPC_shared) 638 ImplicitFirstprivate.push_back(DVar.RefExpr); 639 } 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; } 659 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } 660 661 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS) 662 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { } 663 }; 664 } 665 666 StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, 667 ArrayRef<OMPClause *> Clauses, 668 Stmt *AStmt, 669 SourceLocation StartLoc, 670 SourceLocation EndLoc) { 671 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 672 673 StmtResult Res = StmtError(); 674 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(); 680 // 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 } 696 697 switch (Kind) { 698 case OMPD_parallel: 699 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, 700 StartLoc, EndLoc); 701 break; 702 case OMPD_threadprivate: 703 case OMPD_task: 704 llvm_unreachable("OpenMP Directive is not allowed"); 705 case OMPD_unknown: 706 case NUM_OPENMP_DIRECTIVES: 707 llvm_unreachable("Unknown OpenMP directive"); 708 } 709 710 if (ErrorFound) return StmtError(); 711 return Res; 712 } 713 714 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 715 Stmt *AStmt, 716 SourceLocation StartLoc, 717 SourceLocation EndLoc) { 718 getCurFunction()->setHasBranchProtectedScope(); 719 720 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc, 721 Clauses, AStmt)); 722 } 723 724 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, 725 Expr *Expr, 726 SourceLocation StartLoc, 727 SourceLocation LParenLoc, 728 SourceLocation EndLoc) { 729 OMPClause *Res = 0; 730 switch (Kind) { 731 case OMPC_if: 732 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc); 733 break; 734 case OMPC_default: 735 case OMPC_private: 736 case OMPC_firstprivate: 737 case OMPC_shared: 738 case OMPC_threadprivate: 739 case OMPC_unknown: 740 case NUM_OPENMP_CLAUSES: 741 llvm_unreachable("Clause is not allowed."); 742 } 743 return Res; 744 } 745 746 OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, 747 SourceLocation StartLoc, 748 SourceLocation LParenLoc, 749 SourceLocation EndLoc) { 750 Expr *ValExpr = Condition; 751 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 752 !Condition->isInstantiationDependent() && 753 !Condition->containsUnexpandedParameterPack()) { 754 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 755 Condition->getExprLoc(), 756 Condition); 757 if (Val.isInvalid()) 758 return 0; 759 760 ValExpr = Val.take(); 761 } 762 763 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc); 764 } 765 766 OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind, 767 unsigned Argument, 768 SourceLocation ArgumentLoc, 769 SourceLocation StartLoc, 770 SourceLocation LParenLoc, 771 SourceLocation EndLoc) { 772 OMPClause *Res = 0; 773 switch (Kind) { 774 case OMPC_default: 775 Res = 776 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 777 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 778 break; 779 case OMPC_if: 780 case OMPC_private: 781 case OMPC_firstprivate: 782 case OMPC_shared: 783 case OMPC_threadprivate: 784 case OMPC_unknown: 785 case NUM_OPENMP_CLAUSES: 786 llvm_unreachable("Clause is not allowed."); 787 } 788 return Res; 789 } 790 791 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 792 SourceLocation KindKwLoc, 793 SourceLocation StartLoc, 794 SourceLocation LParenLoc, 795 SourceLocation EndLoc) { 796 if (Kind == OMPC_DEFAULT_unknown) { 797 std::string Values; 798 std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : ""); 799 for (unsigned i = OMPC_DEFAULT_unknown + 1; 800 i < NUM_OPENMP_DEFAULT_KINDS; ++i) { 801 Values += "'"; 802 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); 803 Values += "'"; 804 switch (i) { 805 case NUM_OPENMP_DEFAULT_KINDS - 2: 806 Values += " or "; 807 break; 808 case NUM_OPENMP_DEFAULT_KINDS - 1: 809 break; 810 default: 811 Values += Sep; 812 break; 813 } 814 } 815 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 816 << Values << getOpenMPClauseName(OMPC_default); 817 return 0; 818 } 819 switch (Kind) { 820 case OMPC_DEFAULT_none: 821 DSAStack->setDefaultDSANone(); 822 break; 823 case OMPC_DEFAULT_shared: 824 DSAStack->setDefaultDSAShared(); 825 break; 826 case OMPC_DEFAULT_unknown: 827 case NUM_OPENMP_DEFAULT_KINDS: 828 llvm_unreachable("Clause kind is not allowed."); 829 break; 830 } 831 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, 832 EndLoc); 833 } 834 835 OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, 836 ArrayRef<Expr *> VarList, 837 SourceLocation StartLoc, 838 SourceLocation LParenLoc, 839 SourceLocation EndLoc) { 840 OMPClause *Res = 0; 841 switch (Kind) { 842 case OMPC_private: 843 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 844 break; 845 case OMPC_firstprivate: 846 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 847 break; 848 case OMPC_shared: 849 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 850 break; 851 case OMPC_if: 852 case OMPC_default: 853 case OMPC_threadprivate: 854 case OMPC_unknown: 855 case NUM_OPENMP_CLAUSES: 856 llvm_unreachable("Clause is not allowed."); 857 } 858 return Res; 859 } 860 861 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 862 SourceLocation StartLoc, 863 SourceLocation LParenLoc, 864 SourceLocation EndLoc) { 865 SmallVector<Expr *, 8> Vars; 866 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end(); 867 I != E; ++I) { 868 assert(*I && "NULL expr in OpenMP private clause."); 869 if (isa<DependentScopeDeclRefExpr>(*I)) { 870 // It will be analyzed later. 871 Vars.push_back(*I); 872 continue; 873 } 874 875 SourceLocation ELoc = (*I)->getExprLoc(); 876 // OpenMP [2.1, C/C++] 877 // A list item is a variable name. 878 // OpenMP [2.9.3.3, Restrictions, p.1] 879 // A variable that is part of another variable (as an array or 880 // structure element) cannot appear in a private clause. 881 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I); 882 if (!DE || !isa<VarDecl>(DE->getDecl())) { 883 Diag(ELoc, diag::err_omp_expected_var_name) 884 << (*I)->getSourceRange(); 885 continue; 886 } 887 Decl *D = DE->getDecl(); 888 VarDecl *VD = cast<VarDecl>(D); 889 890 QualType Type = VD->getType(); 891 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 892 // It will be analyzed later. 893 Vars.push_back(DE); 894 continue; 895 } 896 897 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 898 // A variable that appears in a private clause must not have an incomplete 899 // type or a reference type. 900 if (RequireCompleteType(ELoc, Type, 901 diag::err_omp_private_incomplete_type)) { 902 continue; 903 } 904 if (Type->isReferenceType()) { 905 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 906 << getOpenMPClauseName(OMPC_private) << Type; 907 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 908 VarDecl::DeclarationOnly; 909 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 910 diag::note_defined_here) << VD; 911 continue; 912 } 913 914 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 915 // A variable of class type (or array thereof) that appears in a private 916 // clause requires an accesible, unambiguous default constructor for the 917 // class type. 918 while (Type.getNonReferenceType()->isArrayType()) { 919 Type = cast<ArrayType>( 920 Type.getNonReferenceType().getTypePtr())->getElementType(); 921 } 922 CXXRecordDecl *RD = getLangOpts().CPlusPlus ? 923 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0; 924 if (RD) { 925 CXXConstructorDecl *CD = LookupDefaultConstructor(RD); 926 PartialDiagnostic PD = 927 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 928 if (!CD || 929 CheckConstructorAccess(ELoc, CD, 930 InitializedEntity::InitializeTemporary(Type), 931 CD->getAccess(), PD) == AR_inaccessible || 932 CD->isDeleted()) { 933 Diag(ELoc, diag::err_omp_required_method) 934 << getOpenMPClauseName(OMPC_private) << 0; 935 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 936 VarDecl::DeclarationOnly; 937 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 938 diag::note_defined_here) << VD; 939 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 940 continue; 941 } 942 MarkFunctionReferenced(ELoc, CD); 943 DiagnoseUseOfDecl(CD, ELoc); 944 945 CXXDestructorDecl *DD = RD->getDestructor(); 946 if (DD) { 947 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 948 DD->isDeleted()) { 949 Diag(ELoc, diag::err_omp_required_method) 950 << getOpenMPClauseName(OMPC_private) << 4; 951 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 952 VarDecl::DeclarationOnly; 953 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 954 diag::note_defined_here) << VD; 955 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 956 continue; 957 } 958 MarkFunctionReferenced(ELoc, DD); 959 DiagnoseUseOfDecl(DD, ELoc); 960 } 961 } 962 963 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 964 // in a Construct] 965 // Variables with the predetermined data-sharing attributes may not be 966 // listed in data-sharing attributes clauses, except for the cases 967 // listed below. For these exceptions only, listing a predetermined 968 // variable in a data-sharing attribute clause is allowed and overrides 969 // the variable's predetermined data-sharing attributes. 970 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 971 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 972 Diag(ELoc, diag::err_omp_wrong_dsa) 973 << getOpenMPClauseName(DVar.CKind) 974 << getOpenMPClauseName(OMPC_private); 975 if (DVar.RefExpr) { 976 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 977 << getOpenMPClauseName(DVar.CKind); 978 } else { 979 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa) 980 << getOpenMPClauseName(DVar.CKind); 981 } 982 continue; 983 } 984 985 DSAStack->addDSA(VD, DE, OMPC_private); 986 Vars.push_back(DE); 987 } 988 989 if (Vars.empty()) return 0; 990 991 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 992 } 993 994 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 995 SourceLocation StartLoc, 996 SourceLocation LParenLoc, 997 SourceLocation EndLoc) { 998 SmallVector<Expr *, 8> Vars; 999 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end(); 1000 I != E; ++I) { 1001 assert(*I && "NULL expr in OpenMP firstprivate clause."); 1002 if (isa<DependentScopeDeclRefExpr>(*I)) { 1003 // It will be analyzed later. 1004 Vars.push_back(*I); 1005 continue; 1006 } 1007 1008 SourceLocation ELoc = (*I)->getExprLoc(); 1009 // OpenMP [2.1, C/C++] 1010 // A list item is a variable name. 1011 // OpenMP [2.9.3.3, Restrictions, p.1] 1012 // A variable that is part of another variable (as an array or 1013 // structure element) cannot appear in a private clause. 1014 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I); 1015 if (!DE || !isa<VarDecl>(DE->getDecl())) { 1016 Diag(ELoc, diag::err_omp_expected_var_name) 1017 << (*I)->getSourceRange(); 1018 continue; 1019 } 1020 Decl *D = DE->getDecl(); 1021 VarDecl *VD = cast<VarDecl>(D); 1022 1023 QualType Type = VD->getType(); 1024 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 1025 // It will be analyzed later. 1026 Vars.push_back(DE); 1027 continue; 1028 } 1029 1030 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 1031 // A variable that appears in a private clause must not have an incomplete 1032 // type or a reference type. 1033 if (RequireCompleteType(ELoc, Type, 1034 diag::err_omp_firstprivate_incomplete_type)) { 1035 continue; 1036 } 1037 if (Type->isReferenceType()) { 1038 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 1039 << getOpenMPClauseName(OMPC_firstprivate) << Type; 1040 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 1041 VarDecl::DeclarationOnly; 1042 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 1043 diag::note_defined_here) << VD; 1044 continue; 1045 } 1046 1047 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 1048 // A variable of class type (or array thereof) that appears in a private 1049 // clause requires an accesible, unambiguous copy constructor for the 1050 // class type. 1051 Type = Context.getBaseElementType(Type); 1052 CXXRecordDecl *RD = getLangOpts().CPlusPlus ? 1053 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0; 1054 if (RD) { 1055 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0); 1056 PartialDiagnostic PD = 1057 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 1058 if (!CD || 1059 CheckConstructorAccess(ELoc, CD, 1060 InitializedEntity::InitializeTemporary(Type), 1061 CD->getAccess(), PD) == AR_inaccessible || 1062 CD->isDeleted()) { 1063 Diag(ELoc, diag::err_omp_required_method) 1064 << getOpenMPClauseName(OMPC_firstprivate) << 1; 1065 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 1066 VarDecl::DeclarationOnly; 1067 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 1068 diag::note_defined_here) << VD; 1069 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 1070 continue; 1071 } 1072 MarkFunctionReferenced(ELoc, CD); 1073 DiagnoseUseOfDecl(CD, ELoc); 1074 1075 CXXDestructorDecl *DD = RD->getDestructor(); 1076 if (DD) { 1077 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 1078 DD->isDeleted()) { 1079 Diag(ELoc, diag::err_omp_required_method) 1080 << getOpenMPClauseName(OMPC_firstprivate) << 4; 1081 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 1082 VarDecl::DeclarationOnly; 1083 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 1084 diag::note_defined_here) << VD; 1085 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 1086 continue; 1087 } 1088 MarkFunctionReferenced(ELoc, DD); 1089 DiagnoseUseOfDecl(DD, ELoc); 1090 } 1091 } 1092 1093 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate 1094 // variable and it was checked already. 1095 if (StartLoc.isValid() && EndLoc.isValid()) { 1096 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 1097 Type = Type.getNonReferenceType().getCanonicalType(); 1098 bool IsConstant = Type.isConstant(Context); 1099 Type = Context.getBaseElementType(Type); 1100 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 1101 // A list item that specifies a given variable may not appear in more 1102 // than one clause on the same directive, except that a variable may be 1103 // specified in both firstprivate and lastprivate clauses. 1104 // TODO: add processing for lastprivate. 1105 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 1106 DVar.RefExpr) { 1107 Diag(ELoc, diag::err_omp_wrong_dsa) 1108 << getOpenMPClauseName(DVar.CKind) 1109 << getOpenMPClauseName(OMPC_firstprivate); 1110 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1111 << getOpenMPClauseName(DVar.CKind); 1112 continue; 1113 } 1114 1115 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1116 // in a Construct] 1117 // Variables with the predetermined data-sharing attributes may not be 1118 // listed in data-sharing attributes clauses, except for the cases 1119 // listed below. For these exceptions only, listing a predetermined 1120 // variable in a data-sharing attribute clause is allowed and overrides 1121 // the variable's predetermined data-sharing attributes. 1122 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1123 // in a Construct, C/C++, p.2] 1124 // Variables with const-qualified type having no mutable member may be 1125 // listed in a firstprivate clause, even if they are static data members. 1126 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && 1127 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 1128 Diag(ELoc, diag::err_omp_wrong_dsa) 1129 << getOpenMPClauseName(DVar.CKind) 1130 << getOpenMPClauseName(OMPC_firstprivate); 1131 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa) 1132 << getOpenMPClauseName(DVar.CKind); 1133 continue; 1134 } 1135 1136 // OpenMP [2.9.3.4, Restrictions, p.2] 1137 // A list item that is private within a parallel region must not appear 1138 // in a firstprivate clause on a worksharing construct if any of the 1139 // worksharing regions arising from the worksharing construct ever bind 1140 // to any of the parallel regions arising from the parallel construct. 1141 // OpenMP [2.9.3.4, Restrictions, p.3] 1142 // A list item that appears in a reduction clause of a parallel construct 1143 // must not appear in a firstprivate clause on a worksharing or task 1144 // construct if any of the worksharing or task regions arising from the 1145 // worksharing or task construct ever bind to any of the parallel regions 1146 // arising from the parallel construct. 1147 // OpenMP [2.9.3.4, Restrictions, p.4] 1148 // A list item that appears in a reduction clause in worksharing 1149 // construct must not appear in a firstprivate clause in a task construct 1150 // encountered during execution of any of the worksharing regions arising 1151 // from the worksharing construct. 1152 // TODO: 1153 } 1154 1155 DSAStack->addDSA(VD, DE, OMPC_firstprivate); 1156 Vars.push_back(DE); 1157 } 1158 1159 if (Vars.empty()) return 0; 1160 1161 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 1162 Vars); 1163 } 1164 1165 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 1166 SourceLocation StartLoc, 1167 SourceLocation LParenLoc, 1168 SourceLocation EndLoc) { 1169 SmallVector<Expr *, 8> Vars; 1170 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end(); 1171 I != E; ++I) { 1172 assert(*I && "NULL expr in OpenMP shared clause."); 1173 if (isa<DependentScopeDeclRefExpr>(*I)) { 1174 // It will be analyzed later. 1175 Vars.push_back(*I); 1176 continue; 1177 } 1178 1179 SourceLocation ELoc = (*I)->getExprLoc(); 1180 // OpenMP [2.1, C/C++] 1181 // A list item is a variable name. 1182 // OpenMP [2.9.3.4, Restrictions, p.1] 1183 // A variable that is part of another variable (as an array or 1184 // structure element) cannot appear in a private clause. 1185 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I); 1186 if (!DE || !isa<VarDecl>(DE->getDecl())) { 1187 Diag(ELoc, diag::err_omp_expected_var_name) 1188 << (*I)->getSourceRange(); 1189 continue; 1190 } 1191 Decl *D = DE->getDecl(); 1192 VarDecl *VD = cast<VarDecl>(D); 1193 1194 QualType Type = VD->getType(); 1195 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 1196 // It will be analyzed later. 1197 Vars.push_back(DE); 1198 continue; 1199 } 1200 1201 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1202 // in a Construct] 1203 // Variables with the predetermined data-sharing attributes may not be 1204 // listed in data-sharing attributes clauses, except for the cases 1205 // listed below. For these exceptions only, listing a predetermined 1206 // variable in a data-sharing attribute clause is allowed and overrides 1207 // the variable's predetermined data-sharing attributes. 1208 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 1209 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) { 1210 Diag(ELoc, diag::err_omp_wrong_dsa) 1211 << getOpenMPClauseName(DVar.CKind) 1212 << getOpenMPClauseName(OMPC_shared); 1213 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1214 << getOpenMPClauseName(DVar.CKind); 1215 continue; 1216 } 1217 1218 DSAStack->addDSA(VD, DE, OMPC_shared); 1219 Vars.push_back(DE); 1220 } 1221 1222 if (Vars.empty()) return 0; 1223 1224 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 1225 } 1226 1227 #undef DSAStack 1228