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/AST/ASTContext.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/Basic/OpenMPKinds.h" 23 #include "clang/Lex/Preprocessor.h" 24 #include "clang/Sema/Initialization.h" 25 #include "clang/Sema/Lookup.h" 26 #include "clang/Sema/Scope.h" 27 #include "clang/Sema/ScopeInfo.h" 28 #include "clang/Sema/SemaInternal.h" 29 using namespace clang; 30 31 //===----------------------------------------------------------------------===// 32 // Stack of data-sharing attributes for variables 33 //===----------------------------------------------------------------------===// 34 35 namespace { 36 /// \brief Default data sharing attributes, which can be applied to directive. 37 enum DefaultDataSharingAttributes { 38 DSA_unspecified = 0, /// \brief Data sharing attribute not specified. 39 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'. 40 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'. 41 }; 42 43 template <class T> struct MatchesAny { 44 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {} 45 bool operator()(T Kind) { 46 for (auto KindEl : Arr) 47 if (KindEl == Kind) 48 return true; 49 return false; 50 } 51 52 private: 53 ArrayRef<T> Arr; 54 }; 55 struct MatchesAlways { 56 MatchesAlways() {} 57 template <class T> bool operator()(T) { return true; } 58 }; 59 60 typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause; 61 typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective; 62 63 /// \brief Stack for tracking declarations used in OpenMP directives and 64 /// clauses and their data-sharing attributes. 65 class DSAStackTy { 66 public: 67 struct DSAVarData { 68 OpenMPDirectiveKind DKind; 69 OpenMPClauseKind CKind; 70 DeclRefExpr *RefExpr; 71 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr) {} 72 }; 73 74 private: 75 struct DSAInfo { 76 OpenMPClauseKind Attributes; 77 DeclRefExpr *RefExpr; 78 }; 79 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy; 80 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy; 81 82 struct SharingMapTy { 83 DeclSAMapTy SharingMap; 84 AlignedMapTy AlignedMap; 85 DefaultDataSharingAttributes DefaultAttr; 86 OpenMPDirectiveKind Directive; 87 DeclarationNameInfo DirectiveName; 88 Scope *CurScope; 89 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, 90 Scope *CurScope) 91 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified), 92 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope) { 93 } 94 SharingMapTy() 95 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified), 96 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr) {} 97 }; 98 99 typedef SmallVector<SharingMapTy, 64> StackTy; 100 101 /// \brief Stack of used declaration and their data-sharing attributes. 102 StackTy Stack; 103 Sema &SemaRef; 104 105 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; 106 107 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D); 108 109 /// \brief Checks if the variable is a local for OpenMP region. 110 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); 111 112 public: 113 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {} 114 115 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, 116 Scope *CurScope) { 117 Stack.push_back(SharingMapTy(DKind, DirName, CurScope)); 118 } 119 120 void pop() { 121 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); 122 Stack.pop_back(); 123 } 124 125 /// \brief If 'aligned' declaration for given variable \a D was not seen yet, 126 /// add it and return NULL; otherwise return previous occurrence's expression 127 /// for diagnostics. 128 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE); 129 130 /// \brief Adds explicit data sharing attribute to the specified declaration. 131 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A); 132 133 /// \brief Returns data sharing attributes from top of the stack for the 134 /// specified declaration. 135 DSAVarData getTopDSA(VarDecl *D); 136 /// \brief Returns data-sharing attributes for the specified declaration. 137 DSAVarData getImplicitDSA(VarDecl *D); 138 /// \brief Checks if the specified variables has data-sharing attributes which 139 /// match specified \a CPred predicate in any directive which matches \a DPred 140 /// predicate. 141 template <class ClausesPredicate, class DirectivesPredicate> 142 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred, 143 DirectivesPredicate DPred); 144 /// \brief Checks if the specified variables has data-sharing attributes which 145 /// match specified \a CPred predicate in any innermost directive which 146 /// matches \a DPred predicate. 147 template <class ClausesPredicate, class DirectivesPredicate> 148 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred, 149 DirectivesPredicate DPred); 150 151 /// \brief Returns currently analyzed directive. 152 OpenMPDirectiveKind getCurrentDirective() const { 153 return Stack.back().Directive; 154 } 155 /// \brief Returns parent directive. 156 OpenMPDirectiveKind getParentDirective() const { 157 if (Stack.size() > 2) 158 return Stack[Stack.size() - 2].Directive; 159 return OMPD_unknown; 160 } 161 162 /// \brief Set default data sharing attribute to none. 163 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; } 164 /// \brief Set default data sharing attribute to shared. 165 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; } 166 167 DefaultDataSharingAttributes getDefaultDSA() const { 168 return Stack.back().DefaultAttr; 169 } 170 171 /// \brief Checks if the specified variable is a threadprivate. 172 bool isThreadPrivate(VarDecl *D) { 173 DSAVarData DVar = getTopDSA(D); 174 return isOpenMPThreadPrivate(DVar.CKind); 175 } 176 177 Scope *getCurScope() const { return Stack.back().CurScope; } 178 Scope *getCurScope() { return Stack.back().CurScope; } 179 }; 180 } // namespace 181 182 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter, 183 VarDecl *D) { 184 DSAVarData DVar; 185 if (Iter == std::prev(Stack.rend())) { 186 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 187 // in a region but not in construct] 188 // File-scope or namespace-scope variables referenced in called routines 189 // in the region are shared unless they appear in a threadprivate 190 // directive. 191 if (!D->isFunctionOrMethodVarDecl()) 192 DVar.CKind = OMPC_shared; 193 194 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced 195 // in a region but not in construct] 196 // Variables with static storage duration that are declared in called 197 // routines in the region are shared. 198 if (D->hasGlobalStorage()) 199 DVar.CKind = OMPC_shared; 200 201 return DVar; 202 } 203 204 DVar.DKind = Iter->Directive; 205 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 206 // in a Construct, C/C++, predetermined, p.1] 207 // Variables with automatic storage duration that are declared in a scope 208 // inside the construct are private. 209 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() && 210 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { 211 DVar.CKind = OMPC_private; 212 return DVar; 213 } 214 215 // Explicitly specified attributes and local variables with predetermined 216 // attributes. 217 if (Iter->SharingMap.count(D)) { 218 DVar.RefExpr = Iter->SharingMap[D].RefExpr; 219 DVar.CKind = Iter->SharingMap[D].Attributes; 220 return DVar; 221 } 222 223 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 224 // in a Construct, C/C++, implicitly determined, p.1] 225 // In a parallel or task construct, the data-sharing attributes of these 226 // variables are determined by the default clause, if present. 227 switch (Iter->DefaultAttr) { 228 case DSA_shared: 229 DVar.CKind = OMPC_shared; 230 return DVar; 231 case DSA_none: 232 return DVar; 233 case DSA_unspecified: 234 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 235 // in a Construct, implicitly determined, p.2] 236 // In a parallel construct, if no default clause is present, these 237 // variables are shared. 238 if (isOpenMPParallelDirective(DVar.DKind)) { 239 DVar.CKind = OMPC_shared; 240 return DVar; 241 } 242 243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 244 // in a Construct, implicitly determined, p.4] 245 // In a task construct, if no default clause is present, a variable that in 246 // the enclosing context is determined to be shared by all implicit tasks 247 // bound to the current team is shared. 248 if (DVar.DKind == OMPD_task) { 249 DSAVarData DVarTemp; 250 for (StackTy::reverse_iterator I = std::next(Iter), 251 EE = std::prev(Stack.rend()); 252 I != EE; ++I) { 253 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables 254 // Referenced 255 // in a Construct, implicitly determined, p.6] 256 // In a task construct, if no default clause is present, a variable 257 // whose data-sharing attribute is not determined by the rules above is 258 // firstprivate. 259 DVarTemp = getDSA(I, D); 260 if (DVarTemp.CKind != OMPC_shared) { 261 DVar.RefExpr = nullptr; 262 DVar.DKind = OMPD_task; 263 DVar.CKind = OMPC_firstprivate; 264 return DVar; 265 } 266 if (isOpenMPParallelDirective(I->Directive)) 267 break; 268 } 269 DVar.DKind = OMPD_task; 270 DVar.CKind = 271 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; 272 return DVar; 273 } 274 } 275 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 276 // in a Construct, implicitly determined, p.3] 277 // For constructs other than task, if no default clause is present, these 278 // variables inherit their data-sharing attributes from the enclosing 279 // context. 280 return getDSA(std::next(Iter), D); 281 } 282 283 DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) { 284 assert(Stack.size() > 1 && "Data sharing attributes stack is empty"); 285 auto It = Stack.back().AlignedMap.find(D); 286 if (It == Stack.back().AlignedMap.end()) { 287 assert(NewDE && "Unexpected nullptr expr to be added into aligned map"); 288 Stack.back().AlignedMap[D] = NewDE; 289 return nullptr; 290 } else { 291 assert(It->second && "Unexpected nullptr expr in the aligned map"); 292 return It->second; 293 } 294 return nullptr; 295 } 296 297 void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) { 298 if (A == OMPC_threadprivate) { 299 Stack[0].SharingMap[D].Attributes = A; 300 Stack[0].SharingMap[D].RefExpr = E; 301 } else { 302 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); 303 Stack.back().SharingMap[D].Attributes = A; 304 Stack.back().SharingMap[D].RefExpr = E; 305 } 306 } 307 308 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { 309 if (Stack.size() > 2) { 310 reverse_iterator I = Iter, E = std::prev(Stack.rend()); 311 Scope *TopScope = nullptr; 312 while (I != E && !isOpenMPParallelDirective(I->Directive)) { 313 ++I; 314 } 315 if (I == E) 316 return false; 317 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; 318 Scope *CurScope = getCurScope(); 319 while (CurScope != TopScope && !CurScope->isDeclScope(D)) { 320 CurScope = CurScope->getParent(); 321 } 322 return CurScope != TopScope; 323 } 324 return false; 325 } 326 327 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) { 328 DSAVarData DVar; 329 330 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 331 // in a Construct, C/C++, predetermined, p.1] 332 // Variables appearing in threadprivate directives are threadprivate. 333 if (D->getTLSKind() != VarDecl::TLS_None) { 334 DVar.CKind = OMPC_threadprivate; 335 return DVar; 336 } 337 if (Stack[0].SharingMap.count(D)) { 338 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr; 339 DVar.CKind = OMPC_threadprivate; 340 return DVar; 341 } 342 343 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 344 // in a Construct, C/C++, predetermined, p.1] 345 // Variables with automatic storage duration that are declared in a scope 346 // inside the construct are private. 347 OpenMPDirectiveKind Kind = getCurrentDirective(); 348 if (!isOpenMPParallelDirective(Kind)) { 349 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() && 350 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { 351 DVar.CKind = OMPC_private; 352 return DVar; 353 } 354 } 355 356 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 357 // in a Construct, C/C++, predetermined, p.4] 358 // Static data members are shared. 359 if (D->isStaticDataMember()) { 360 // Variables with const-qualified type having no mutable member may be 361 // listed in a firstprivate clause, even if they are static data members. 362 DSAVarData DVarTemp = 363 hasDSA(D, MatchesAnyClause(OMPC_firstprivate), MatchesAlways()); 364 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 365 return DVar; 366 367 DVar.CKind = OMPC_shared; 368 return DVar; 369 } 370 371 QualType Type = D->getType().getNonReferenceType().getCanonicalType(); 372 bool IsConstant = Type.isConstant(SemaRef.getASTContext()); 373 while (Type->isArrayType()) { 374 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType(); 375 Type = ElemType.getNonReferenceType().getCanonicalType(); 376 } 377 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 378 // in a Construct, C/C++, predetermined, p.6] 379 // Variables with const qualified type having no mutable member are 380 // shared. 381 CXXRecordDecl *RD = 382 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 383 if (IsConstant && 384 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) { 385 // Variables with const-qualified type having no mutable member may be 386 // listed in a firstprivate clause, even if they are static data members. 387 DSAVarData DVarTemp = 388 hasDSA(D, MatchesAnyClause(OMPC_firstprivate), MatchesAlways()); 389 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) 390 return DVar; 391 392 DVar.CKind = OMPC_shared; 393 return DVar; 394 } 395 396 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 397 // in a Construct, C/C++, predetermined, p.7] 398 // Variables with static storage duration that are declared in a scope 399 // inside the construct are shared. 400 if (D->isStaticLocal()) { 401 DVar.CKind = OMPC_shared; 402 return DVar; 403 } 404 405 // Explicitly specified attributes and local variables with predetermined 406 // attributes. 407 if (Stack.back().SharingMap.count(D)) { 408 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr; 409 DVar.CKind = Stack.back().SharingMap[D].Attributes; 410 } 411 412 return DVar; 413 } 414 415 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) { 416 return getDSA(std::next(Stack.rbegin()), D); 417 } 418 419 template <class ClausesPredicate, class DirectivesPredicate> 420 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred, 421 DirectivesPredicate DPred) { 422 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()), 423 E = std::prev(Stack.rend()); 424 I != E; ++I) { 425 if (!DPred(I->Directive)) 426 continue; 427 DSAVarData DVar = getDSA(I, D); 428 if (CPred(DVar.CKind)) 429 return DVar; 430 } 431 return DSAVarData(); 432 } 433 434 template <class ClausesPredicate, class DirectivesPredicate> 435 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(VarDecl *D, 436 ClausesPredicate CPred, 437 DirectivesPredicate DPred) { 438 for (auto I = Stack.rbegin(), EE = std::prev(Stack.rend()); I != EE; ++I) { 439 if (!DPred(I->Directive)) 440 continue; 441 DSAVarData DVar = getDSA(I, D); 442 if (CPred(DVar.CKind)) 443 return DVar; 444 return DSAVarData(); 445 } 446 return DSAVarData(); 447 } 448 449 void Sema::InitDataSharingAttributesStack() { 450 VarDataSharingAttributesStack = new DSAStackTy(*this); 451 } 452 453 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) 454 455 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } 456 457 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, 458 const DeclarationNameInfo &DirName, 459 Scope *CurScope) { 460 DSAStack->push(DKind, DirName, CurScope); 461 PushExpressionEvaluationContext(PotentiallyEvaluated); 462 } 463 464 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { 465 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1] 466 // A variable of class type (or array thereof) that appears in a lastprivate 467 // clause requires an accessible, unambiguous default constructor for the 468 // class type, unless the list item is also specified in a firstprivate 469 // clause. 470 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) { 471 for (auto C : D->clauses()) { 472 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) { 473 for (auto VarRef : Clause->varlists()) { 474 if (VarRef->isValueDependent() || VarRef->isTypeDependent()) 475 continue; 476 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl()); 477 auto DVar = DSAStack->getTopDSA(VD); 478 if (DVar.CKind == OMPC_lastprivate) { 479 SourceLocation ELoc = VarRef->getExprLoc(); 480 auto Type = VarRef->getType(); 481 if (Type->isArrayType()) 482 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0); 483 CXXRecordDecl *RD = 484 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 485 // FIXME This code must be replaced by actual constructing of the 486 // lastprivate variable. 487 if (RD) { 488 CXXConstructorDecl *CD = LookupDefaultConstructor(RD); 489 PartialDiagnostic PD = 490 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 491 if (!CD || 492 CheckConstructorAccess( 493 ELoc, CD, InitializedEntity::InitializeTemporary(Type), 494 CD->getAccess(), PD) == AR_inaccessible || 495 CD->isDeleted()) { 496 Diag(ELoc, diag::err_omp_required_method) 497 << getOpenMPClauseName(OMPC_lastprivate) << 0; 498 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 499 VarDecl::DeclarationOnly; 500 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl 501 : diag::note_defined_here) 502 << VD; 503 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 504 continue; 505 } 506 MarkFunctionReferenced(ELoc, CD); 507 DiagnoseUseOfDecl(CD, ELoc); 508 } 509 } 510 } 511 } 512 } 513 } 514 515 DSAStack->pop(); 516 DiscardCleanupsInEvaluationContext(); 517 PopExpressionEvaluationContext(); 518 } 519 520 namespace { 521 522 class VarDeclFilterCCC : public CorrectionCandidateCallback { 523 private: 524 Sema &SemaRef; 525 526 public: 527 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {} 528 bool ValidateCandidate(const TypoCorrection &Candidate) override { 529 NamedDecl *ND = Candidate.getCorrectionDecl(); 530 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) { 531 return VD->hasGlobalStorage() && 532 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(), 533 SemaRef.getCurScope()); 534 } 535 return false; 536 } 537 }; 538 } // namespace 539 540 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, 541 CXXScopeSpec &ScopeSpec, 542 const DeclarationNameInfo &Id) { 543 LookupResult Lookup(*this, Id, LookupOrdinaryName); 544 LookupParsedName(Lookup, CurScope, &ScopeSpec, true); 545 546 if (Lookup.isAmbiguous()) 547 return ExprError(); 548 549 VarDecl *VD; 550 if (!Lookup.isSingleResult()) { 551 VarDeclFilterCCC Validator(*this); 552 if (TypoCorrection Corrected = 553 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator, 554 CTK_ErrorRecovery)) { 555 diagnoseTypo(Corrected, 556 PDiag(Lookup.empty() 557 ? diag::err_undeclared_var_use_suggest 558 : diag::err_omp_expected_var_arg_suggest) 559 << Id.getName()); 560 VD = Corrected.getCorrectionDeclAs<VarDecl>(); 561 } else { 562 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use 563 : diag::err_omp_expected_var_arg) 564 << Id.getName(); 565 return ExprError(); 566 } 567 } else { 568 if (!(VD = Lookup.getAsSingle<VarDecl>())) { 569 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); 570 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); 571 return ExprError(); 572 } 573 } 574 Lookup.suppressDiagnostics(); 575 576 // OpenMP [2.9.2, Syntax, C/C++] 577 // Variables must be file-scope, namespace-scope, or static block-scope. 578 if (!VD->hasGlobalStorage()) { 579 Diag(Id.getLoc(), diag::err_omp_global_var_arg) 580 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); 581 bool IsDecl = 582 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 583 Diag(VD->getLocation(), 584 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 585 << VD; 586 return ExprError(); 587 } 588 589 VarDecl *CanonicalVD = VD->getCanonicalDecl(); 590 NamedDecl *ND = cast<NamedDecl>(CanonicalVD); 591 // OpenMP [2.9.2, Restrictions, C/C++, p.2] 592 // A threadprivate directive for file-scope variables must appear outside 593 // any definition or declaration. 594 if (CanonicalVD->getDeclContext()->isTranslationUnit() && 595 !getCurLexicalContext()->isTranslationUnit()) { 596 Diag(Id.getLoc(), diag::err_omp_var_scope) 597 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 598 bool IsDecl = 599 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 600 Diag(VD->getLocation(), 601 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 602 << VD; 603 return ExprError(); 604 } 605 // OpenMP [2.9.2, Restrictions, C/C++, p.3] 606 // A threadprivate directive for static class member variables must appear 607 // in the class definition, in the same scope in which the member 608 // variables are declared. 609 if (CanonicalVD->isStaticDataMember() && 610 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { 611 Diag(Id.getLoc(), diag::err_omp_var_scope) 612 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 613 bool IsDecl = 614 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 615 Diag(VD->getLocation(), 616 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 617 << VD; 618 return ExprError(); 619 } 620 // OpenMP [2.9.2, Restrictions, C/C++, p.4] 621 // A threadprivate directive for namespace-scope variables must appear 622 // outside any definition or declaration other than the namespace 623 // definition itself. 624 if (CanonicalVD->getDeclContext()->isNamespace() && 625 (!getCurLexicalContext()->isFileContext() || 626 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { 627 Diag(Id.getLoc(), diag::err_omp_var_scope) 628 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 629 bool IsDecl = 630 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 631 Diag(VD->getLocation(), 632 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 633 << VD; 634 return ExprError(); 635 } 636 // OpenMP [2.9.2, Restrictions, C/C++, p.6] 637 // A threadprivate directive for static block-scope variables must appear 638 // in the scope of the variable and not in a nested scope. 639 if (CanonicalVD->isStaticLocal() && CurScope && 640 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { 641 Diag(Id.getLoc(), diag::err_omp_var_scope) 642 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 643 bool IsDecl = 644 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 645 Diag(VD->getLocation(), 646 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 647 << VD; 648 return ExprError(); 649 } 650 651 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] 652 // A threadprivate directive must lexically precede all references to any 653 // of the variables in its list. 654 if (VD->isUsed()) { 655 Diag(Id.getLoc(), diag::err_omp_var_used) 656 << getOpenMPDirectiveName(OMPD_threadprivate) << VD; 657 return ExprError(); 658 } 659 660 QualType ExprType = VD->getType().getNonReferenceType(); 661 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc()); 662 return DE; 663 } 664 665 Sema::DeclGroupPtrTy 666 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, 667 ArrayRef<Expr *> VarList) { 668 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { 669 CurContext->addDecl(D); 670 return DeclGroupPtrTy::make(DeclGroupRef(D)); 671 } 672 return DeclGroupPtrTy(); 673 } 674 675 namespace { 676 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> { 677 Sema &SemaRef; 678 679 public: 680 bool VisitDeclRefExpr(const DeclRefExpr *E) { 681 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) { 682 if (VD->hasLocalStorage()) { 683 SemaRef.Diag(E->getLocStart(), 684 diag::err_omp_local_var_in_threadprivate_init) 685 << E->getSourceRange(); 686 SemaRef.Diag(VD->getLocation(), diag::note_defined_here) 687 << VD << VD->getSourceRange(); 688 return true; 689 } 690 } 691 return false; 692 } 693 bool VisitStmt(const Stmt *S) { 694 for (auto Child : S->children()) { 695 if (Child && Visit(Child)) 696 return true; 697 } 698 return false; 699 } 700 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {} 701 }; 702 } // namespace 703 704 OMPThreadPrivateDecl * 705 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { 706 SmallVector<Expr *, 8> Vars; 707 for (auto &RefExpr : VarList) { 708 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); 709 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 710 SourceLocation ILoc = DE->getExprLoc(); 711 712 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 713 // A threadprivate variable must not have an incomplete type. 714 if (RequireCompleteType(ILoc, VD->getType(), 715 diag::err_omp_threadprivate_incomplete_type)) { 716 continue; 717 } 718 719 // OpenMP [2.9.2, Restrictions, C/C++, p.10] 720 // A threadprivate variable must not have a reference type. 721 if (VD->getType()->isReferenceType()) { 722 Diag(ILoc, diag::err_omp_ref_type_arg) 723 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); 724 bool IsDecl = 725 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 726 Diag(VD->getLocation(), 727 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 728 << VD; 729 continue; 730 } 731 732 // Check if this is a TLS variable. 733 if (VD->getTLSKind()) { 734 Diag(ILoc, diag::err_omp_var_thread_local) << VD; 735 bool IsDecl = 736 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 737 Diag(VD->getLocation(), 738 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 739 << VD; 740 continue; 741 } 742 743 // Check if initial value of threadprivate variable reference variable with 744 // local storage (it is not supported by runtime). 745 if (auto Init = VD->getAnyInitializer()) { 746 LocalVarRefChecker Checker(*this); 747 if (Checker.Visit(Init)) 748 continue; 749 } 750 751 Vars.push_back(RefExpr); 752 DSAStack->addDSA(VD, DE, OMPC_threadprivate); 753 } 754 OMPThreadPrivateDecl *D = nullptr; 755 if (!Vars.empty()) { 756 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, 757 Vars); 758 D->setAccess(AS_public); 759 } 760 return D; 761 } 762 763 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack, 764 const VarDecl *VD, DSAStackTy::DSAVarData DVar, 765 bool IsLoopIterVar = false) { 766 if (DVar.RefExpr) { 767 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) 768 << getOpenMPClauseName(DVar.CKind); 769 return; 770 } 771 enum { 772 PDSA_StaticMemberShared, 773 PDSA_StaticLocalVarShared, 774 PDSA_LoopIterVarPrivate, 775 PDSA_LoopIterVarLinear, 776 PDSA_LoopIterVarLastprivate, 777 PDSA_ConstVarShared, 778 PDSA_GlobalVarShared, 779 PDSA_LocalVarPrivate 780 } Reason; 781 bool ReportHint = false; 782 if (IsLoopIterVar) { 783 if (DVar.CKind == OMPC_private) 784 Reason = PDSA_LoopIterVarPrivate; 785 else if (DVar.CKind == OMPC_lastprivate) 786 Reason = PDSA_LoopIterVarLastprivate; 787 else 788 Reason = PDSA_LoopIterVarLinear; 789 } else if (VD->isStaticLocal()) 790 Reason = PDSA_StaticLocalVarShared; 791 else if (VD->isStaticDataMember()) 792 Reason = PDSA_StaticMemberShared; 793 else if (VD->isFileVarDecl()) 794 Reason = PDSA_GlobalVarShared; 795 else if (VD->getType().isConstant(SemaRef.getASTContext())) 796 Reason = PDSA_ConstVarShared; 797 else { 798 ReportHint = true; 799 Reason = PDSA_LocalVarPrivate; 800 } 801 802 SemaRef.Diag(VD->getLocation(), diag::note_omp_predetermined_dsa) 803 << Reason << ReportHint 804 << getOpenMPDirectiveName(Stack->getCurrentDirective()); 805 } 806 807 namespace { 808 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { 809 DSAStackTy *Stack; 810 Sema &SemaRef; 811 bool ErrorFound; 812 CapturedStmt *CS; 813 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; 814 815 public: 816 void VisitDeclRefExpr(DeclRefExpr *E) { 817 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 818 // Skip internally declared variables. 819 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) 820 return; 821 822 SourceLocation ELoc = E->getExprLoc(); 823 824 OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); 825 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD); 826 if (DVar.CKind != OMPC_unknown) { 827 if (DKind == OMPD_task && DVar.CKind != OMPC_shared && 828 !Stack->isThreadPrivate(VD) && !DVar.RefExpr) 829 ImplicitFirstprivate.push_back(DVar.RefExpr); 830 return; 831 } 832 // The default(none) clause requires that each variable that is referenced 833 // in the construct, and does not have a predetermined data-sharing 834 // attribute, must have its data-sharing attribute explicitly determined 835 // by being listed in a data-sharing attribute clause. 836 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && 837 (isOpenMPParallelDirective(DKind) || DKind == OMPD_task)) { 838 ErrorFound = true; 839 SemaRef.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD; 840 return; 841 } 842 843 // OpenMP [2.9.3.6, Restrictions, p.2] 844 // A list item that appears in a reduction clause of the innermost 845 // enclosing worksharing or parallel construct may not be accessed in an 846 // explicit task. 847 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction), 848 MatchesAlways()); 849 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) { 850 ErrorFound = true; 851 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task); 852 ReportOriginalDSA(SemaRef, Stack, VD, DVar); 853 return; 854 } 855 856 // Define implicit data-sharing attributes for task. 857 DVar = Stack->getImplicitDSA(VD); 858 if (DKind == OMPD_task && DVar.CKind != OMPC_shared) 859 ImplicitFirstprivate.push_back(DVar.RefExpr); 860 } 861 } 862 void VisitOMPExecutableDirective(OMPExecutableDirective *S) { 863 for (auto C : S->clauses()) 864 if (C) 865 for (StmtRange R = C->children(); R; ++R) 866 if (Stmt *Child = *R) 867 Visit(Child); 868 } 869 void VisitStmt(Stmt *S) { 870 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E; 871 ++I) 872 if (Stmt *Child = *I) 873 if (!isa<OMPExecutableDirective>(Child)) 874 Visit(Child); 875 } 876 877 bool isErrorFound() { return ErrorFound; } 878 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } 879 880 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS) 881 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {} 882 }; 883 } // namespace 884 885 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc, 886 Scope *CurScope) { 887 switch (DKind) { 888 case OMPD_parallel: { 889 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); 890 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); 891 Sema::CapturedParamNameType Params[] = { 892 std::make_pair(".global_tid.", KmpInt32PtrTy), 893 std::make_pair(".bound_tid.", KmpInt32PtrTy), 894 std::make_pair(StringRef(), QualType()) // __context with shared vars 895 }; 896 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params); 897 break; 898 } 899 case OMPD_simd: { 900 Sema::CapturedParamNameType Params[] = { 901 std::make_pair(StringRef(), QualType()) // __context with shared vars 902 }; 903 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params); 904 break; 905 } 906 case OMPD_for: { 907 Sema::CapturedParamNameType Params[] = { 908 std::make_pair(StringRef(), QualType()) // __context with shared vars 909 }; 910 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params); 911 break; 912 } 913 case OMPD_sections: { 914 Sema::CapturedParamNameType Params[] = { 915 std::make_pair(StringRef(), QualType()) // __context with shared vars 916 }; 917 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params); 918 break; 919 } 920 case OMPD_section: { 921 Sema::CapturedParamNameType Params[] = { 922 std::make_pair(StringRef(), QualType()) // __context with shared vars 923 }; 924 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params); 925 break; 926 } 927 case OMPD_single: { 928 Sema::CapturedParamNameType Params[] = { 929 std::make_pair(StringRef(), QualType()) // __context with shared vars 930 }; 931 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params); 932 break; 933 } 934 case OMPD_threadprivate: 935 case OMPD_task: 936 llvm_unreachable("OpenMP Directive is not allowed"); 937 case OMPD_unknown: 938 llvm_unreachable("Unknown OpenMP directive"); 939 } 940 } 941 942 bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack, 943 OpenMPDirectiveKind CurrentRegion, 944 SourceLocation StartLoc) { 945 if (Stack->getCurScope()) { 946 auto ParentRegion = Stack->getParentDirective(); 947 bool NestingProhibited = false; 948 bool CloseNesting = true; 949 bool ShouldBeInParallelRegion = false; 950 if (isOpenMPSimdDirective(ParentRegion)) { 951 // OpenMP [2.16, Nesting of Regions] 952 // OpenMP constructs may not be nested inside a simd region. 953 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd); 954 return true; 955 } 956 if (CurrentRegion == OMPD_section) { 957 // OpenMP [2.7.2, sections Construct, Restrictions] 958 // Orphaned section directives are prohibited. That is, the section 959 // directives must appear within the sections construct and must not be 960 // encountered elsewhere in the sections region. 961 if (ParentRegion != OMPD_sections) { 962 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive) 963 << (ParentRegion != OMPD_unknown) 964 << getOpenMPDirectiveName(ParentRegion); 965 return true; 966 } 967 return false; 968 } 969 if (isOpenMPWorksharingDirective(CurrentRegion) && 970 !isOpenMPParallelDirective(CurrentRegion) && 971 !isOpenMPSimdDirective(CurrentRegion)) { 972 // OpenMP [2.16, Nesting of Regions] 973 // A worksharing region may not be closely nested inside a worksharing, 974 // explicit task, critical, ordered, atomic, or master region. 975 // TODO 976 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) && 977 !isOpenMPSimdDirective(ParentRegion); 978 ShouldBeInParallelRegion = true; 979 } 980 if (NestingProhibited) { 981 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region) 982 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << true 983 << getOpenMPDirectiveName(CurrentRegion) << ShouldBeInParallelRegion; 984 return true; 985 } 986 } 987 return false; 988 } 989 990 StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, 991 ArrayRef<OMPClause *> Clauses, 992 Stmt *AStmt, 993 SourceLocation StartLoc, 994 SourceLocation EndLoc) { 995 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 996 997 StmtResult Res = StmtError(); 998 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc)) 999 return StmtError(); 1000 1001 // Check default data sharing attributes for referenced variables. 1002 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); 1003 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); 1004 if (DSAChecker.isErrorFound()) 1005 return StmtError(); 1006 // Generate list of implicitly defined firstprivate variables. 1007 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; 1008 ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); 1009 1010 bool ErrorFound = false; 1011 if (!DSAChecker.getImplicitFirstprivate().empty()) { 1012 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( 1013 DSAChecker.getImplicitFirstprivate(), SourceLocation(), 1014 SourceLocation(), SourceLocation())) { 1015 ClausesWithImplicit.push_back(Implicit); 1016 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != 1017 DSAChecker.getImplicitFirstprivate().size(); 1018 } else 1019 ErrorFound = true; 1020 } 1021 1022 switch (Kind) { 1023 case OMPD_parallel: 1024 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, 1025 EndLoc); 1026 break; 1027 case OMPD_simd: 1028 Res = 1029 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 1030 break; 1031 case OMPD_for: 1032 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); 1033 break; 1034 case OMPD_sections: 1035 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc, 1036 EndLoc); 1037 break; 1038 case OMPD_section: 1039 assert(ClausesWithImplicit.empty() && 1040 "No clauses is allowed for 'omp section' directive"); 1041 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc); 1042 break; 1043 case OMPD_single: 1044 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc, 1045 EndLoc); 1046 break; 1047 case OMPD_threadprivate: 1048 case OMPD_task: 1049 llvm_unreachable("OpenMP Directive is not allowed"); 1050 case OMPD_unknown: 1051 llvm_unreachable("Unknown OpenMP directive"); 1052 } 1053 1054 if (ErrorFound) 1055 return StmtError(); 1056 return Res; 1057 } 1058 1059 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, 1060 Stmt *AStmt, 1061 SourceLocation StartLoc, 1062 SourceLocation EndLoc) { 1063 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 1064 CapturedStmt *CS = cast<CapturedStmt>(AStmt); 1065 // 1.2.2 OpenMP Language Terminology 1066 // Structured block - An executable statement with a single entry at the 1067 // top and a single exit at the bottom. 1068 // The point of exit cannot be a branch out of the structured block. 1069 // longjmp() and throw() must not violate the entry/exit criteria. 1070 CS->getCapturedDecl()->setNothrow(); 1071 1072 getCurFunction()->setHasBranchProtectedScope(); 1073 1074 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, 1075 AStmt); 1076 } 1077 1078 namespace { 1079 /// \brief Helper class for checking canonical form of the OpenMP loops and 1080 /// extracting iteration space of each loop in the loop nest, that will be used 1081 /// for IR generation. 1082 class OpenMPIterationSpaceChecker { 1083 /// \brief Reference to Sema. 1084 Sema &SemaRef; 1085 /// \brief A location for diagnostics (when there is no some better location). 1086 SourceLocation DefaultLoc; 1087 /// \brief A location for diagnostics (when increment is not compatible). 1088 SourceLocation ConditionLoc; 1089 /// \brief A source location for referring to condition later. 1090 SourceRange ConditionSrcRange; 1091 /// \brief Loop variable. 1092 VarDecl *Var; 1093 /// \brief Lower bound (initializer for the var). 1094 Expr *LB; 1095 /// \brief Upper bound. 1096 Expr *UB; 1097 /// \brief Loop step (increment). 1098 Expr *Step; 1099 /// \brief This flag is true when condition is one of: 1100 /// Var < UB 1101 /// Var <= UB 1102 /// UB > Var 1103 /// UB >= Var 1104 bool TestIsLessOp; 1105 /// \brief This flag is true when condition is strict ( < or > ). 1106 bool TestIsStrictOp; 1107 /// \brief This flag is true when step is subtracted on each iteration. 1108 bool SubtractStep; 1109 1110 public: 1111 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc) 1112 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc), 1113 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr), 1114 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false), 1115 SubtractStep(false) {} 1116 /// \brief Check init-expr for canonical loop form and save loop counter 1117 /// variable - #Var and its initialization value - #LB. 1118 bool CheckInit(Stmt *S); 1119 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags 1120 /// for less/greater and for strict/non-strict comparison. 1121 bool CheckCond(Expr *S); 1122 /// \brief Check incr-expr for canonical loop form and return true if it 1123 /// does not conform, otherwise save loop step (#Step). 1124 bool CheckInc(Expr *S); 1125 /// \brief Return the loop counter variable. 1126 VarDecl *GetLoopVar() const { return Var; } 1127 /// \brief Return true if any expression is dependent. 1128 bool Dependent() const; 1129 1130 private: 1131 /// \brief Check the right-hand side of an assignment in the increment 1132 /// expression. 1133 bool CheckIncRHS(Expr *RHS); 1134 /// \brief Helper to set loop counter variable and its initializer. 1135 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB); 1136 /// \brief Helper to set upper bound. 1137 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR, 1138 const SourceLocation &SL); 1139 /// \brief Helper to set loop increment. 1140 bool SetStep(Expr *NewStep, bool Subtract); 1141 }; 1142 1143 bool OpenMPIterationSpaceChecker::Dependent() const { 1144 if (!Var) { 1145 assert(!LB && !UB && !Step); 1146 return false; 1147 } 1148 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) || 1149 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent()); 1150 } 1151 1152 bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) { 1153 // State consistency checking to ensure correct usage. 1154 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr && 1155 !TestIsLessOp && !TestIsStrictOp); 1156 if (!NewVar || !NewLB) 1157 return true; 1158 Var = NewVar; 1159 LB = NewLB; 1160 return false; 1161 } 1162 1163 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp, 1164 const SourceRange &SR, 1165 const SourceLocation &SL) { 1166 // State consistency checking to ensure correct usage. 1167 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr && 1168 !TestIsLessOp && !TestIsStrictOp); 1169 if (!NewUB) 1170 return true; 1171 UB = NewUB; 1172 TestIsLessOp = LessOp; 1173 TestIsStrictOp = StrictOp; 1174 ConditionSrcRange = SR; 1175 ConditionLoc = SL; 1176 return false; 1177 } 1178 1179 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) { 1180 // State consistency checking to ensure correct usage. 1181 assert(Var != nullptr && LB != nullptr && Step == nullptr); 1182 if (!NewStep) 1183 return true; 1184 if (!NewStep->isValueDependent()) { 1185 // Check that the step is integer expression. 1186 SourceLocation StepLoc = NewStep->getLocStart(); 1187 ExprResult Val = 1188 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep); 1189 if (Val.isInvalid()) 1190 return true; 1191 NewStep = Val.get(); 1192 1193 // OpenMP [2.6, Canonical Loop Form, Restrictions] 1194 // If test-expr is of form var relational-op b and relational-op is < or 1195 // <= then incr-expr must cause var to increase on each iteration of the 1196 // loop. If test-expr is of form var relational-op b and relational-op is 1197 // > or >= then incr-expr must cause var to decrease on each iteration of 1198 // the loop. 1199 // If test-expr is of form b relational-op var and relational-op is < or 1200 // <= then incr-expr must cause var to decrease on each iteration of the 1201 // loop. If test-expr is of form b relational-op var and relational-op is 1202 // > or >= then incr-expr must cause var to increase on each iteration of 1203 // the loop. 1204 llvm::APSInt Result; 1205 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context); 1206 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation(); 1207 bool IsConstNeg = 1208 IsConstant && Result.isSigned() && (Subtract != Result.isNegative()); 1209 bool IsConstZero = IsConstant && !Result.getBoolValue(); 1210 if (UB && (IsConstZero || 1211 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract)) 1212 : (!IsConstNeg || (IsUnsigned && !Subtract))))) { 1213 SemaRef.Diag(NewStep->getExprLoc(), 1214 diag::err_omp_loop_incr_not_compatible) 1215 << Var << TestIsLessOp << NewStep->getSourceRange(); 1216 SemaRef.Diag(ConditionLoc, 1217 diag::note_omp_loop_cond_requres_compatible_incr) 1218 << TestIsLessOp << ConditionSrcRange; 1219 return true; 1220 } 1221 } 1222 1223 Step = NewStep; 1224 SubtractStep = Subtract; 1225 return false; 1226 } 1227 1228 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) { 1229 // Check init-expr for canonical loop form and save loop counter 1230 // variable - #Var and its initialization value - #LB. 1231 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following: 1232 // var = lb 1233 // integer-type var = lb 1234 // random-access-iterator-type var = lb 1235 // pointer-type var = lb 1236 // 1237 if (!S) { 1238 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init); 1239 return true; 1240 } 1241 if (Expr *E = dyn_cast<Expr>(S)) 1242 S = E->IgnoreParens(); 1243 if (auto BO = dyn_cast<BinaryOperator>(S)) { 1244 if (BO->getOpcode() == BO_Assign) 1245 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens())) 1246 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS()); 1247 } else if (auto DS = dyn_cast<DeclStmt>(S)) { 1248 if (DS->isSingleDecl()) { 1249 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) { 1250 if (Var->hasInit()) { 1251 // Accept non-canonical init form here but emit ext. warning. 1252 if (Var->getInitStyle() != VarDecl::CInit) 1253 SemaRef.Diag(S->getLocStart(), 1254 diag::ext_omp_loop_not_canonical_init) 1255 << S->getSourceRange(); 1256 return SetVarAndLB(Var, Var->getInit()); 1257 } 1258 } 1259 } 1260 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) 1261 if (CE->getOperator() == OO_Equal) 1262 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0))) 1263 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1)); 1264 1265 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init) 1266 << S->getSourceRange(); 1267 return true; 1268 } 1269 1270 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the 1271 /// variable (which may be the loop variable) if possible. 1272 static const VarDecl *GetInitVarDecl(const Expr *E) { 1273 if (!E) 1274 return nullptr; 1275 E = E->IgnoreParenImpCasts(); 1276 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E)) 1277 if (const CXXConstructorDecl *Ctor = CE->getConstructor()) 1278 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 && 1279 CE->getArg(0) != nullptr) 1280 E = CE->getArg(0)->IgnoreParenImpCasts(); 1281 auto DRE = dyn_cast_or_null<DeclRefExpr>(E); 1282 if (!DRE) 1283 return nullptr; 1284 return dyn_cast<VarDecl>(DRE->getDecl()); 1285 } 1286 1287 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) { 1288 // Check test-expr for canonical form, save upper-bound UB, flags for 1289 // less/greater and for strict/non-strict comparison. 1290 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 1291 // var relational-op b 1292 // b relational-op var 1293 // 1294 if (!S) { 1295 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var; 1296 return true; 1297 } 1298 S = S->IgnoreParenImpCasts(); 1299 SourceLocation CondLoc = S->getLocStart(); 1300 if (auto BO = dyn_cast<BinaryOperator>(S)) { 1301 if (BO->isRelationalOp()) { 1302 if (GetInitVarDecl(BO->getLHS()) == Var) 1303 return SetUB(BO->getRHS(), 1304 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE), 1305 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 1306 BO->getSourceRange(), BO->getOperatorLoc()); 1307 if (GetInitVarDecl(BO->getRHS()) == Var) 1308 return SetUB(BO->getLHS(), 1309 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE), 1310 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT), 1311 BO->getSourceRange(), BO->getOperatorLoc()); 1312 } 1313 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 1314 if (CE->getNumArgs() == 2) { 1315 auto Op = CE->getOperator(); 1316 switch (Op) { 1317 case OO_Greater: 1318 case OO_GreaterEqual: 1319 case OO_Less: 1320 case OO_LessEqual: 1321 if (GetInitVarDecl(CE->getArg(0)) == Var) 1322 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual, 1323 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 1324 CE->getOperatorLoc()); 1325 if (GetInitVarDecl(CE->getArg(1)) == Var) 1326 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual, 1327 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(), 1328 CE->getOperatorLoc()); 1329 break; 1330 default: 1331 break; 1332 } 1333 } 1334 } 1335 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond) 1336 << S->getSourceRange() << Var; 1337 return true; 1338 } 1339 1340 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) { 1341 // RHS of canonical loop form increment can be: 1342 // var + incr 1343 // incr + var 1344 // var - incr 1345 // 1346 RHS = RHS->IgnoreParenImpCasts(); 1347 if (auto BO = dyn_cast<BinaryOperator>(RHS)) { 1348 if (BO->isAdditiveOp()) { 1349 bool IsAdd = BO->getOpcode() == BO_Add; 1350 if (GetInitVarDecl(BO->getLHS()) == Var) 1351 return SetStep(BO->getRHS(), !IsAdd); 1352 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var) 1353 return SetStep(BO->getLHS(), false); 1354 } 1355 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) { 1356 bool IsAdd = CE->getOperator() == OO_Plus; 1357 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) { 1358 if (GetInitVarDecl(CE->getArg(0)) == Var) 1359 return SetStep(CE->getArg(1), !IsAdd); 1360 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var) 1361 return SetStep(CE->getArg(0), false); 1362 } 1363 } 1364 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr) 1365 << RHS->getSourceRange() << Var; 1366 return true; 1367 } 1368 1369 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) { 1370 // Check incr-expr for canonical loop form and return true if it 1371 // does not conform. 1372 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following: 1373 // ++var 1374 // var++ 1375 // --var 1376 // var-- 1377 // var += incr 1378 // var -= incr 1379 // var = var + incr 1380 // var = incr + var 1381 // var = var - incr 1382 // 1383 if (!S) { 1384 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var; 1385 return true; 1386 } 1387 S = S->IgnoreParens(); 1388 if (auto UO = dyn_cast<UnaryOperator>(S)) { 1389 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var) 1390 return SetStep( 1391 SemaRef.ActOnIntegerConstant(UO->getLocStart(), 1392 (UO->isDecrementOp() ? -1 : 1)).get(), 1393 false); 1394 } else if (auto BO = dyn_cast<BinaryOperator>(S)) { 1395 switch (BO->getOpcode()) { 1396 case BO_AddAssign: 1397 case BO_SubAssign: 1398 if (GetInitVarDecl(BO->getLHS()) == Var) 1399 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign); 1400 break; 1401 case BO_Assign: 1402 if (GetInitVarDecl(BO->getLHS()) == Var) 1403 return CheckIncRHS(BO->getRHS()); 1404 break; 1405 default: 1406 break; 1407 } 1408 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) { 1409 switch (CE->getOperator()) { 1410 case OO_PlusPlus: 1411 case OO_MinusMinus: 1412 if (GetInitVarDecl(CE->getArg(0)) == Var) 1413 return SetStep( 1414 SemaRef.ActOnIntegerConstant( 1415 CE->getLocStart(), 1416 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(), 1417 false); 1418 break; 1419 case OO_PlusEqual: 1420 case OO_MinusEqual: 1421 if (GetInitVarDecl(CE->getArg(0)) == Var) 1422 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual); 1423 break; 1424 case OO_Equal: 1425 if (GetInitVarDecl(CE->getArg(0)) == Var) 1426 return CheckIncRHS(CE->getArg(1)); 1427 break; 1428 default: 1429 break; 1430 } 1431 } 1432 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr) 1433 << S->getSourceRange() << Var; 1434 return true; 1435 } 1436 } // namespace 1437 1438 /// \brief Called on a for stmt to check and extract its iteration space 1439 /// for further processing (such as collapsing). 1440 static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S, 1441 Sema &SemaRef, DSAStackTy &DSA, 1442 unsigned CurrentNestedLoopCount, 1443 unsigned NestedLoopCount, 1444 Expr *NestedLoopCountExpr) { 1445 // OpenMP [2.6, Canonical Loop Form] 1446 // for (init-expr; test-expr; incr-expr) structured-block 1447 auto For = dyn_cast_or_null<ForStmt>(S); 1448 if (!For) { 1449 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for) 1450 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind) 1451 << NestedLoopCount << (CurrentNestedLoopCount > 0) 1452 << CurrentNestedLoopCount; 1453 if (NestedLoopCount > 1) 1454 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(), 1455 diag::note_omp_collapse_expr) 1456 << NestedLoopCountExpr->getSourceRange(); 1457 return true; 1458 } 1459 assert(For->getBody()); 1460 1461 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc()); 1462 1463 // Check init. 1464 auto Init = For->getInit(); 1465 if (ISC.CheckInit(Init)) { 1466 return true; 1467 } 1468 1469 bool HasErrors = false; 1470 1471 // Check loop variable's type. 1472 auto Var = ISC.GetLoopVar(); 1473 1474 // OpenMP [2.6, Canonical Loop Form] 1475 // Var is one of the following: 1476 // A variable of signed or unsigned integer type. 1477 // For C++, a variable of a random access iterator type. 1478 // For C, a variable of a pointer type. 1479 auto VarType = Var->getType(); 1480 if (!VarType->isDependentType() && !VarType->isIntegerType() && 1481 !VarType->isPointerType() && 1482 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) { 1483 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type) 1484 << SemaRef.getLangOpts().CPlusPlus; 1485 HasErrors = true; 1486 } 1487 1488 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in 1489 // a Construct, C/C++]. 1490 // The loop iteration variable in the associated for-loop of a simd construct 1491 // with just one associated for-loop may be listed in a linear clause with a 1492 // constant-linear-step that is the increment of the associated for-loop. 1493 // The loop iteration variable(s) in the associated for-loop(s) of a for or 1494 // parallel for construct may be listed in a private or lastprivate clause. 1495 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var); 1496 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown && 1497 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) || 1498 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown && 1499 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) && 1500 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 1501 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa) 1502 << getOpenMPClauseName(DVar.CKind); 1503 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true); 1504 HasErrors = true; 1505 } else { 1506 // Make the loop iteration variable private by default. 1507 DSA.addDSA(Var, nullptr, OMPC_private); 1508 } 1509 1510 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars"); 1511 1512 // Check test-expr. 1513 HasErrors |= ISC.CheckCond(For->getCond()); 1514 1515 // Check incr-expr. 1516 HasErrors |= ISC.CheckInc(For->getInc()); 1517 1518 if (ISC.Dependent()) 1519 return HasErrors; 1520 1521 // FIXME: Build loop's iteration space representation. 1522 return HasErrors; 1523 } 1524 1525 /// \brief A helper routine to skip no-op (attributed, compound) stmts get the 1526 /// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt 1527 /// to get the first for loop. 1528 static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) { 1529 if (IgnoreCaptured) 1530 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S)) 1531 S = CapS->getCapturedStmt(); 1532 // OpenMP [2.8.1, simd construct, Restrictions] 1533 // All loops associated with the construct must be perfectly nested; that is, 1534 // there must be no intervening code nor any OpenMP directive between any two 1535 // loops. 1536 while (true) { 1537 if (auto AS = dyn_cast_or_null<AttributedStmt>(S)) 1538 S = AS->getSubStmt(); 1539 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) { 1540 if (CS->size() != 1) 1541 break; 1542 S = CS->body_back(); 1543 } else 1544 break; 1545 } 1546 return S; 1547 } 1548 1549 /// \brief Called on a for stmt to check itself and nested loops (if any). 1550 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop, 1551 /// number of collapsed loops otherwise. 1552 static unsigned CheckOpenMPLoop(OpenMPDirectiveKind DKind, 1553 Expr *NestedLoopCountExpr, Stmt *AStmt, 1554 Sema &SemaRef, DSAStackTy &DSA) { 1555 unsigned NestedLoopCount = 1; 1556 if (NestedLoopCountExpr) { 1557 // Found 'collapse' clause - calculate collapse number. 1558 llvm::APSInt Result; 1559 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) 1560 NestedLoopCount = Result.getLimitedValue(); 1561 } 1562 // This is helper routine for loop directives (e.g., 'for', 'simd', 1563 // 'for simd', etc.). 1564 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true); 1565 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) { 1566 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt, 1567 NestedLoopCount, NestedLoopCountExpr)) 1568 return 0; 1569 // Move on to the next nested for loop, or to the loop body. 1570 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false); 1571 } 1572 1573 // FIXME: Build resulting iteration space for IR generation (collapsing 1574 // iteration spaces when loop count > 1 ('collapse' clause)). 1575 return NestedLoopCount; 1576 } 1577 1578 static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) { 1579 auto CollapseFilter = [](const OMPClause *C) -> bool { 1580 return C->getClauseKind() == OMPC_collapse; 1581 }; 1582 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I( 1583 Clauses, CollapseFilter); 1584 if (I) 1585 return cast<OMPCollapseClause>(*I)->getNumForLoops(); 1586 return nullptr; 1587 } 1588 1589 StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, 1590 Stmt *AStmt, SourceLocation StartLoc, 1591 SourceLocation EndLoc) { 1592 // In presence of clause 'collapse', it will define the nested loops number. 1593 unsigned NestedLoopCount = CheckOpenMPLoop( 1594 OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack); 1595 if (NestedLoopCount == 0) 1596 return StmtError(); 1597 1598 getCurFunction()->setHasBranchProtectedScope(); 1599 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 1600 Clauses, AStmt); 1601 } 1602 1603 StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, 1604 Stmt *AStmt, SourceLocation StartLoc, 1605 SourceLocation EndLoc) { 1606 // In presence of clause 'collapse', it will define the nested loops number. 1607 unsigned NestedLoopCount = CheckOpenMPLoop( 1608 OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack); 1609 if (NestedLoopCount == 0) 1610 return StmtError(); 1611 1612 getCurFunction()->setHasBranchProtectedScope(); 1613 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount, 1614 Clauses, AStmt); 1615 } 1616 1617 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses, 1618 Stmt *AStmt, 1619 SourceLocation StartLoc, 1620 SourceLocation EndLoc) { 1621 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 1622 auto BaseStmt = AStmt; 1623 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt)) 1624 BaseStmt = CS->getCapturedStmt(); 1625 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) { 1626 auto S = C->children(); 1627 if (!S) 1628 return StmtError(); 1629 // All associated statements must be '#pragma omp section' except for 1630 // the first one. 1631 for (++S; S; ++S) { 1632 auto SectionStmt = *S; 1633 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) { 1634 if (SectionStmt) 1635 Diag(SectionStmt->getLocStart(), 1636 diag::err_omp_sections_substmt_not_section); 1637 return StmtError(); 1638 } 1639 } 1640 } else { 1641 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt); 1642 return StmtError(); 1643 } 1644 1645 getCurFunction()->setHasBranchProtectedScope(); 1646 1647 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, 1648 AStmt); 1649 } 1650 1651 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt, 1652 SourceLocation StartLoc, 1653 SourceLocation EndLoc) { 1654 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); 1655 1656 getCurFunction()->setHasBranchProtectedScope(); 1657 1658 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt); 1659 } 1660 1661 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses, 1662 Stmt *AStmt, 1663 SourceLocation StartLoc, 1664 SourceLocation EndLoc) { 1665 getCurFunction()->setHasBranchProtectedScope(); 1666 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt); 1667 } 1668 1669 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, 1670 SourceLocation StartLoc, 1671 SourceLocation LParenLoc, 1672 SourceLocation EndLoc) { 1673 OMPClause *Res = nullptr; 1674 switch (Kind) { 1675 case OMPC_if: 1676 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc); 1677 break; 1678 case OMPC_num_threads: 1679 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); 1680 break; 1681 case OMPC_safelen: 1682 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); 1683 break; 1684 case OMPC_collapse: 1685 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); 1686 break; 1687 case OMPC_default: 1688 case OMPC_proc_bind: 1689 case OMPC_schedule: 1690 case OMPC_private: 1691 case OMPC_firstprivate: 1692 case OMPC_lastprivate: 1693 case OMPC_shared: 1694 case OMPC_reduction: 1695 case OMPC_linear: 1696 case OMPC_aligned: 1697 case OMPC_copyin: 1698 case OMPC_ordered: 1699 case OMPC_nowait: 1700 case OMPC_threadprivate: 1701 case OMPC_unknown: 1702 llvm_unreachable("Clause is not allowed."); 1703 } 1704 return Res; 1705 } 1706 1707 OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc, 1708 SourceLocation LParenLoc, 1709 SourceLocation EndLoc) { 1710 Expr *ValExpr = Condition; 1711 if (!Condition->isValueDependent() && !Condition->isTypeDependent() && 1712 !Condition->isInstantiationDependent() && 1713 !Condition->containsUnexpandedParameterPack()) { 1714 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), 1715 Condition->getExprLoc(), Condition); 1716 if (Val.isInvalid()) 1717 return nullptr; 1718 1719 ValExpr = Val.get(); 1720 } 1721 1722 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc); 1723 } 1724 1725 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc, 1726 Expr *Op) { 1727 if (!Op) 1728 return ExprError(); 1729 1730 class IntConvertDiagnoser : public ICEConvertDiagnoser { 1731 public: 1732 IntConvertDiagnoser() 1733 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} 1734 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, 1735 QualType T) override { 1736 return S.Diag(Loc, diag::err_omp_not_integral) << T; 1737 } 1738 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, 1739 QualType T) override { 1740 return S.Diag(Loc, diag::err_omp_incomplete_type) << T; 1741 } 1742 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, 1743 QualType T, 1744 QualType ConvTy) override { 1745 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; 1746 } 1747 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, 1748 QualType ConvTy) override { 1749 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 1750 << ConvTy->isEnumeralType() << ConvTy; 1751 } 1752 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, 1753 QualType T) override { 1754 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; 1755 } 1756 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, 1757 QualType ConvTy) override { 1758 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) 1759 << ConvTy->isEnumeralType() << ConvTy; 1760 } 1761 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, 1762 QualType) override { 1763 llvm_unreachable("conversion functions are permitted"); 1764 } 1765 } ConvertDiagnoser; 1766 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); 1767 } 1768 1769 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, 1770 SourceLocation StartLoc, 1771 SourceLocation LParenLoc, 1772 SourceLocation EndLoc) { 1773 Expr *ValExpr = NumThreads; 1774 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() && 1775 !NumThreads->isInstantiationDependent() && 1776 !NumThreads->containsUnexpandedParameterPack()) { 1777 SourceLocation NumThreadsLoc = NumThreads->getLocStart(); 1778 ExprResult Val = 1779 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads); 1780 if (Val.isInvalid()) 1781 return nullptr; 1782 1783 ValExpr = Val.get(); 1784 1785 // OpenMP [2.5, Restrictions] 1786 // The num_threads expression must evaluate to a positive integer value. 1787 llvm::APSInt Result; 1788 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() && 1789 !Result.isStrictlyPositive()) { 1790 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause) 1791 << "num_threads" << NumThreads->getSourceRange(); 1792 return nullptr; 1793 } 1794 } 1795 1796 return new (Context) 1797 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); 1798 } 1799 1800 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, 1801 OpenMPClauseKind CKind) { 1802 if (!E) 1803 return ExprError(); 1804 if (E->isValueDependent() || E->isTypeDependent() || 1805 E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) 1806 return E; 1807 llvm::APSInt Result; 1808 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); 1809 if (ICE.isInvalid()) 1810 return ExprError(); 1811 if (!Result.isStrictlyPositive()) { 1812 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) 1813 << getOpenMPClauseName(CKind) << E->getSourceRange(); 1814 return ExprError(); 1815 } 1816 return ICE; 1817 } 1818 1819 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, 1820 SourceLocation LParenLoc, 1821 SourceLocation EndLoc) { 1822 // OpenMP [2.8.1, simd construct, Description] 1823 // The parameter of the safelen clause must be a constant 1824 // positive integer expression. 1825 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); 1826 if (Safelen.isInvalid()) 1827 return nullptr; 1828 return new (Context) 1829 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc); 1830 } 1831 1832 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops, 1833 SourceLocation StartLoc, 1834 SourceLocation LParenLoc, 1835 SourceLocation EndLoc) { 1836 // OpenMP [2.7.1, loop construct, Description] 1837 // OpenMP [2.8.1, simd construct, Description] 1838 // OpenMP [2.9.6, distribute construct, Description] 1839 // The parameter of the collapse clause must be a constant 1840 // positive integer expression. 1841 ExprResult NumForLoopsResult = 1842 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse); 1843 if (NumForLoopsResult.isInvalid()) 1844 return nullptr; 1845 return new (Context) 1846 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc); 1847 } 1848 1849 OMPClause *Sema::ActOnOpenMPSimpleClause( 1850 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, 1851 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { 1852 OMPClause *Res = nullptr; 1853 switch (Kind) { 1854 case OMPC_default: 1855 Res = 1856 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), 1857 ArgumentLoc, StartLoc, LParenLoc, EndLoc); 1858 break; 1859 case OMPC_proc_bind: 1860 Res = ActOnOpenMPProcBindClause( 1861 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, 1862 LParenLoc, EndLoc); 1863 break; 1864 case OMPC_if: 1865 case OMPC_num_threads: 1866 case OMPC_safelen: 1867 case OMPC_collapse: 1868 case OMPC_schedule: 1869 case OMPC_private: 1870 case OMPC_firstprivate: 1871 case OMPC_lastprivate: 1872 case OMPC_shared: 1873 case OMPC_reduction: 1874 case OMPC_linear: 1875 case OMPC_aligned: 1876 case OMPC_copyin: 1877 case OMPC_ordered: 1878 case OMPC_nowait: 1879 case OMPC_threadprivate: 1880 case OMPC_unknown: 1881 llvm_unreachable("Clause is not allowed."); 1882 } 1883 return Res; 1884 } 1885 1886 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, 1887 SourceLocation KindKwLoc, 1888 SourceLocation StartLoc, 1889 SourceLocation LParenLoc, 1890 SourceLocation EndLoc) { 1891 if (Kind == OMPC_DEFAULT_unknown) { 1892 std::string Values; 1893 static_assert(OMPC_DEFAULT_unknown > 0, 1894 "OMPC_DEFAULT_unknown not greater than 0"); 1895 std::string Sep(", "); 1896 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) { 1897 Values += "'"; 1898 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); 1899 Values += "'"; 1900 switch (i) { 1901 case OMPC_DEFAULT_unknown - 2: 1902 Values += " or "; 1903 break; 1904 case OMPC_DEFAULT_unknown - 1: 1905 break; 1906 default: 1907 Values += Sep; 1908 break; 1909 } 1910 } 1911 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 1912 << Values << getOpenMPClauseName(OMPC_default); 1913 return nullptr; 1914 } 1915 switch (Kind) { 1916 case OMPC_DEFAULT_none: 1917 DSAStack->setDefaultDSANone(); 1918 break; 1919 case OMPC_DEFAULT_shared: 1920 DSAStack->setDefaultDSAShared(); 1921 break; 1922 case OMPC_DEFAULT_unknown: 1923 llvm_unreachable("Clause kind is not allowed."); 1924 break; 1925 } 1926 return new (Context) 1927 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 1928 } 1929 1930 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, 1931 SourceLocation KindKwLoc, 1932 SourceLocation StartLoc, 1933 SourceLocation LParenLoc, 1934 SourceLocation EndLoc) { 1935 if (Kind == OMPC_PROC_BIND_unknown) { 1936 std::string Values; 1937 std::string Sep(", "); 1938 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) { 1939 Values += "'"; 1940 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i); 1941 Values += "'"; 1942 switch (i) { 1943 case OMPC_PROC_BIND_unknown - 2: 1944 Values += " or "; 1945 break; 1946 case OMPC_PROC_BIND_unknown - 1: 1947 break; 1948 default: 1949 Values += Sep; 1950 break; 1951 } 1952 } 1953 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) 1954 << Values << getOpenMPClauseName(OMPC_proc_bind); 1955 return nullptr; 1956 } 1957 return new (Context) 1958 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); 1959 } 1960 1961 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause( 1962 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr, 1963 SourceLocation StartLoc, SourceLocation LParenLoc, 1964 SourceLocation ArgumentLoc, SourceLocation CommaLoc, 1965 SourceLocation EndLoc) { 1966 OMPClause *Res = nullptr; 1967 switch (Kind) { 1968 case OMPC_schedule: 1969 Res = ActOnOpenMPScheduleClause( 1970 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc, 1971 LParenLoc, ArgumentLoc, CommaLoc, EndLoc); 1972 break; 1973 case OMPC_if: 1974 case OMPC_num_threads: 1975 case OMPC_safelen: 1976 case OMPC_collapse: 1977 case OMPC_default: 1978 case OMPC_proc_bind: 1979 case OMPC_private: 1980 case OMPC_firstprivate: 1981 case OMPC_lastprivate: 1982 case OMPC_shared: 1983 case OMPC_reduction: 1984 case OMPC_linear: 1985 case OMPC_aligned: 1986 case OMPC_copyin: 1987 case OMPC_ordered: 1988 case OMPC_nowait: 1989 case OMPC_threadprivate: 1990 case OMPC_unknown: 1991 llvm_unreachable("Clause is not allowed."); 1992 } 1993 return Res; 1994 } 1995 1996 OMPClause *Sema::ActOnOpenMPScheduleClause( 1997 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc, 1998 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc, 1999 SourceLocation EndLoc) { 2000 if (Kind == OMPC_SCHEDULE_unknown) { 2001 std::string Values; 2002 std::string Sep(", "); 2003 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) { 2004 Values += "'"; 2005 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i); 2006 Values += "'"; 2007 switch (i) { 2008 case OMPC_SCHEDULE_unknown - 2: 2009 Values += " or "; 2010 break; 2011 case OMPC_SCHEDULE_unknown - 1: 2012 break; 2013 default: 2014 Values += Sep; 2015 break; 2016 } 2017 } 2018 Diag(KindLoc, diag::err_omp_unexpected_clause_value) 2019 << Values << getOpenMPClauseName(OMPC_schedule); 2020 return nullptr; 2021 } 2022 Expr *ValExpr = ChunkSize; 2023 if (ChunkSize) { 2024 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() && 2025 !ChunkSize->isInstantiationDependent() && 2026 !ChunkSize->containsUnexpandedParameterPack()) { 2027 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart(); 2028 ExprResult Val = 2029 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize); 2030 if (Val.isInvalid()) 2031 return nullptr; 2032 2033 ValExpr = Val.get(); 2034 2035 // OpenMP [2.7.1, Restrictions] 2036 // chunk_size must be a loop invariant integer expression with a positive 2037 // value. 2038 llvm::APSInt Result; 2039 if (ValExpr->isIntegerConstantExpr(Result, Context) && 2040 Result.isSigned() && !Result.isStrictlyPositive()) { 2041 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause) 2042 << "schedule" << ChunkSize->getSourceRange(); 2043 return nullptr; 2044 } 2045 } 2046 } 2047 2048 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, 2049 EndLoc, Kind, ValExpr); 2050 } 2051 2052 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind, 2053 SourceLocation StartLoc, 2054 SourceLocation EndLoc) { 2055 OMPClause *Res = nullptr; 2056 switch (Kind) { 2057 case OMPC_ordered: 2058 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc); 2059 break; 2060 case OMPC_nowait: 2061 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc); 2062 break; 2063 case OMPC_if: 2064 case OMPC_num_threads: 2065 case OMPC_safelen: 2066 case OMPC_collapse: 2067 case OMPC_schedule: 2068 case OMPC_private: 2069 case OMPC_firstprivate: 2070 case OMPC_lastprivate: 2071 case OMPC_shared: 2072 case OMPC_reduction: 2073 case OMPC_linear: 2074 case OMPC_aligned: 2075 case OMPC_copyin: 2076 case OMPC_default: 2077 case OMPC_proc_bind: 2078 case OMPC_threadprivate: 2079 case OMPC_unknown: 2080 llvm_unreachable("Clause is not allowed."); 2081 } 2082 return Res; 2083 } 2084 2085 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc, 2086 SourceLocation EndLoc) { 2087 return new (Context) OMPOrderedClause(StartLoc, EndLoc); 2088 } 2089 2090 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc, 2091 SourceLocation EndLoc) { 2092 return new (Context) OMPNowaitClause(StartLoc, EndLoc); 2093 } 2094 2095 OMPClause *Sema::ActOnOpenMPVarListClause( 2096 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr, 2097 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc, 2098 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec, 2099 const DeclarationNameInfo &ReductionId) { 2100 OMPClause *Res = nullptr; 2101 switch (Kind) { 2102 case OMPC_private: 2103 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); 2104 break; 2105 case OMPC_firstprivate: 2106 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 2107 break; 2108 case OMPC_lastprivate: 2109 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc); 2110 break; 2111 case OMPC_shared: 2112 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); 2113 break; 2114 case OMPC_reduction: 2115 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc, 2116 EndLoc, ReductionIdScopeSpec, ReductionId); 2117 break; 2118 case OMPC_linear: 2119 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, 2120 ColonLoc, EndLoc); 2121 break; 2122 case OMPC_aligned: 2123 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc, 2124 ColonLoc, EndLoc); 2125 break; 2126 case OMPC_copyin: 2127 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); 2128 break; 2129 case OMPC_if: 2130 case OMPC_num_threads: 2131 case OMPC_safelen: 2132 case OMPC_collapse: 2133 case OMPC_default: 2134 case OMPC_proc_bind: 2135 case OMPC_schedule: 2136 case OMPC_ordered: 2137 case OMPC_nowait: 2138 case OMPC_threadprivate: 2139 case OMPC_unknown: 2140 llvm_unreachable("Clause is not allowed."); 2141 } 2142 return Res; 2143 } 2144 2145 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, 2146 SourceLocation StartLoc, 2147 SourceLocation LParenLoc, 2148 SourceLocation EndLoc) { 2149 SmallVector<Expr *, 8> Vars; 2150 for (auto &RefExpr : VarList) { 2151 assert(RefExpr && "NULL expr in OpenMP private clause."); 2152 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 2153 // It will be analyzed later. 2154 Vars.push_back(RefExpr); 2155 continue; 2156 } 2157 2158 SourceLocation ELoc = RefExpr->getExprLoc(); 2159 // OpenMP [2.1, C/C++] 2160 // A list item is a variable name. 2161 // OpenMP [2.9.3.3, Restrictions, p.1] 2162 // A variable that is part of another variable (as an array or 2163 // structure element) cannot appear in a private clause. 2164 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 2165 if (!DE || !isa<VarDecl>(DE->getDecl())) { 2166 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 2167 continue; 2168 } 2169 Decl *D = DE->getDecl(); 2170 VarDecl *VD = cast<VarDecl>(D); 2171 2172 QualType Type = VD->getType(); 2173 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 2174 // It will be analyzed later. 2175 Vars.push_back(DE); 2176 continue; 2177 } 2178 2179 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 2180 // A variable that appears in a private clause must not have an incomplete 2181 // type or a reference type. 2182 if (RequireCompleteType(ELoc, Type, 2183 diag::err_omp_private_incomplete_type)) { 2184 continue; 2185 } 2186 if (Type->isReferenceType()) { 2187 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 2188 << getOpenMPClauseName(OMPC_private) << Type; 2189 bool IsDecl = 2190 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2191 Diag(VD->getLocation(), 2192 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2193 << VD; 2194 continue; 2195 } 2196 2197 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] 2198 // A variable of class type (or array thereof) that appears in a private 2199 // clause requires an accessible, unambiguous default constructor for the 2200 // class type. 2201 while (Type.getNonReferenceType()->isArrayType()) { 2202 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr()) 2203 ->getElementType(); 2204 } 2205 CXXRecordDecl *RD = getLangOpts().CPlusPlus 2206 ? Type.getNonReferenceType()->getAsCXXRecordDecl() 2207 : nullptr; 2208 // FIXME This code must be replaced by actual constructing/destructing of 2209 // the private variable. 2210 if (RD) { 2211 CXXConstructorDecl *CD = LookupDefaultConstructor(RD); 2212 PartialDiagnostic PD = 2213 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 2214 if (!CD || 2215 CheckConstructorAccess(ELoc, CD, 2216 InitializedEntity::InitializeTemporary(Type), 2217 CD->getAccess(), PD) == AR_inaccessible || 2218 CD->isDeleted()) { 2219 Diag(ELoc, diag::err_omp_required_method) 2220 << getOpenMPClauseName(OMPC_private) << 0; 2221 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2222 VarDecl::DeclarationOnly; 2223 Diag(VD->getLocation(), 2224 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2225 << VD; 2226 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 2227 continue; 2228 } 2229 MarkFunctionReferenced(ELoc, CD); 2230 DiagnoseUseOfDecl(CD, ELoc); 2231 2232 CXXDestructorDecl *DD = RD->getDestructor(); 2233 if (DD) { 2234 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 2235 DD->isDeleted()) { 2236 Diag(ELoc, diag::err_omp_required_method) 2237 << getOpenMPClauseName(OMPC_private) << 4; 2238 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2239 VarDecl::DeclarationOnly; 2240 Diag(VD->getLocation(), 2241 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2242 << VD; 2243 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 2244 continue; 2245 } 2246 MarkFunctionReferenced(ELoc, DD); 2247 DiagnoseUseOfDecl(DD, ELoc); 2248 } 2249 } 2250 2251 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 2252 // in a Construct] 2253 // Variables with the predetermined data-sharing attributes may not be 2254 // listed in data-sharing attributes clauses, except for the cases 2255 // listed below. For these exceptions only, listing a predetermined 2256 // variable in a data-sharing attribute clause is allowed and overrides 2257 // the variable's predetermined data-sharing attributes. 2258 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 2259 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { 2260 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 2261 << getOpenMPClauseName(OMPC_private); 2262 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2263 continue; 2264 } 2265 2266 DSAStack->addDSA(VD, DE, OMPC_private); 2267 Vars.push_back(DE); 2268 } 2269 2270 if (Vars.empty()) 2271 return nullptr; 2272 2273 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 2274 } 2275 2276 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, 2277 SourceLocation StartLoc, 2278 SourceLocation LParenLoc, 2279 SourceLocation EndLoc) { 2280 SmallVector<Expr *, 8> Vars; 2281 for (auto &RefExpr : VarList) { 2282 assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); 2283 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 2284 // It will be analyzed later. 2285 Vars.push_back(RefExpr); 2286 continue; 2287 } 2288 2289 SourceLocation ELoc = RefExpr->getExprLoc(); 2290 // OpenMP [2.1, C/C++] 2291 // A list item is a variable name. 2292 // OpenMP [2.9.3.3, Restrictions, p.1] 2293 // A variable that is part of another variable (as an array or 2294 // structure element) cannot appear in a private clause. 2295 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 2296 if (!DE || !isa<VarDecl>(DE->getDecl())) { 2297 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 2298 continue; 2299 } 2300 Decl *D = DE->getDecl(); 2301 VarDecl *VD = cast<VarDecl>(D); 2302 2303 QualType Type = VD->getType(); 2304 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 2305 // It will be analyzed later. 2306 Vars.push_back(DE); 2307 continue; 2308 } 2309 2310 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 2311 // A variable that appears in a private clause must not have an incomplete 2312 // type or a reference type. 2313 if (RequireCompleteType(ELoc, Type, 2314 diag::err_omp_firstprivate_incomplete_type)) { 2315 continue; 2316 } 2317 if (Type->isReferenceType()) { 2318 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 2319 << getOpenMPClauseName(OMPC_firstprivate) << Type; 2320 bool IsDecl = 2321 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2322 Diag(VD->getLocation(), 2323 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2324 << VD; 2325 continue; 2326 } 2327 2328 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] 2329 // A variable of class type (or array thereof) that appears in a private 2330 // clause requires an accessible, unambiguous copy constructor for the 2331 // class type. 2332 Type = Context.getBaseElementType(Type); 2333 CXXRecordDecl *RD = getLangOpts().CPlusPlus 2334 ? Type.getNonReferenceType()->getAsCXXRecordDecl() 2335 : nullptr; 2336 // FIXME This code must be replaced by actual constructing/destructing of 2337 // the firstprivate variable. 2338 if (RD) { 2339 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0); 2340 PartialDiagnostic PD = 2341 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 2342 if (!CD || 2343 CheckConstructorAccess(ELoc, CD, 2344 InitializedEntity::InitializeTemporary(Type), 2345 CD->getAccess(), PD) == AR_inaccessible || 2346 CD->isDeleted()) { 2347 Diag(ELoc, diag::err_omp_required_method) 2348 << getOpenMPClauseName(OMPC_firstprivate) << 1; 2349 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2350 VarDecl::DeclarationOnly; 2351 Diag(VD->getLocation(), 2352 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2353 << VD; 2354 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 2355 continue; 2356 } 2357 MarkFunctionReferenced(ELoc, CD); 2358 DiagnoseUseOfDecl(CD, ELoc); 2359 2360 CXXDestructorDecl *DD = RD->getDestructor(); 2361 if (DD) { 2362 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 2363 DD->isDeleted()) { 2364 Diag(ELoc, diag::err_omp_required_method) 2365 << getOpenMPClauseName(OMPC_firstprivate) << 4; 2366 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2367 VarDecl::DeclarationOnly; 2368 Diag(VD->getLocation(), 2369 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2370 << VD; 2371 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 2372 continue; 2373 } 2374 MarkFunctionReferenced(ELoc, DD); 2375 DiagnoseUseOfDecl(DD, ELoc); 2376 } 2377 } 2378 2379 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate 2380 // variable and it was checked already. 2381 if (StartLoc.isValid() && EndLoc.isValid()) { 2382 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 2383 Type = Type.getNonReferenceType().getCanonicalType(); 2384 bool IsConstant = Type.isConstant(Context); 2385 Type = Context.getBaseElementType(Type); 2386 // OpenMP [2.4.13, Data-sharing Attribute Clauses] 2387 // A list item that specifies a given variable may not appear in more 2388 // than one clause on the same directive, except that a variable may be 2389 // specified in both firstprivate and lastprivate clauses. 2390 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && 2391 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) { 2392 Diag(ELoc, diag::err_omp_wrong_dsa) 2393 << getOpenMPClauseName(DVar.CKind) 2394 << getOpenMPClauseName(OMPC_firstprivate); 2395 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2396 continue; 2397 } 2398 2399 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 2400 // in a Construct] 2401 // Variables with the predetermined data-sharing attributes may not be 2402 // listed in data-sharing attributes clauses, except for the cases 2403 // listed below. For these exceptions only, listing a predetermined 2404 // variable in a data-sharing attribute clause is allowed and overrides 2405 // the variable's predetermined data-sharing attributes. 2406 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 2407 // in a Construct, C/C++, p.2] 2408 // Variables with const-qualified type having no mutable member may be 2409 // listed in a firstprivate clause, even if they are static data members. 2410 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && 2411 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { 2412 Diag(ELoc, diag::err_omp_wrong_dsa) 2413 << getOpenMPClauseName(DVar.CKind) 2414 << getOpenMPClauseName(OMPC_firstprivate); 2415 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2416 continue; 2417 } 2418 2419 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 2420 // OpenMP [2.9.3.4, Restrictions, p.2] 2421 // A list item that is private within a parallel region must not appear 2422 // in a firstprivate clause on a worksharing construct if any of the 2423 // worksharing regions arising from the worksharing construct ever bind 2424 // to any of the parallel regions arising from the parallel construct. 2425 if (isOpenMPWorksharingDirective(CurrDir) && 2426 !isOpenMPParallelDirective(CurrDir)) { 2427 DVar = DSAStack->getImplicitDSA(VD); 2428 if (DVar.CKind != OMPC_shared) { 2429 Diag(ELoc, diag::err_omp_required_access) 2430 << getOpenMPClauseName(OMPC_firstprivate) 2431 << getOpenMPClauseName(OMPC_shared); 2432 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2433 continue; 2434 } 2435 } 2436 // OpenMP [2.9.3.4, Restrictions, p.3] 2437 // A list item that appears in a reduction clause of a parallel construct 2438 // must not appear in a firstprivate clause on a worksharing or task 2439 // construct if any of the worksharing or task regions arising from the 2440 // worksharing or task construct ever bind to any of the parallel regions 2441 // arising from the parallel construct. 2442 // TODO 2443 // OpenMP [2.9.3.4, Restrictions, p.4] 2444 // A list item that appears in a reduction clause in worksharing 2445 // construct must not appear in a firstprivate clause in a task construct 2446 // encountered during execution of any of the worksharing regions arising 2447 // from the worksharing construct. 2448 // TODO 2449 } 2450 2451 DSAStack->addDSA(VD, DE, OMPC_firstprivate); 2452 Vars.push_back(DE); 2453 } 2454 2455 if (Vars.empty()) 2456 return nullptr; 2457 2458 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 2459 Vars); 2460 } 2461 2462 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList, 2463 SourceLocation StartLoc, 2464 SourceLocation LParenLoc, 2465 SourceLocation EndLoc) { 2466 SmallVector<Expr *, 8> Vars; 2467 for (auto &RefExpr : VarList) { 2468 assert(RefExpr && "NULL expr in OpenMP lastprivate clause."); 2469 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 2470 // It will be analyzed later. 2471 Vars.push_back(RefExpr); 2472 continue; 2473 } 2474 2475 SourceLocation ELoc = RefExpr->getExprLoc(); 2476 // OpenMP [2.1, C/C++] 2477 // A list item is a variable name. 2478 // OpenMP [2.14.3.5, Restrictions, p.1] 2479 // A variable that is part of another variable (as an array or structure 2480 // element) cannot appear in a lastprivate clause. 2481 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); 2482 if (!DE || !isa<VarDecl>(DE->getDecl())) { 2483 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 2484 continue; 2485 } 2486 Decl *D = DE->getDecl(); 2487 VarDecl *VD = cast<VarDecl>(D); 2488 2489 QualType Type = VD->getType(); 2490 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 2491 // It will be analyzed later. 2492 Vars.push_back(DE); 2493 continue; 2494 } 2495 2496 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2] 2497 // A variable that appears in a lastprivate clause must not have an 2498 // incomplete type or a reference type. 2499 if (RequireCompleteType(ELoc, Type, 2500 diag::err_omp_lastprivate_incomplete_type)) { 2501 continue; 2502 } 2503 if (Type->isReferenceType()) { 2504 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 2505 << getOpenMPClauseName(OMPC_lastprivate) << Type; 2506 bool IsDecl = 2507 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2508 Diag(VD->getLocation(), 2509 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2510 << VD; 2511 continue; 2512 } 2513 2514 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 2515 // in a Construct] 2516 // Variables with the predetermined data-sharing attributes may not be 2517 // listed in data-sharing attributes clauses, except for the cases 2518 // listed below. 2519 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 2520 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate && 2521 DVar.CKind != OMPC_firstprivate && 2522 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) { 2523 Diag(ELoc, diag::err_omp_wrong_dsa) 2524 << getOpenMPClauseName(DVar.CKind) 2525 << getOpenMPClauseName(OMPC_lastprivate); 2526 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2527 continue; 2528 } 2529 2530 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 2531 // OpenMP [2.14.3.5, Restrictions, p.2] 2532 // A list item that is private within a parallel region, or that appears in 2533 // the reduction clause of a parallel construct, must not appear in a 2534 // lastprivate clause on a worksharing construct if any of the corresponding 2535 // worksharing regions ever binds to any of the corresponding parallel 2536 // regions. 2537 if (isOpenMPWorksharingDirective(CurrDir) && 2538 !isOpenMPParallelDirective(CurrDir)) { 2539 DVar = DSAStack->getImplicitDSA(VD); 2540 if (DVar.CKind != OMPC_shared) { 2541 Diag(ELoc, diag::err_omp_required_access) 2542 << getOpenMPClauseName(OMPC_lastprivate) 2543 << getOpenMPClauseName(OMPC_shared); 2544 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2545 continue; 2546 } 2547 } 2548 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2] 2549 // A variable of class type (or array thereof) that appears in a 2550 // lastprivate clause requires an accessible, unambiguous default 2551 // constructor for the class type, unless the list item is also specified 2552 // in a firstprivate clause. 2553 // A variable of class type (or array thereof) that appears in a 2554 // lastprivate clause requires an accessible, unambiguous copy assignment 2555 // operator for the class type. 2556 while (Type.getNonReferenceType()->isArrayType()) 2557 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr()) 2558 ->getElementType(); 2559 CXXRecordDecl *RD = getLangOpts().CPlusPlus 2560 ? Type.getNonReferenceType()->getAsCXXRecordDecl() 2561 : nullptr; 2562 // FIXME This code must be replaced by actual copying and destructing of the 2563 // lastprivate variable. 2564 if (RD) { 2565 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0); 2566 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess()); 2567 if (MD) { 2568 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible || 2569 MD->isDeleted()) { 2570 Diag(ELoc, diag::err_omp_required_method) 2571 << getOpenMPClauseName(OMPC_lastprivate) << 2; 2572 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2573 VarDecl::DeclarationOnly; 2574 Diag(VD->getLocation(), 2575 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2576 << VD; 2577 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 2578 continue; 2579 } 2580 MarkFunctionReferenced(ELoc, MD); 2581 DiagnoseUseOfDecl(MD, ELoc); 2582 } 2583 2584 CXXDestructorDecl *DD = RD->getDestructor(); 2585 if (DD) { 2586 PartialDiagnostic PD = 2587 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 2588 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 2589 DD->isDeleted()) { 2590 Diag(ELoc, diag::err_omp_required_method) 2591 << getOpenMPClauseName(OMPC_lastprivate) << 4; 2592 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2593 VarDecl::DeclarationOnly; 2594 Diag(VD->getLocation(), 2595 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2596 << VD; 2597 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 2598 continue; 2599 } 2600 MarkFunctionReferenced(ELoc, DD); 2601 DiagnoseUseOfDecl(DD, ELoc); 2602 } 2603 } 2604 2605 if (DVar.CKind != OMPC_firstprivate) 2606 DSAStack->addDSA(VD, DE, OMPC_lastprivate); 2607 Vars.push_back(DE); 2608 } 2609 2610 if (Vars.empty()) 2611 return nullptr; 2612 2613 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, 2614 Vars); 2615 } 2616 2617 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, 2618 SourceLocation StartLoc, 2619 SourceLocation LParenLoc, 2620 SourceLocation EndLoc) { 2621 SmallVector<Expr *, 8> Vars; 2622 for (auto &RefExpr : VarList) { 2623 assert(RefExpr && "NULL expr in OpenMP shared clause."); 2624 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 2625 // It will be analyzed later. 2626 Vars.push_back(RefExpr); 2627 continue; 2628 } 2629 2630 SourceLocation ELoc = RefExpr->getExprLoc(); 2631 // OpenMP [2.1, C/C++] 2632 // A list item is a variable name. 2633 // OpenMP [2.14.3.2, Restrictions, p.1] 2634 // A variable that is part of another variable (as an array or structure 2635 // element) cannot appear in a shared unless it is a static data member 2636 // of a C++ class. 2637 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 2638 if (!DE || !isa<VarDecl>(DE->getDecl())) { 2639 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 2640 continue; 2641 } 2642 Decl *D = DE->getDecl(); 2643 VarDecl *VD = cast<VarDecl>(D); 2644 2645 QualType Type = VD->getType(); 2646 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 2647 // It will be analyzed later. 2648 Vars.push_back(DE); 2649 continue; 2650 } 2651 2652 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced 2653 // in a Construct] 2654 // Variables with the predetermined data-sharing attributes may not be 2655 // listed in data-sharing attributes clauses, except for the cases 2656 // listed below. For these exceptions only, listing a predetermined 2657 // variable in a data-sharing attribute clause is allowed and overrides 2658 // the variable's predetermined data-sharing attributes. 2659 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 2660 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && 2661 DVar.RefExpr) { 2662 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 2663 << getOpenMPClauseName(OMPC_shared); 2664 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2665 continue; 2666 } 2667 2668 DSAStack->addDSA(VD, DE, OMPC_shared); 2669 Vars.push_back(DE); 2670 } 2671 2672 if (Vars.empty()) 2673 return nullptr; 2674 2675 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 2676 } 2677 2678 namespace { 2679 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> { 2680 DSAStackTy *Stack; 2681 2682 public: 2683 bool VisitDeclRefExpr(DeclRefExpr *E) { 2684 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 2685 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD); 2686 if (DVar.CKind == OMPC_shared && !DVar.RefExpr) 2687 return false; 2688 if (DVar.CKind != OMPC_unknown) 2689 return true; 2690 DSAStackTy::DSAVarData DVarPrivate = 2691 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways()); 2692 if (DVarPrivate.CKind != OMPC_unknown) 2693 return true; 2694 return false; 2695 } 2696 return false; 2697 } 2698 bool VisitStmt(Stmt *S) { 2699 for (auto Child : S->children()) { 2700 if (Child && Visit(Child)) 2701 return true; 2702 } 2703 return false; 2704 } 2705 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {} 2706 }; 2707 } // namespace 2708 2709 OMPClause *Sema::ActOnOpenMPReductionClause( 2710 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc, 2711 SourceLocation ColonLoc, SourceLocation EndLoc, 2712 CXXScopeSpec &ReductionIdScopeSpec, 2713 const DeclarationNameInfo &ReductionId) { 2714 // TODO: Allow scope specification search when 'declare reduction' is 2715 // supported. 2716 assert(ReductionIdScopeSpec.isEmpty() && 2717 "No support for scoped reduction identifiers yet."); 2718 2719 auto DN = ReductionId.getName(); 2720 auto OOK = DN.getCXXOverloadedOperator(); 2721 BinaryOperatorKind BOK = BO_Comma; 2722 2723 // OpenMP [2.14.3.6, reduction clause] 2724 // C 2725 // reduction-identifier is either an identifier or one of the following 2726 // operators: +, -, *, &, |, ^, && and || 2727 // C++ 2728 // reduction-identifier is either an id-expression or one of the following 2729 // operators: +, -, *, &, |, ^, && and || 2730 // FIXME: Only 'min' and 'max' identifiers are supported for now. 2731 switch (OOK) { 2732 case OO_Plus: 2733 case OO_Minus: 2734 BOK = BO_AddAssign; 2735 break; 2736 case OO_Star: 2737 BOK = BO_MulAssign; 2738 break; 2739 case OO_Amp: 2740 BOK = BO_AndAssign; 2741 break; 2742 case OO_Pipe: 2743 BOK = BO_OrAssign; 2744 break; 2745 case OO_Caret: 2746 BOK = BO_XorAssign; 2747 break; 2748 case OO_AmpAmp: 2749 BOK = BO_LAnd; 2750 break; 2751 case OO_PipePipe: 2752 BOK = BO_LOr; 2753 break; 2754 default: 2755 if (auto II = DN.getAsIdentifierInfo()) { 2756 if (II->isStr("max")) 2757 BOK = BO_GT; 2758 else if (II->isStr("min")) 2759 BOK = BO_LT; 2760 } 2761 break; 2762 } 2763 SourceRange ReductionIdRange; 2764 if (ReductionIdScopeSpec.isValid()) { 2765 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc()); 2766 } 2767 ReductionIdRange.setEnd(ReductionId.getEndLoc()); 2768 if (BOK == BO_Comma) { 2769 // Not allowed reduction identifier is found. 2770 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier) 2771 << ReductionIdRange; 2772 return nullptr; 2773 } 2774 2775 SmallVector<Expr *, 8> Vars; 2776 for (auto RefExpr : VarList) { 2777 assert(RefExpr && "nullptr expr in OpenMP reduction clause."); 2778 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 2779 // It will be analyzed later. 2780 Vars.push_back(RefExpr); 2781 continue; 2782 } 2783 2784 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() || 2785 RefExpr->isInstantiationDependent() || 2786 RefExpr->containsUnexpandedParameterPack()) { 2787 // It will be analyzed later. 2788 Vars.push_back(RefExpr); 2789 continue; 2790 } 2791 2792 auto ELoc = RefExpr->getExprLoc(); 2793 auto ERange = RefExpr->getSourceRange(); 2794 // OpenMP [2.1, C/C++] 2795 // A list item is a variable or array section, subject to the restrictions 2796 // specified in Section 2.4 on page 42 and in each of the sections 2797 // describing clauses and directives for which a list appears. 2798 // OpenMP [2.14.3.3, Restrictions, p.1] 2799 // A variable that is part of another variable (as an array or 2800 // structure element) cannot appear in a private clause. 2801 auto DE = dyn_cast<DeclRefExpr>(RefExpr); 2802 if (!DE || !isa<VarDecl>(DE->getDecl())) { 2803 Diag(ELoc, diag::err_omp_expected_var_name) << ERange; 2804 continue; 2805 } 2806 auto D = DE->getDecl(); 2807 auto VD = cast<VarDecl>(D); 2808 auto Type = VD->getType(); 2809 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] 2810 // A variable that appears in a private clause must not have an incomplete 2811 // type or a reference type. 2812 if (RequireCompleteType(ELoc, Type, 2813 diag::err_omp_reduction_incomplete_type)) 2814 continue; 2815 // OpenMP [2.14.3.6, reduction clause, Restrictions] 2816 // Arrays may not appear in a reduction clause. 2817 if (Type.getNonReferenceType()->isArrayType()) { 2818 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange; 2819 bool IsDecl = 2820 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2821 Diag(VD->getLocation(), 2822 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2823 << VD; 2824 continue; 2825 } 2826 // OpenMP [2.14.3.6, reduction clause, Restrictions] 2827 // A list item that appears in a reduction clause must not be 2828 // const-qualified. 2829 if (Type.getNonReferenceType().isConstant(Context)) { 2830 Diag(ELoc, diag::err_omp_const_variable) 2831 << getOpenMPClauseName(OMPC_reduction) << Type << ERange; 2832 bool IsDecl = 2833 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2834 Diag(VD->getLocation(), 2835 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2836 << VD; 2837 continue; 2838 } 2839 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4] 2840 // If a list-item is a reference type then it must bind to the same object 2841 // for all threads of the team. 2842 VarDecl *VDDef = VD->getDefinition(); 2843 if (Type->isReferenceType() && VDDef) { 2844 DSARefChecker Check(DSAStack); 2845 if (Check.Visit(VDDef->getInit())) { 2846 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange; 2847 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef; 2848 continue; 2849 } 2850 } 2851 // OpenMP [2.14.3.6, reduction clause, Restrictions] 2852 // The type of a list item that appears in a reduction clause must be valid 2853 // for the reduction-identifier. For a max or min reduction in C, the type 2854 // of the list item must be an allowed arithmetic data type: char, int, 2855 // float, double, or _Bool, possibly modified with long, short, signed, or 2856 // unsigned. For a max or min reduction in C++, the type of the list item 2857 // must be an allowed arithmetic data type: char, wchar_t, int, float, 2858 // double, or bool, possibly modified with long, short, signed, or unsigned. 2859 if ((BOK == BO_GT || BOK == BO_LT) && 2860 !(Type->isScalarType() || 2861 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) { 2862 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg) 2863 << getLangOpts().CPlusPlus; 2864 bool IsDecl = 2865 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2866 Diag(VD->getLocation(), 2867 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2868 << VD; 2869 continue; 2870 } 2871 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) && 2872 !getLangOpts().CPlusPlus && Type->isFloatingType()) { 2873 Diag(ELoc, diag::err_omp_clause_floating_type_arg); 2874 bool IsDecl = 2875 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2876 Diag(VD->getLocation(), 2877 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2878 << VD; 2879 continue; 2880 } 2881 bool Suppress = getDiagnostics().getSuppressAllDiagnostics(); 2882 getDiagnostics().setSuppressAllDiagnostics(true); 2883 ExprResult ReductionOp = 2884 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK, 2885 RefExpr, RefExpr); 2886 getDiagnostics().setSuppressAllDiagnostics(Suppress); 2887 if (ReductionOp.isInvalid()) { 2888 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type 2889 << ReductionIdRange; 2890 bool IsDecl = 2891 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 2892 Diag(VD->getLocation(), 2893 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2894 << VD; 2895 continue; 2896 } 2897 2898 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced 2899 // in a Construct] 2900 // Variables with the predetermined data-sharing attributes may not be 2901 // listed in data-sharing attributes clauses, except for the cases 2902 // listed below. For these exceptions only, listing a predetermined 2903 // variable in a data-sharing attribute clause is allowed and overrides 2904 // the variable's predetermined data-sharing attributes. 2905 // OpenMP [2.14.3.6, Restrictions, p.3] 2906 // Any number of reduction clauses can be specified on the directive, 2907 // but a list item can appear only once in the reduction clauses for that 2908 // directive. 2909 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 2910 if (DVar.CKind == OMPC_reduction) { 2911 Diag(ELoc, diag::err_omp_once_referenced) 2912 << getOpenMPClauseName(OMPC_reduction); 2913 if (DVar.RefExpr) { 2914 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced); 2915 } 2916 } else if (DVar.CKind != OMPC_unknown) { 2917 Diag(ELoc, diag::err_omp_wrong_dsa) 2918 << getOpenMPClauseName(DVar.CKind) 2919 << getOpenMPClauseName(OMPC_reduction); 2920 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2921 continue; 2922 } 2923 2924 // OpenMP [2.14.3.6, Restrictions, p.1] 2925 // A list item that appears in a reduction clause of a worksharing 2926 // construct must be shared in the parallel regions to which any of the 2927 // worksharing regions arising from the worksharing construct bind. 2928 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective(); 2929 if (isOpenMPWorksharingDirective(CurrDir) && 2930 !isOpenMPParallelDirective(CurrDir)) { 2931 DVar = DSAStack->getImplicitDSA(VD); 2932 if (DVar.CKind != OMPC_shared) { 2933 Diag(ELoc, diag::err_omp_required_access) 2934 << getOpenMPClauseName(OMPC_reduction) 2935 << getOpenMPClauseName(OMPC_shared); 2936 ReportOriginalDSA(*this, DSAStack, VD, DVar); 2937 continue; 2938 } 2939 } 2940 2941 CXXRecordDecl *RD = getLangOpts().CPlusPlus 2942 ? Type.getNonReferenceType()->getAsCXXRecordDecl() 2943 : nullptr; 2944 // FIXME This code must be replaced by actual constructing/destructing of 2945 // the reduction variable. 2946 if (RD) { 2947 CXXConstructorDecl *CD = LookupDefaultConstructor(RD); 2948 PartialDiagnostic PD = 2949 PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); 2950 if (!CD || 2951 CheckConstructorAccess(ELoc, CD, 2952 InitializedEntity::InitializeTemporary(Type), 2953 CD->getAccess(), PD) == AR_inaccessible || 2954 CD->isDeleted()) { 2955 Diag(ELoc, diag::err_omp_required_method) 2956 << getOpenMPClauseName(OMPC_reduction) << 0; 2957 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2958 VarDecl::DeclarationOnly; 2959 Diag(VD->getLocation(), 2960 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2961 << VD; 2962 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 2963 continue; 2964 } 2965 MarkFunctionReferenced(ELoc, CD); 2966 DiagnoseUseOfDecl(CD, ELoc); 2967 2968 CXXDestructorDecl *DD = RD->getDestructor(); 2969 if (DD) { 2970 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || 2971 DD->isDeleted()) { 2972 Diag(ELoc, diag::err_omp_required_method) 2973 << getOpenMPClauseName(OMPC_reduction) << 4; 2974 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 2975 VarDecl::DeclarationOnly; 2976 Diag(VD->getLocation(), 2977 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 2978 << VD; 2979 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 2980 continue; 2981 } 2982 MarkFunctionReferenced(ELoc, DD); 2983 DiagnoseUseOfDecl(DD, ELoc); 2984 } 2985 } 2986 2987 DSAStack->addDSA(VD, DE, OMPC_reduction); 2988 Vars.push_back(DE); 2989 } 2990 2991 if (Vars.empty()) 2992 return nullptr; 2993 2994 return OMPReductionClause::Create( 2995 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars, 2996 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId); 2997 } 2998 2999 OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step, 3000 SourceLocation StartLoc, 3001 SourceLocation LParenLoc, 3002 SourceLocation ColonLoc, 3003 SourceLocation EndLoc) { 3004 SmallVector<Expr *, 8> Vars; 3005 for (auto &RefExpr : VarList) { 3006 assert(RefExpr && "NULL expr in OpenMP linear clause."); 3007 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 3008 // It will be analyzed later. 3009 Vars.push_back(RefExpr); 3010 continue; 3011 } 3012 3013 // OpenMP [2.14.3.7, linear clause] 3014 // A list item that appears in a linear clause is subject to the private 3015 // clause semantics described in Section 2.14.3.3 on page 159 except as 3016 // noted. In addition, the value of the new list item on each iteration 3017 // of the associated loop(s) corresponds to the value of the original 3018 // list item before entering the construct plus the logical number of 3019 // the iteration times linear-step. 3020 3021 SourceLocation ELoc = RefExpr->getExprLoc(); 3022 // OpenMP [2.1, C/C++] 3023 // A list item is a variable name. 3024 // OpenMP [2.14.3.3, Restrictions, p.1] 3025 // A variable that is part of another variable (as an array or 3026 // structure element) cannot appear in a private clause. 3027 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 3028 if (!DE || !isa<VarDecl>(DE->getDecl())) { 3029 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 3030 continue; 3031 } 3032 3033 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 3034 3035 // OpenMP [2.14.3.7, linear clause] 3036 // A list-item cannot appear in more than one linear clause. 3037 // A list-item that appears in a linear clause cannot appear in any 3038 // other data-sharing attribute clause. 3039 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); 3040 if (DVar.RefExpr) { 3041 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) 3042 << getOpenMPClauseName(OMPC_linear); 3043 ReportOriginalDSA(*this, DSAStack, VD, DVar); 3044 continue; 3045 } 3046 3047 QualType QType = VD->getType(); 3048 if (QType->isDependentType() || QType->isInstantiationDependentType()) { 3049 // It will be analyzed later. 3050 Vars.push_back(DE); 3051 continue; 3052 } 3053 3054 // A variable must not have an incomplete type or a reference type. 3055 if (RequireCompleteType(ELoc, QType, 3056 diag::err_omp_linear_incomplete_type)) { 3057 continue; 3058 } 3059 if (QType->isReferenceType()) { 3060 Diag(ELoc, diag::err_omp_clause_ref_type_arg) 3061 << getOpenMPClauseName(OMPC_linear) << QType; 3062 bool IsDecl = 3063 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3064 Diag(VD->getLocation(), 3065 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3066 << VD; 3067 continue; 3068 } 3069 3070 // A list item must not be const-qualified. 3071 if (QType.isConstant(Context)) { 3072 Diag(ELoc, diag::err_omp_const_variable) 3073 << getOpenMPClauseName(OMPC_linear); 3074 bool IsDecl = 3075 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3076 Diag(VD->getLocation(), 3077 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3078 << VD; 3079 continue; 3080 } 3081 3082 // A list item must be of integral or pointer type. 3083 QType = QType.getUnqualifiedType().getCanonicalType(); 3084 const Type *Ty = QType.getTypePtrOrNull(); 3085 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && 3086 !Ty->isPointerType())) { 3087 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType; 3088 bool IsDecl = 3089 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3090 Diag(VD->getLocation(), 3091 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3092 << VD; 3093 continue; 3094 } 3095 3096 DSAStack->addDSA(VD, DE, OMPC_linear); 3097 Vars.push_back(DE); 3098 } 3099 3100 if (Vars.empty()) 3101 return nullptr; 3102 3103 Expr *StepExpr = Step; 3104 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && 3105 !Step->isInstantiationDependent() && 3106 !Step->containsUnexpandedParameterPack()) { 3107 SourceLocation StepLoc = Step->getLocStart(); 3108 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step); 3109 if (Val.isInvalid()) 3110 return nullptr; 3111 StepExpr = Val.get(); 3112 3113 // Warn about zero linear step (it would be probably better specified as 3114 // making corresponding variables 'const'). 3115 llvm::APSInt Result; 3116 if (StepExpr->isIntegerConstantExpr(Result, Context) && 3117 !Result.isNegative() && !Result.isStrictlyPositive()) 3118 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] 3119 << (Vars.size() > 1); 3120 } 3121 3122 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc, 3123 Vars, StepExpr); 3124 } 3125 3126 OMPClause *Sema::ActOnOpenMPAlignedClause( 3127 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc, 3128 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) { 3129 3130 SmallVector<Expr *, 8> Vars; 3131 for (auto &RefExpr : VarList) { 3132 assert(RefExpr && "NULL expr in OpenMP aligned clause."); 3133 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 3134 // It will be analyzed later. 3135 Vars.push_back(RefExpr); 3136 continue; 3137 } 3138 3139 SourceLocation ELoc = RefExpr->getExprLoc(); 3140 // OpenMP [2.1, C/C++] 3141 // A list item is a variable name. 3142 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 3143 if (!DE || !isa<VarDecl>(DE->getDecl())) { 3144 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 3145 continue; 3146 } 3147 3148 VarDecl *VD = cast<VarDecl>(DE->getDecl()); 3149 3150 // OpenMP [2.8.1, simd construct, Restrictions] 3151 // The type of list items appearing in the aligned clause must be 3152 // array, pointer, reference to array, or reference to pointer. 3153 QualType QType = DE->getType() 3154 .getNonReferenceType() 3155 .getUnqualifiedType() 3156 .getCanonicalType(); 3157 const Type *Ty = QType.getTypePtrOrNull(); 3158 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() && 3159 !Ty->isPointerType())) { 3160 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr) 3161 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange(); 3162 bool IsDecl = 3163 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; 3164 Diag(VD->getLocation(), 3165 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3166 << VD; 3167 continue; 3168 } 3169 3170 // OpenMP [2.8.1, simd construct, Restrictions] 3171 // A list-item cannot appear in more than one aligned clause. 3172 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) { 3173 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange(); 3174 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa) 3175 << getOpenMPClauseName(OMPC_aligned); 3176 continue; 3177 } 3178 3179 Vars.push_back(DE); 3180 } 3181 3182 // OpenMP [2.8.1, simd construct, Description] 3183 // The parameter of the aligned clause, alignment, must be a constant 3184 // positive integer expression. 3185 // If no optional parameter is specified, implementation-defined default 3186 // alignments for SIMD instructions on the target platforms are assumed. 3187 if (Alignment != nullptr) { 3188 ExprResult AlignResult = 3189 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned); 3190 if (AlignResult.isInvalid()) 3191 return nullptr; 3192 Alignment = AlignResult.get(); 3193 } 3194 if (Vars.empty()) 3195 return nullptr; 3196 3197 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc, 3198 EndLoc, Vars, Alignment); 3199 } 3200 3201 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, 3202 SourceLocation StartLoc, 3203 SourceLocation LParenLoc, 3204 SourceLocation EndLoc) { 3205 SmallVector<Expr *, 8> Vars; 3206 for (auto &RefExpr : VarList) { 3207 assert(RefExpr && "NULL expr in OpenMP copyin clause."); 3208 if (isa<DependentScopeDeclRefExpr>(RefExpr)) { 3209 // It will be analyzed later. 3210 Vars.push_back(RefExpr); 3211 continue; 3212 } 3213 3214 SourceLocation ELoc = RefExpr->getExprLoc(); 3215 // OpenMP [2.1, C/C++] 3216 // A list item is a variable name. 3217 // OpenMP [2.14.4.1, Restrictions, p.1] 3218 // A list item that appears in a copyin clause must be threadprivate. 3219 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); 3220 if (!DE || !isa<VarDecl>(DE->getDecl())) { 3221 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); 3222 continue; 3223 } 3224 3225 Decl *D = DE->getDecl(); 3226 VarDecl *VD = cast<VarDecl>(D); 3227 3228 QualType Type = VD->getType(); 3229 if (Type->isDependentType() || Type->isInstantiationDependentType()) { 3230 // It will be analyzed later. 3231 Vars.push_back(DE); 3232 continue; 3233 } 3234 3235 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] 3236 // A list item that appears in a copyin clause must be threadprivate. 3237 if (!DSAStack->isThreadPrivate(VD)) { 3238 Diag(ELoc, diag::err_omp_required_access) 3239 << getOpenMPClauseName(OMPC_copyin) 3240 << getOpenMPDirectiveName(OMPD_threadprivate); 3241 continue; 3242 } 3243 3244 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] 3245 // A variable of class type (or array thereof) that appears in a 3246 // copyin clause requires an accessible, unambiguous copy assignment 3247 // operator for the class type. 3248 Type = Context.getBaseElementType(Type); 3249 CXXRecordDecl *RD = 3250 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; 3251 // FIXME This code must be replaced by actual assignment of the 3252 // threadprivate variable. 3253 if (RD) { 3254 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0); 3255 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess()); 3256 if (MD) { 3257 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible || 3258 MD->isDeleted()) { 3259 Diag(ELoc, diag::err_omp_required_method) 3260 << getOpenMPClauseName(OMPC_copyin) << 2; 3261 bool IsDecl = VD->isThisDeclarationADefinition(Context) == 3262 VarDecl::DeclarationOnly; 3263 Diag(VD->getLocation(), 3264 IsDecl ? diag::note_previous_decl : diag::note_defined_here) 3265 << VD; 3266 Diag(RD->getLocation(), diag::note_previous_decl) << RD; 3267 continue; 3268 } 3269 MarkFunctionReferenced(ELoc, MD); 3270 DiagnoseUseOfDecl(MD, ELoc); 3271 } 3272 } 3273 3274 DSAStack->addDSA(VD, DE, OMPC_copyin); 3275 Vars.push_back(DE); 3276 } 3277 3278 if (Vars.empty()) 3279 return nullptr; 3280 3281 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); 3282 } 3283 3284 #undef DSAStack 3285