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 = std::next(Iter), 207 EE = std::prev(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(std::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, std::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(std::next(Stack.rbegin()), D); 355 } 356 357 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind, 358 OpenMPDirectiveKind DKind) { 359 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()), 360 E = std::prev(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_simd: 703 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, 704 StartLoc, EndLoc); 705 break; 706 case OMPD_threadprivate: 707 case OMPD_task: 708 llvm_unreachable("OpenMP Directive is not allowed"); 709 case OMPD_unknown: 710 case NUM_OPENMP_DIRECTIVES: 711 llvm_unreachable("Unknown OpenMP directive"); 712 } 713 714 if (ErrorFound) return StmtError(); 715 return Res; 716 } 717 718 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 719 Stmt *AStmt, 720 SourceLocation StartLoc, 721 SourceLocation EndLoc) { 722 getCurFunction()->setHasBranchProtectedScope(); 723 724 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc, 725 Clauses, AStmt)); 726 } 727 728 StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, 729 Stmt *AStmt, 730 SourceLocation StartLoc, 731 SourceLocation EndLoc) { 732 Stmt *CStmt = AStmt; 733 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt)) 734 CStmt = CS->getCapturedStmt(); 735 while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt)) 736 CStmt = AS->getSubStmt(); 737 ForStmt *For = dyn_cast<ForStmt>(CStmt); 738 if (!For) { 739 Diag(CStmt->getLocStart(), diag::err_omp_not_for) 740 << getOpenMPDirectiveName(OMPD_simd); 741 return StmtError(); 742 } 743 744 // FIXME: Checking loop canonical form, collapsing etc. 745 746 getCurFunction()->setHasBranchProtectedScope(); 747 return Owned(OMPSimdDirective::Create(Context, StartLoc, EndLoc, 748 Clauses, AStmt)); 749 } 750 751 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, 752 Expr *Expr, 753 SourceLocation StartLoc, 754 SourceLocation LParenLoc, 755 SourceLocation EndLoc) { 756 OMPClause *Res = 0; 757 switch (Kind) { 758 case OMPC_if: 759 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc); 760 break; 761 case OMPC_default: 762 case OMPC_private: 763 case OMPC_firstprivate: 764 case OMPC_shared: 765 case OMPC_threadprivate: 766 case OMPC_unknown: 767 case NUM_OPENMP_CLAUSES: 768 llvm_unreachable("Clause is not allowed."); 769 } 770 return Res; 771 } 772 773 OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, 774 SourceLocation StartLoc, 775 SourceLocation LParenLoc, 776 SourceLocation EndLoc) { 777 Expr *ValExpr = Condition; 778 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 779 !Condition->isInstantiationDependent() && 780 !Condition->containsUnexpandedParameterPack()) { 781 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 782 Condition->getExprLoc(), 783 Condition); 784 if (Val.isInvalid()) 785 return 0; 786 787 ValExpr = Val.take(); 788 } 789 790 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc); 791 } 792 793 OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind, 794 unsigned Argument, 795 SourceLocation ArgumentLoc, 796 SourceLocation StartLoc, 797 SourceLocation LParenLoc, 798 SourceLocation EndLoc) { 799 OMPClause *Res = 0; 800 switch (Kind) { 801 case OMPC_default: 802 Res = 803 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 804 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 805 break; 806 case OMPC_if: 807 case OMPC_private: 808 case OMPC_firstprivate: 809 case OMPC_shared: 810 case OMPC_threadprivate: 811 case OMPC_unknown: 812 case NUM_OPENMP_CLAUSES: 813 llvm_unreachable("Clause is not allowed."); 814 } 815 return Res; 816 } 817 818 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 819 SourceLocation KindKwLoc, 820 SourceLocation StartLoc, 821 SourceLocation LParenLoc, 822 SourceLocation EndLoc) { 823 if (Kind == OMPC_DEFAULT_unknown) { 824 std::string Values; 825 std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : ""); 826 for (unsigned i = OMPC_DEFAULT_unknown + 1; 827 i < NUM_OPENMP_DEFAULT_KINDS; ++i) { 828 Values += "'"; 829 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); 830 Values += "'"; 831 switch (i) { 832 case NUM_OPENMP_DEFAULT_KINDS - 2: 833 Values += " or "; 834 break; 835 case NUM_OPENMP_DEFAULT_KINDS - 1: 836 break; 837 default: 838 Values += Sep; 839 break; 840 } 841 } 842 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 843 << Values << getOpenMPClauseName(OMPC_default); 844 return 0; 845 } 846 switch (Kind) { 847 case OMPC_DEFAULT_none: 848 DSAStack->setDefaultDSANone(); 849 break; 850 case OMPC_DEFAULT_shared: 851 DSAStack->setDefaultDSAShared(); 852 break; 853 case OMPC_DEFAULT_unknown: 854 case NUM_OPENMP_DEFAULT_KINDS: 855 llvm_unreachable("Clause kind is not allowed."); 856 break; 857 } 858 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, 859 EndLoc); 860 } 861 862 OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, 863 ArrayRef<Expr *> VarList, 864 SourceLocation StartLoc, 865 SourceLocation LParenLoc, 866 SourceLocation EndLoc) { 867 OMPClause *Res = 0; 868 switch (Kind) { 869 case OMPC_private: 870 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 871 break; 872 case OMPC_firstprivate: 873 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 874 break; 875 case OMPC_shared: 876 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 877 break; 878 case OMPC_if: 879 case OMPC_default: 880 case OMPC_threadprivate: 881 case OMPC_unknown: 882 case NUM_OPENMP_CLAUSES: 883 llvm_unreachable("Clause is not allowed."); 884 } 885 return Res; 886 } 887 888 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 889 SourceLocation StartLoc, 890 SourceLocation LParenLoc, 891 SourceLocation EndLoc) { 892 SmallVector<Expr *, 8> Vars; 893 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end(); 894 I != E; ++I) { 895 assert(*I && "NULL expr in OpenMP private clause."); 896 if (isa<DependentScopeDeclRefExpr>(*I)) { 897 // It will be analyzed later. 898 Vars.push_back(*I); 899 continue; 900 } 901 902 SourceLocation ELoc = (*I)->getExprLoc(); 903 // OpenMP [2.1, C/C++] 904 // A list item is a variable name. 905 // OpenMP [2.9.3.3, Restrictions, p.1] 906 // A variable that is part of another variable (as an array or 907 // structure element) cannot appear in a private clause. 908 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I); 909 if (!DE || !isa<VarDecl>(DE->getDecl())) { 910 Diag(ELoc, diag::err_omp_expected_var_name) 911 << (*I)->getSourceRange(); 912 continue; 913 } 914 Decl *D = DE->getDecl(); 915 VarDecl *VD = cast<VarDecl>(D); 916 917 QualType Type = VD->getType(); 918 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 919 // It will be analyzed later. 920 Vars.push_back(DE); 921 continue; 922 } 923 924 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 925 // A variable that appears in a private clause must not have an incomplete 926 // type or a reference type. 927 if (RequireCompleteType(ELoc, Type, 928 diag::err_omp_private_incomplete_type)) { 929 continue; 930 } 931 if (Type->isReferenceType()) { 932 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 933 << getOpenMPClauseName(OMPC_private) << Type; 934 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 935 VarDecl::DeclarationOnly; 936 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 937 diag::note_defined_here) << VD; 938 continue; 939 } 940 941 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 942 // A variable of class type (or array thereof) that appears in a private 943 // clause requires an accesible, unambiguous default constructor for the 944 // class type. 945 while (Type.getNonReferenceType()->isArrayType()) { 946 Type = cast<ArrayType>( 947 Type.getNonReferenceType().getTypePtr())->getElementType(); 948 } 949 CXXRecordDecl *RD = getLangOpts().CPlusPlus ? 950 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0; 951 if (RD) { 952 CXXConstructorDecl *CD = LookupDefaultConstructor(RD); 953 PartialDiagnostic PD = 954 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 955 if (!CD || 956 CheckConstructorAccess(ELoc, CD, 957 InitializedEntity::InitializeTemporary(Type), 958 CD->getAccess(), PD) == AR_inaccessible || 959 CD->isDeleted()) { 960 Diag(ELoc, diag::err_omp_required_method) 961 << getOpenMPClauseName(OMPC_private) << 0; 962 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 963 VarDecl::DeclarationOnly; 964 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 965 diag::note_defined_here) << VD; 966 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 967 continue; 968 } 969 MarkFunctionReferenced(ELoc, CD); 970 DiagnoseUseOfDecl(CD, ELoc); 971 972 CXXDestructorDecl *DD = RD->getDestructor(); 973 if (DD) { 974 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 975 DD->isDeleted()) { 976 Diag(ELoc, diag::err_omp_required_method) 977 << getOpenMPClauseName(OMPC_private) << 4; 978 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 979 VarDecl::DeclarationOnly; 980 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 981 diag::note_defined_here) << VD; 982 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 983 continue; 984 } 985 MarkFunctionReferenced(ELoc, DD); 986 DiagnoseUseOfDecl(DD, ELoc); 987 } 988 } 989 990 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 991 // in a Construct] 992 // Variables with the predetermined data-sharing attributes may not be 993 // listed in data-sharing attributes clauses, except for the cases 994 // listed below. For these exceptions only, listing a predetermined 995 // variable in a data-sharing attribute clause is allowed and overrides 996 // the variable's predetermined data-sharing attributes. 997 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 998 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 999 Diag(ELoc, diag::err_omp_wrong_dsa) 1000 << getOpenMPClauseName(DVar.CKind) 1001 << getOpenMPClauseName(OMPC_private); 1002 if (DVar.RefExpr) { 1003 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1004 << getOpenMPClauseName(DVar.CKind); 1005 } else { 1006 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa) 1007 << getOpenMPClauseName(DVar.CKind); 1008 } 1009 continue; 1010 } 1011 1012 DSAStack->addDSA(VD, DE, OMPC_private); 1013 Vars.push_back(DE); 1014 } 1015 1016 if (Vars.empty()) return 0; 1017 1018 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 1019 } 1020 1021 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 1022 SourceLocation StartLoc, 1023 SourceLocation LParenLoc, 1024 SourceLocation EndLoc) { 1025 SmallVector<Expr *, 8> Vars; 1026 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end(); 1027 I != E; ++I) { 1028 assert(*I && "NULL expr in OpenMP firstprivate clause."); 1029 if (isa<DependentScopeDeclRefExpr>(*I)) { 1030 // It will be analyzed later. 1031 Vars.push_back(*I); 1032 continue; 1033 } 1034 1035 SourceLocation ELoc = (*I)->getExprLoc(); 1036 // OpenMP [2.1, C/C++] 1037 // A list item is a variable name. 1038 // OpenMP [2.9.3.3, Restrictions, p.1] 1039 // A variable that is part of another variable (as an array or 1040 // structure element) cannot appear in a private clause. 1041 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I); 1042 if (!DE || !isa<VarDecl>(DE->getDecl())) { 1043 Diag(ELoc, diag::err_omp_expected_var_name) 1044 << (*I)->getSourceRange(); 1045 continue; 1046 } 1047 Decl *D = DE->getDecl(); 1048 VarDecl *VD = cast<VarDecl>(D); 1049 1050 QualType Type = VD->getType(); 1051 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 1052 // It will be analyzed later. 1053 Vars.push_back(DE); 1054 continue; 1055 } 1056 1057 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 1058 // A variable that appears in a private clause must not have an incomplete 1059 // type or a reference type. 1060 if (RequireCompleteType(ELoc, Type, 1061 diag::err_omp_firstprivate_incomplete_type)) { 1062 continue; 1063 } 1064 if (Type->isReferenceType()) { 1065 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 1066 << getOpenMPClauseName(OMPC_firstprivate) << Type; 1067 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 1068 VarDecl::DeclarationOnly; 1069 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 1070 diag::note_defined_here) << VD; 1071 continue; 1072 } 1073 1074 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 1075 // A variable of class type (or array thereof) that appears in a private 1076 // clause requires an accesible, unambiguous copy constructor for the 1077 // class type. 1078 Type = Context.getBaseElementType(Type); 1079 CXXRecordDecl *RD = getLangOpts().CPlusPlus ? 1080 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0; 1081 if (RD) { 1082 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0); 1083 PartialDiagnostic PD = 1084 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 1085 if (!CD || 1086 CheckConstructorAccess(ELoc, CD, 1087 InitializedEntity::InitializeTemporary(Type), 1088 CD->getAccess(), PD) == AR_inaccessible || 1089 CD->isDeleted()) { 1090 Diag(ELoc, diag::err_omp_required_method) 1091 << getOpenMPClauseName(OMPC_firstprivate) << 1; 1092 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 1093 VarDecl::DeclarationOnly; 1094 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 1095 diag::note_defined_here) << VD; 1096 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 1097 continue; 1098 } 1099 MarkFunctionReferenced(ELoc, CD); 1100 DiagnoseUseOfDecl(CD, ELoc); 1101 1102 CXXDestructorDecl *DD = RD->getDestructor(); 1103 if (DD) { 1104 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 1105 DD->isDeleted()) { 1106 Diag(ELoc, diag::err_omp_required_method) 1107 << getOpenMPClauseName(OMPC_firstprivate) << 4; 1108 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 1109 VarDecl::DeclarationOnly; 1110 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl : 1111 diag::note_defined_here) << VD; 1112 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 1113 continue; 1114 } 1115 MarkFunctionReferenced(ELoc, DD); 1116 DiagnoseUseOfDecl(DD, ELoc); 1117 } 1118 } 1119 1120 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate 1121 // variable and it was checked already. 1122 if (StartLoc.isValid() && EndLoc.isValid()) { 1123 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 1124 Type = Type.getNonReferenceType().getCanonicalType(); 1125 bool IsConstant = Type.isConstant(Context); 1126 Type = Context.getBaseElementType(Type); 1127 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 1128 // A list item that specifies a given variable may not appear in more 1129 // than one clause on the same directive, except that a variable may be 1130 // specified in both firstprivate and lastprivate clauses. 1131 // TODO: add processing for lastprivate. 1132 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 1133 DVar.RefExpr) { 1134 Diag(ELoc, diag::err_omp_wrong_dsa) 1135 << getOpenMPClauseName(DVar.CKind) 1136 << getOpenMPClauseName(OMPC_firstprivate); 1137 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1138 << getOpenMPClauseName(DVar.CKind); 1139 continue; 1140 } 1141 1142 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1143 // in a Construct] 1144 // Variables with the predetermined data-sharing attributes may not be 1145 // listed in data-sharing attributes clauses, except for the cases 1146 // listed below. For these exceptions only, listing a predetermined 1147 // variable in a data-sharing attribute clause is allowed and overrides 1148 // the variable's predetermined data-sharing attributes. 1149 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1150 // in a Construct, C/C++, p.2] 1151 // Variables with const-qualified type having no mutable member may be 1152 // listed in a firstprivate clause, even if they are static data members. 1153 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && 1154 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 1155 Diag(ELoc, diag::err_omp_wrong_dsa) 1156 << getOpenMPClauseName(DVar.CKind) 1157 << getOpenMPClauseName(OMPC_firstprivate); 1158 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa) 1159 << getOpenMPClauseName(DVar.CKind); 1160 continue; 1161 } 1162 1163 // OpenMP [2.9.3.4, Restrictions, p.2] 1164 // A list item that is private within a parallel region must not appear 1165 // in a firstprivate clause on a worksharing construct if any of the 1166 // worksharing regions arising from the worksharing construct ever bind 1167 // to any of the parallel regions arising from the parallel construct. 1168 // OpenMP [2.9.3.4, Restrictions, p.3] 1169 // A list item that appears in a reduction clause of a parallel construct 1170 // must not appear in a firstprivate clause on a worksharing or task 1171 // construct if any of the worksharing or task regions arising from the 1172 // worksharing or task construct ever bind to any of the parallel regions 1173 // arising from the parallel construct. 1174 // OpenMP [2.9.3.4, Restrictions, p.4] 1175 // A list item that appears in a reduction clause in worksharing 1176 // construct must not appear in a firstprivate clause in a task construct 1177 // encountered during execution of any of the worksharing regions arising 1178 // from the worksharing construct. 1179 // TODO: 1180 } 1181 1182 DSAStack->addDSA(VD, DE, OMPC_firstprivate); 1183 Vars.push_back(DE); 1184 } 1185 1186 if (Vars.empty()) return 0; 1187 1188 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 1189 Vars); 1190 } 1191 1192 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 1193 SourceLocation StartLoc, 1194 SourceLocation LParenLoc, 1195 SourceLocation EndLoc) { 1196 SmallVector<Expr *, 8> Vars; 1197 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end(); 1198 I != E; ++I) { 1199 assert(*I && "NULL expr in OpenMP shared clause."); 1200 if (isa<DependentScopeDeclRefExpr>(*I)) { 1201 // It will be analyzed later. 1202 Vars.push_back(*I); 1203 continue; 1204 } 1205 1206 SourceLocation ELoc = (*I)->getExprLoc(); 1207 // OpenMP [2.1, C/C++] 1208 // A list item is a variable name. 1209 // OpenMP [2.9.3.4, Restrictions, p.1] 1210 // A variable that is part of another variable (as an array or 1211 // structure element) cannot appear in a private clause. 1212 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I); 1213 if (!DE || !isa<VarDecl>(DE->getDecl())) { 1214 Diag(ELoc, diag::err_omp_expected_var_name) 1215 << (*I)->getSourceRange(); 1216 continue; 1217 } 1218 Decl *D = DE->getDecl(); 1219 VarDecl *VD = cast<VarDecl>(D); 1220 1221 QualType Type = VD->getType(); 1222 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 1223 // It will be analyzed later. 1224 Vars.push_back(DE); 1225 continue; 1226 } 1227 1228 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 1229 // in a Construct] 1230 // Variables with the predetermined data-sharing attributes may not be 1231 // listed in data-sharing attributes clauses, except for the cases 1232 // listed below. For these exceptions only, listing a predetermined 1233 // variable in a data-sharing attribute clause is allowed and overrides 1234 // the variable's predetermined data-sharing attributes. 1235 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 1236 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) { 1237 Diag(ELoc, diag::err_omp_wrong_dsa) 1238 << getOpenMPClauseName(DVar.CKind) 1239 << getOpenMPClauseName(OMPC_shared); 1240 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 1241 << getOpenMPClauseName(DVar.CKind); 1242 continue; 1243 } 1244 1245 DSAStack->addDSA(VD, DE, OMPC_shared); 1246 Vars.push_back(DE); 1247 } 1248 1249 if (Vars.empty()) return 0; 1250 1251 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 1252 } 1253 1254 #undef DSAStack 1255