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/SemaInternal.h" 25 #include "clang/Sema/Lookup.h" 26 #include "clang/Sema/Scope.h" 27 #include "clang/Sema/ScopeInfo.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 public: 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); 109 110 /// \brief Returns currently analyzed directive. 111 OpenMPDirectiveKind getCurrentDirective() const { 112 return Stack.back().Directive; 113 } 114 115 /// \brief Set default data sharing attribute to none. 116 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; } 117 /// \brief Set default data sharing attribute to shared. 118 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; } 119 120 DefaultDataSharingAttributes getDefaultDSA() const { 121 return Stack.back().DefaultAttr; 122 } 123 124 Scope *getCurScope() { return Stack.back().CurScope; } 125 }; 126 } // end anonymous namespace. 127 128 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter, 129 VarDecl *D) { 130 DSAVarData DVar; 131 if (Iter == Stack.rend() - 1) { 132 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 133 // in a region but not in construct] 134 // File-scope or namespace-scope variables referenced in called routines 135 // in the region are shared unless they appear in a threadprivate 136 // directive. 137 // TODO 138 if (!D->isFunctionOrMethodVarDecl()) 139 DVar.CKind = OMPC_shared; 140 141 return DVar; 142 } 143 DVar.DKind = Iter->Directive; 144 // Explicitly specified attributes and local variables with predetermined 145 // attributes. 146 if (Iter->SharingMap.count(D)) { 147 DVar.RefExpr = Iter->SharingMap[D].RefExpr; 148 DVar.CKind = Iter->SharingMap[D].Attributes; 149 return DVar; 150 } 151 152 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 153 // in a Construct, C/C++, implicitly determined, p.1] 154 // In a parallel or task construct, the data-sharing attributes of these 155 // variables are determined by the default clause, if present. 156 switch (Iter->DefaultAttr) { 157 case DSA_shared: 158 DVar.CKind = OMPC_shared; 159 return DVar; 160 case DSA_none: 161 return DVar; 162 case DSA_unspecified: 163 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 164 // in a Construct, implicitly determined, p.2] 165 // In a parallel construct, if no default clause is present, these 166 // variables are shared. 167 if (DVar.DKind == OMPD_parallel) { 168 DVar.CKind = OMPC_shared; 169 return DVar; 170 } 171 172 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 173 // in a Construct, implicitly determined, p.4] 174 // In a task construct, if no default clause is present, a variable that in 175 // the enclosing context is determined to be shared by all implicit tasks 176 // bound to the current team is shared. 177 // TODO 178 if (DVar.DKind == OMPD_task) { 179 DSAVarData DVarTemp; 180 for (StackTy::reverse_iterator I = Iter + 1, 181 EE = Stack.rend() - 1; 182 I != EE; ++I) { 183 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 184 // in a Construct, implicitly determined, p.6] 185 // In a task construct, if no default clause is present, a variable 186 // whose data-sharing attribute is not determined by the rules above is 187 // firstprivate. 188 DVarTemp = getDSA(I, D); 189 if (DVarTemp.CKind != OMPC_shared) { 190 DVar.RefExpr = 0; 191 DVar.DKind = OMPD_task; 192 DVar.CKind = OMPC_unknown; 193 // TODO: should return OMPC_firstprivate 194 return DVar; 195 } 196 if (I->Directive == OMPD_parallel) break; 197 } 198 DVar.DKind = OMPD_task; 199 // TODO: Should return OMPC_firstprivate instead of OMPC_unknown. 200 DVar.CKind = 201 (DVarTemp.CKind == OMPC_unknown) ? OMPC_unknown : OMPC_shared; 202 return DVar; 203 } 204 } 205 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 206 // in a Construct, implicitly determined, p.3] 207 // For constructs other than task, if no default clause is present, these 208 // variables inherit their data-sharing attributes from the enclosing 209 // context. 210 return getDSA(Iter + 1, D); 211 } 212 213 void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) { 214 if (A == OMPC_threadprivate) { 215 Stack[0].SharingMap[D].Attributes = A; 216 Stack[0].SharingMap[D].RefExpr = E; 217 } else { 218 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 219 Stack.back().SharingMap[D].Attributes = A; 220 Stack.back().SharingMap[D].RefExpr = E; 221 } 222 } 223 224 bool DSAStackTy::isOpenMPLocal(VarDecl *D) { 225 Scope *CurScope = getCurScope(); 226 while (CurScope && !CurScope->isDeclScope(D)) 227 CurScope = CurScope->getParent(); 228 while (CurScope && !CurScope->isOpenMPDirectiveScope()) 229 CurScope = CurScope->getParent(); 230 bool isOpenMPLocal = !!CurScope; 231 if (!isOpenMPLocal) { 232 CurScope = getCurScope(); 233 while (CurScope && !CurScope->isOpenMPDirectiveScope()) 234 CurScope = CurScope->getParent(); 235 isOpenMPLocal = 236 CurScope && 237 isa<CapturedDecl>(D->getDeclContext()) && 238 static_cast<DeclContext *>( 239 CurScope->getFnParent()->getEntity())->Encloses(D->getDeclContext()); 240 } 241 return isOpenMPLocal; 242 } 243 244 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) { 245 DSAVarData DVar; 246 247 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 248 // in a Construct, C/C++, predetermined, p.1] 249 // Variables appearing in threadprivate directives are threadprivate. 250 if (D->getTLSKind() != VarDecl::TLS_None) { 251 DVar.CKind = OMPC_threadprivate; 252 return DVar; 253 } 254 if (Stack[0].SharingMap.count(D)) { 255 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr; 256 DVar.CKind = OMPC_threadprivate; 257 return DVar; 258 } 259 260 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 261 // in a Construct, C/C++, predetermined, p.1] 262 // Variables with automatic storage duration that are declared in a scope 263 // inside the construct are private. 264 if (isOpenMPLocal(D) && D->isLocalVarDecl() && 265 (D->getStorageClass() == SC_Auto || 266 D->getStorageClass() == SC_None)) { 267 DVar.CKind = OMPC_private; 268 return DVar; 269 } 270 271 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 272 // in a Construct, C/C++, predetermined, p.4] 273 // Static data memebers are shared. 274 if (D->isStaticDataMember()) { 275 // Variables with const-qualified type having no mutable member may be 276 // listed in a firstprivate clause, even if they are static data members. 277 // TODO: 278 DVar.CKind = OMPC_shared; 279 return DVar; 280 } 281 282 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 283 bool IsConstant = Type.isConstant(Actions.getASTContext()); 284 while (Type->isArrayType()) { 285 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType(); 286 Type = ElemType.getNonReferenceType().getCanonicalType(); 287 } 288 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 289 // in a Construct, C/C++, predetermined, p.6] 290 // Variables with const qualified type having no mutable member are 291 // shared. 292 CXXRecordDecl *RD = Actions.getLangOpts().CPlusPlus ? 293 Type->getAsCXXRecordDecl() : 0; 294 if (IsConstant && 295 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) { 296 // Variables with const-qualified type having no mutable member may be 297 // listed in a firstprivate clause, even if they are static data members. 298 // TODO 299 DVar.CKind = OMPC_shared; 300 return DVar; 301 } 302 303 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 304 // in a Construct, C/C++, predetermined, p.7] 305 // Variables with static storage duration that are declared in a scope 306 // inside the construct are shared. 307 if (isOpenMPLocal(D) && D->isStaticLocal()) { 308 DVar.CKind = OMPC_shared; 309 return DVar; 310 } 311 312 // Explicitly specified attributes and local variables with predetermined 313 // attributes. 314 if (Stack.back().SharingMap.count(D)) { 315 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr; 316 DVar.CKind = Stack.back().SharingMap[D].Attributes; 317 } 318 319 return DVar; 320 } 321 322 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) { 323 return getDSA(Stack.rbegin() + 1, D); 324 } 325 326 void Sema::InitDataSharingAttributesStack() { 327 VarDataSharingAttributesStack = new DSAStackTy(*this); 328 } 329 330 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 331 332 void Sema::DestroyDataSharingAttributesStack() { 333 delete DSAStack; 334 } 335 336 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 337 const DeclarationNameInfo &DirName, 338 Scope *CurScope) { 339 DSAStack->push(DKind, DirName, CurScope); 340 PushExpressionEvaluationContext(PotentiallyEvaluated); 341 } 342 343 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 344 DSAStack->pop(); 345 DiscardCleanupsInEvaluationContext(); 346 PopExpressionEvaluationContext(); 347 } 348 349 namespace { 350 351 class VarDeclFilterCCC : public CorrectionCandidateCallback { 352 private: 353 Sema &Actions; 354 public: 355 VarDeclFilterCCC(Sema &S) : Actions(S) { } 356 virtual bool ValidateCandidate(const TypoCorrection &Candidate) { 357 NamedDecl *ND = Candidate.getCorrectionDecl(); 358 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) { 359 return VD->hasGlobalStorage() && 360 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(), 361 Actions.getCurScope()); 362 } 363 return false; 364 } 365 }; 366 } 367 368 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 369 CXXScopeSpec &ScopeSpec, 370 const DeclarationNameInfo &Id) { 371 LookupResult Lookup(*this, Id, LookupOrdinaryName); 372 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 373 374 if (Lookup.isAmbiguous()) 375 return ExprError(); 376 377 VarDecl *VD; 378 if (!Lookup.isSingleResult()) { 379 VarDeclFilterCCC Validator(*this); 380 if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope, 381 0, Validator)) { 382 diagnoseTypo(Corrected, 383 PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest 384 : diag::err_omp_expected_var_arg_suggest) 385 << Id.getName()); 386 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 387 } else { 388 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 389 : diag::err_omp_expected_var_arg) 390 << Id.getName(); 391 return ExprError(); 392 } 393 } else { 394 if (!(VD = Lookup.getAsSingle<VarDecl>())) { 395 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) 396 << Id.getName(); 397 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 398 return ExprError(); 399 } 400 } 401 Lookup.suppressDiagnostics(); 402 403 // OpenMP [2.9.2, Syntax, C/C++] 404 // Variables must be file-scope, namespace-scope, or static block-scope. 405 if (!VD->hasGlobalStorage()) { 406 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 407 << getOpenMPDirectiveName(OMPD_threadprivate) 408 << !VD->isStaticLocal(); 409 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 410 VarDecl::DeclarationOnly; 411 Diag(VD->getLocation(), 412 IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD; 413 return ExprError(); 414 } 415 416 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 417 NamedDecl *ND = cast<NamedDecl>(CanonicalVD); 418 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 419 // A threadprivate directive for file-scope variables must appear outside 420 // any definition or declaration. 421 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 422 !getCurLexicalContext()->isTranslationUnit()) { 423 Diag(Id.getLoc(), diag::err_omp_var_scope) 424 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 425 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 426 VarDecl::DeclarationOnly; 427 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 428 diag::note_defined_here) << VD; 429 return ExprError(); 430 } 431 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 432 // A threadprivate directive for static class member variables must appear 433 // in the class definition, in the same scope in which the member 434 // variables are declared. 435 if (CanonicalVD->isStaticDataMember() && 436 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 437 Diag(Id.getLoc(), diag::err_omp_var_scope) 438 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 439 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 440 VarDecl::DeclarationOnly; 441 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 442 diag::note_defined_here) << VD; 443 return ExprError(); 444 } 445 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 446 // A threadprivate directive for namespace-scope variables must appear 447 // outside any definition or declaration other than the namespace 448 // definition itself. 449 if (CanonicalVD->getDeclContext()->isNamespace() && 450 (!getCurLexicalContext()->isFileContext() || 451 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 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 } 460 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 461 // A threadprivate directive for static block-scope variables must appear 462 // in the scope of the variable and not in a nested scope. 463 if (CanonicalVD->isStaticLocal() && CurScope && 464 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 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; 471 return ExprError(); 472 } 473 474 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 475 // A threadprivate directive must lexically precede all references to any 476 // of the variables in its list. 477 if (VD->isUsed()) { 478 Diag(Id.getLoc(), diag::err_omp_var_used) 479 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 480 return ExprError(); 481 } 482 483 QualType ExprType = VD->getType().getNonReferenceType(); 484 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_RValue, Id.getLoc()); 485 DSAStack->addDSA(VD, cast<DeclRefExpr>(DE.get()), OMPC_threadprivate); 486 return DE; 487 } 488 489 Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective( 490 SourceLocation Loc, 491 ArrayRef<Expr *> VarList) { 492 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 493 CurContext->addDecl(D); 494 return DeclGroupPtrTy::make(DeclGroupRef(D)); 495 } 496 return DeclGroupPtrTy(); 497 } 498 499 OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl( 500 SourceLocation Loc, 501 ArrayRef<Expr *> VarList) { 502 SmallVector<Expr *, 8> Vars; 503 for (ArrayRef<Expr *>::iterator I = VarList.begin(), 504 E = VarList.end(); 505 I != E; ++I) { 506 DeclRefExpr *DE = cast<DeclRefExpr>(*I); 507 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 508 SourceLocation ILoc = DE->getExprLoc(); 509 510 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 511 // A threadprivate variable must not have an incomplete type. 512 if (RequireCompleteType(ILoc, VD->getType(), 513 diag::err_omp_threadprivate_incomplete_type)) { 514 continue; 515 } 516 517 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 518 // A threadprivate variable must not have a reference type. 519 if (VD->getType()->isReferenceType()) { 520 Diag(ILoc, diag::err_omp_ref_type_arg) 521 << getOpenMPDirectiveName(OMPD_threadprivate) 522 << VD->getType(); 523 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 524 VarDecl::DeclarationOnly; 525 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 526 diag::note_defined_here) << VD; 527 continue; 528 } 529 530 // Check if this is a TLS variable. 531 if (VD->getTLSKind()) { 532 Diag(ILoc, diag::err_omp_var_thread_local) << VD; 533 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 534 VarDecl::DeclarationOnly; 535 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 536 diag::note_defined_here) << VD; 537 continue; 538 } 539 540 Vars.push_back(*I); 541 } 542 return Vars.empty() ? 543 0 : OMPThreadPrivateDecl::Create(Context, 544 getCurLexicalContext(), 545 Loc, Vars); 546 } 547 548 namespace { 549 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { 550 DSAStackTy *Stack; 551 Sema &Actions; 552 bool ErrorFound; 553 CapturedStmt *CS; 554 public: 555 void VisitDeclRefExpr(DeclRefExpr *E) { 556 if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 557 if (VD->isImplicit() && VD->hasAttr<UnusedAttr>()) return; 558 // Skip internally declared variables. 559 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) return; 560 561 SourceLocation ELoc = E->getExprLoc(); 562 563 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 564 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD); 565 if (DVar.CKind != OMPC_unknown) { 566 if (DKind == OMPD_task && DVar.CKind != OMPC_shared && 567 DVar.CKind != OMPC_threadprivate && !DVar.RefExpr) { 568 // TODO: should be marked as firstprivate. 569 } 570 return; 571 } 572 // The default(none) clause requires that each variable that is referenced 573 // in the construct, and does not have a predetermined data-sharing 574 // attribute, must have its data-sharing attribute explicitly determined 575 // by being listed in a data-sharing attribute clause. 576 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 577 (DKind == OMPD_parallel || DKind == OMPD_task)) { 578 ErrorFound = true; 579 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD; 580 return; 581 } 582 583 // OpenMP [2.9.3.6, Restrictions, p.2] 584 // A list item that appears in a reduction clause of the innermost 585 // enclosing worksharing or parallel construct may not be accessed in an 586 // explicit task. 587 // TODO: 588 589 // Define implicit data-sharing attributes for task. 590 DVar = Stack->getImplicitDSA(VD); 591 if (DKind == OMPD_task && DVar.CKind != OMPC_shared) { 592 // TODO: should be marked as firstprivate. 593 } 594 } 595 } 596 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 597 for (ArrayRef<OMPClause *>::iterator I = S->clauses().begin(), 598 E = S->clauses().end(); 599 I != E; ++I) 600 if (OMPClause *C = *I) 601 for (StmtRange R = C->children(); R; ++R) 602 if (Stmt *Child = *R) 603 Visit(Child); 604 } 605 void VisitStmt(Stmt *S) { 606 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); 607 I != E; ++I) 608 if (Stmt *Child = *I) 609 if (!isa<OMPExecutableDirective>(Child)) 610 Visit(Child); 611 } 612 613 bool isErrorFound() { return ErrorFound; } 614 615 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS) 616 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { } 617 }; 618 } 619 620 StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, 621 ArrayRef<OMPClause *> Clauses, 622 Stmt *AStmt, 623 SourceLocation StartLoc, 624 SourceLocation EndLoc) { 625 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 626 627 StmtResult Res = StmtError(); 628 629 // Check default data sharing attributes for referenced variables. 630 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 631 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); 632 if (DSAChecker.isErrorFound()) 633 return StmtError(); 634 635 switch (Kind) { 636 case OMPD_parallel: 637 Res = ActOnOpenMPParallelDirective(Clauses, AStmt, StartLoc, EndLoc); 638 break; 639 case OMPD_threadprivate: 640 case OMPD_task: 641 llvm_unreachable("OpenMP Directive is not allowed"); 642 case OMPD_unknown: 643 case NUM_OPENMP_DIRECTIVES: 644 llvm_unreachable("Unknown OpenMP directive"); 645 } 646 return Res; 647 } 648 649 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 650 Stmt *AStmt, 651 SourceLocation StartLoc, 652 SourceLocation EndLoc) { 653 getCurFunction()->setHasBranchProtectedScope(); 654 655 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc, 656 Clauses, AStmt)); 657 } 658 659 OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind, 660 unsigned Argument, 661 SourceLocation ArgumentLoc, 662 SourceLocation StartLoc, 663 SourceLocation LParenLoc, 664 SourceLocation EndLoc) { 665 OMPClause *Res = 0; 666 switch (Kind) { 667 case OMPC_default: 668 Res = 669 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 670 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 671 break; 672 case OMPC_private: 673 case OMPC_shared: 674 case OMPC_threadprivate: 675 case OMPC_unknown: 676 case NUM_OPENMP_CLAUSES: 677 llvm_unreachable("Clause is not allowed."); 678 } 679 return Res; 680 } 681 682 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 683 SourceLocation KindKwLoc, 684 SourceLocation StartLoc, 685 SourceLocation LParenLoc, 686 SourceLocation EndLoc) { 687 if (Kind == OMPC_DEFAULT_unknown) { 688 std::string Values; 689 std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : ""); 690 for (unsigned i = OMPC_DEFAULT_unknown + 1; 691 i < NUM_OPENMP_DEFAULT_KINDS; ++i) { 692 Values += "'"; 693 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); 694 Values += "'"; 695 switch (i) { 696 case NUM_OPENMP_DEFAULT_KINDS - 2: 697 Values += " or "; 698 break; 699 case NUM_OPENMP_DEFAULT_KINDS - 1: 700 break; 701 default: 702 Values += Sep; 703 break; 704 } 705 } 706 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 707 << Values << getOpenMPClauseName(OMPC_default); 708 return 0; 709 } 710 switch (Kind) { 711 case OMPC_DEFAULT_none: 712 DSAStack->setDefaultDSANone(); 713 break; 714 case OMPC_DEFAULT_shared: 715 DSAStack->setDefaultDSAShared(); 716 break; 717 default: 718 break; 719 } 720 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, 721 EndLoc); 722 } 723 724 OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, 725 ArrayRef<Expr *> VarList, 726 SourceLocation StartLoc, 727 SourceLocation LParenLoc, 728 SourceLocation EndLoc) { 729 OMPClause *Res = 0; 730 switch (Kind) { 731 case OMPC_private: 732 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 733 break; 734 case OMPC_shared: 735 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 736 break; 737 case OMPC_default: 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::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 747 SourceLocation StartLoc, 748 SourceLocation LParenLoc, 749 SourceLocation EndLoc) { 750 SmallVector<Expr *, 8> Vars; 751 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end(); 752 I != E; ++I) { 753 assert(*I && "NULL expr in OpenMP private clause."); 754 if (isa<DependentScopeDeclRefExpr>(*I)) { 755 // It will be analyzed later. 756 Vars.push_back(*I); 757 continue; 758 } 759 760 SourceLocation ELoc = (*I)->getExprLoc(); 761 // OpenMP [2.1, C/C++] 762 // A list item is a variable name. 763 // OpenMP [2.9.3.3, Restrictions, p.1] 764 // A variable that is part of another variable (as an array or 765 // structure element) cannot appear in a private clause. 766 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I); 767 if (!DE || !isa<VarDecl>(DE->getDecl())) { 768 Diag(ELoc, diag::err_omp_expected_var_name) 769 << (*I)->getSourceRange(); 770 continue; 771 } 772 Decl *D = DE->getDecl(); 773 VarDecl *VD = cast<VarDecl>(D); 774 775 QualType Type = VD->getType(); 776 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 777 // It will be analyzed later. 778 Vars.push_back(DE); 779 continue; 780 } 781 782 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 783 // A variable that appears in a private clause must not have an incomplete 784 // type or a reference type. 785 if (RequireCompleteType(ELoc, Type, 786 diag::err_omp_private_incomplete_type)) { 787 continue; 788 } 789 if (Type->isReferenceType()) { 790 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 791 << getOpenMPClauseName(OMPC_private) << Type; 792 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 793 VarDecl::DeclarationOnly; 794 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 795 diag::note_defined_here) << VD; 796 continue; 797 } 798 799 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 800 // A variable of class type (or array thereof) that appears in a private 801 // clause requires an accesible, unambiguous default constructor for the 802 // class type. 803 while (Type.getNonReferenceType()->isArrayType()) { 804 Type = cast<ArrayType>( 805 Type.getNonReferenceType().getTypePtr())->getElementType(); 806 } 807 CXXRecordDecl *RD = getLangOpts().CPlusPlus ? 808 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0; 809 if (RD) { 810 CXXConstructorDecl *CD = LookupDefaultConstructor(RD); 811 PartialDiagnostic PD = 812 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 813 if (!CD || 814 CheckConstructorAccess(ELoc, CD, 815 InitializedEntity::InitializeTemporary(Type), 816 CD->getAccess(), PD) == AR_inaccessible || 817 CD->isDeleted()) { 818 Diag(ELoc, diag::err_omp_required_method) 819 << getOpenMPClauseName(OMPC_private) << 0; 820 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 821 VarDecl::DeclarationOnly; 822 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 823 diag::note_defined_here) << VD; 824 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 825 continue; 826 } 827 MarkFunctionReferenced(ELoc, CD); 828 DiagnoseUseOfDecl(CD, ELoc); 829 830 CXXDestructorDecl *DD = RD->getDestructor(); 831 if (DD) { 832 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 833 DD->isDeleted()) { 834 Diag(ELoc, diag::err_omp_required_method) 835 << getOpenMPClauseName(OMPC_private) << 4; 836 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 837 VarDecl::DeclarationOnly; 838 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 839 diag::note_defined_here) << VD; 840 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 841 continue; 842 } 843 MarkFunctionReferenced(ELoc, DD); 844 DiagnoseUseOfDecl(DD, ELoc); 845 } 846 } 847 848 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 849 // in a Construct] 850 // Variables with the predetermined data-sharing attributes may not be 851 // listed in data-sharing attributes clauses, except for the cases 852 // listed below. For these exceptions only, listing a predetermined 853 // variable in a data-sharing attribute clause is allowed and overrides 854 // the variable's predetermined data-sharing attributes. 855 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 856 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 857 Diag(ELoc, diag::err_omp_wrong_dsa) 858 << getOpenMPClauseName(DVar.CKind) 859 << getOpenMPClauseName(OMPC_private); 860 if (DVar.RefExpr) { 861 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 862 << getOpenMPClauseName(DVar.CKind); 863 } else { 864 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa) 865 << getOpenMPClauseName(DVar.CKind); 866 } 867 continue; 868 } 869 870 DSAStack->addDSA(VD, DE, OMPC_private); 871 Vars.push_back(DE); 872 } 873 874 if (Vars.empty()) return 0; 875 876 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 877 } 878 879 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 880 SourceLocation StartLoc, 881 SourceLocation LParenLoc, 882 SourceLocation EndLoc) { 883 SmallVector<Expr *, 8> Vars; 884 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end(); 885 I != E; ++I) { 886 assert(*I && "NULL expr in OpenMP shared clause."); 887 if (isa<DependentScopeDeclRefExpr>(*I)) { 888 // It will be analyzed later. 889 Vars.push_back(*I); 890 continue; 891 } 892 893 SourceLocation ELoc = (*I)->getExprLoc(); 894 // OpenMP [2.1, C/C++] 895 // A list item is a variable name. 896 // OpenMP [2.9.3.4, Restrictions, p.1] 897 // A variable that is part of another variable (as an array or 898 // structure element) cannot appear in a private clause. 899 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I); 900 if (!DE || !isa<VarDecl>(DE->getDecl())) { 901 Diag(ELoc, diag::err_omp_expected_var_name) 902 << (*I)->getSourceRange(); 903 continue; 904 } 905 Decl *D = DE->getDecl(); 906 VarDecl *VD = cast<VarDecl>(D); 907 908 QualType Type = VD->getType(); 909 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 910 // It will be analyzed later. 911 Vars.push_back(DE); 912 continue; 913 } 914 915 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 916 // in a Construct] 917 // Variables with the predetermined data-sharing attributes may not be 918 // listed in data-sharing attributes clauses, except for the cases 919 // listed below. For these exceptions only, listing a predetermined 920 // variable in a data-sharing attribute clause is allowed and overrides 921 // the variable's predetermined data-sharing attributes. 922 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 923 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) { 924 Diag(ELoc, diag::err_omp_wrong_dsa) 925 << getOpenMPClauseName(DVar.CKind) 926 << getOpenMPClauseName(OMPC_shared); 927 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 928 << getOpenMPClauseName(DVar.CKind); 929 continue; 930 } 931 932 DSAStack->addDSA(VD, DE, OMPC_shared); 933 Vars.push_back(DE); 934 } 935 936 if (Vars.empty()) return 0; 937 938 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 939 } 940 941 #undef DSAStack 942