Home | History | Annotate | Download | only in Sema
      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 "TreeTransform.h"
     16 #include "clang/AST/ASTContext.h"
     17 #include "clang/AST/ASTMutationListener.h"
     18 #include "clang/AST/Decl.h"
     19 #include "clang/AST/DeclCXX.h"
     20 #include "clang/AST/DeclOpenMP.h"
     21 #include "clang/AST/StmtCXX.h"
     22 #include "clang/AST/StmtOpenMP.h"
     23 #include "clang/AST/StmtVisitor.h"
     24 #include "clang/Basic/OpenMPKinds.h"
     25 #include "clang/Basic/TargetInfo.h"
     26 #include "clang/Lex/Preprocessor.h"
     27 #include "clang/Sema/Initialization.h"
     28 #include "clang/Sema/Lookup.h"
     29 #include "clang/Sema/Scope.h"
     30 #include "clang/Sema/ScopeInfo.h"
     31 #include "clang/Sema/SemaInternal.h"
     32 using namespace clang;
     33 
     34 //===----------------------------------------------------------------------===//
     35 // Stack of data-sharing attributes for variables
     36 //===----------------------------------------------------------------------===//
     37 
     38 namespace {
     39 /// \brief Default data sharing attributes, which can be applied to directive.
     40 enum DefaultDataSharingAttributes {
     41   DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
     42   DSA_none = 1 << 0,   /// \brief Default data sharing attribute 'none'.
     43   DSA_shared = 1 << 1  /// \brief Default data sharing attribute 'shared'.
     44 };
     45 
     46 template <class T> struct MatchesAny {
     47   explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
     48   bool operator()(T Kind) {
     49     for (auto KindEl : Arr)
     50       if (KindEl == Kind)
     51         return true;
     52     return false;
     53   }
     54 
     55 private:
     56   ArrayRef<T> Arr;
     57 };
     58 struct MatchesAlways {
     59   MatchesAlways() {}
     60   template <class T> bool operator()(T) { return true; }
     61 };
     62 
     63 typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
     64 typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
     65 
     66 /// \brief Stack for tracking declarations used in OpenMP directives and
     67 /// clauses and their data-sharing attributes.
     68 class DSAStackTy {
     69 public:
     70   struct DSAVarData {
     71     OpenMPDirectiveKind DKind;
     72     OpenMPClauseKind CKind;
     73     DeclRefExpr *RefExpr;
     74     SourceLocation ImplicitDSALoc;
     75     DSAVarData()
     76         : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
     77           ImplicitDSALoc() {}
     78   };
     79 
     80 public:
     81   struct MapInfo {
     82     Expr *RefExpr;
     83   };
     84 
     85 private:
     86   struct DSAInfo {
     87     OpenMPClauseKind Attributes;
     88     DeclRefExpr *RefExpr;
     89   };
     90   typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
     91   typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
     92   typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
     93   typedef llvm::SmallDenseMap<VarDecl *, MapInfo, 64> MappedDeclsTy;
     94   typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
     95       CriticalsWithHintsTy;
     96 
     97   struct SharingMapTy {
     98     DeclSAMapTy SharingMap;
     99     AlignedMapTy AlignedMap;
    100     MappedDeclsTy MappedDecls;
    101     LoopControlVariablesSetTy LCVSet;
    102     DefaultDataSharingAttributes DefaultAttr;
    103     SourceLocation DefaultAttrLoc;
    104     OpenMPDirectiveKind Directive;
    105     DeclarationNameInfo DirectiveName;
    106     Scope *CurScope;
    107     SourceLocation ConstructLoc;
    108     /// \brief first argument (Expr *) contains optional argument of the
    109     /// 'ordered' clause, the second one is true if the regions has 'ordered'
    110     /// clause, false otherwise.
    111     llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
    112     bool NowaitRegion;
    113     bool CancelRegion;
    114     unsigned CollapseNumber;
    115     SourceLocation InnerTeamsRegionLoc;
    116     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
    117                  Scope *CurScope, SourceLocation Loc)
    118         : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
    119           Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
    120           ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
    121           CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
    122     SharingMapTy()
    123         : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
    124           Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
    125           ConstructLoc(), OrderedRegion(), NowaitRegion(false),
    126           CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
    127   };
    128 
    129   typedef SmallVector<SharingMapTy, 64> StackTy;
    130 
    131   /// \brief Stack of used declaration and their data-sharing attributes.
    132   StackTy Stack;
    133   /// \brief true, if check for DSA must be from parent directive, false, if
    134   /// from current directive.
    135   OpenMPClauseKind ClauseKindMode;
    136   Sema &SemaRef;
    137   bool ForceCapturing;
    138   CriticalsWithHintsTy Criticals;
    139 
    140   typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
    141 
    142   DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
    143 
    144   /// \brief Checks if the variable is a local for OpenMP region.
    145   bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
    146 
    147 public:
    148   explicit DSAStackTy(Sema &S)
    149       : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
    150         ForceCapturing(false) {}
    151 
    152   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
    153   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
    154 
    155   bool isForceVarCapturing() const { return ForceCapturing; }
    156   void setForceVarCapturing(bool V) { ForceCapturing = V; }
    157 
    158   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
    159             Scope *CurScope, SourceLocation Loc) {
    160     Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
    161     Stack.back().DefaultAttrLoc = Loc;
    162   }
    163 
    164   void pop() {
    165     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
    166     Stack.pop_back();
    167   }
    168 
    169   void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
    170     Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
    171   }
    172   const std::pair<OMPCriticalDirective *, llvm::APSInt>
    173   getCriticalWithHint(const DeclarationNameInfo &Name) const {
    174     auto I = Criticals.find(Name.getAsString());
    175     if (I != Criticals.end())
    176       return I->second;
    177     return std::make_pair(nullptr, llvm::APSInt());
    178   }
    179   /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
    180   /// add it and return NULL; otherwise return previous occurrence's expression
    181   /// for diagnostics.
    182   DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
    183 
    184   /// \brief Register specified variable as loop control variable.
    185   void addLoopControlVariable(VarDecl *D);
    186   /// \brief Check if the specified variable is a loop control variable for
    187   /// current region.
    188   bool isLoopControlVariable(VarDecl *D);
    189 
    190   /// \brief Adds explicit data sharing attribute to the specified declaration.
    191   void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
    192 
    193   /// \brief Returns data sharing attributes from top of the stack for the
    194   /// specified declaration.
    195   DSAVarData getTopDSA(VarDecl *D, bool FromParent);
    196   /// \brief Returns data-sharing attributes for the specified declaration.
    197   DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
    198   /// \brief Checks if the specified variables has data-sharing attributes which
    199   /// match specified \a CPred predicate in any directive which matches \a DPred
    200   /// predicate.
    201   template <class ClausesPredicate, class DirectivesPredicate>
    202   DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
    203                     DirectivesPredicate DPred, bool FromParent);
    204   /// \brief Checks if the specified variables has data-sharing attributes which
    205   /// match specified \a CPred predicate in any innermost directive which
    206   /// matches \a DPred predicate.
    207   template <class ClausesPredicate, class DirectivesPredicate>
    208   DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
    209                              DirectivesPredicate DPred,
    210                              bool FromParent);
    211   /// \brief Checks if the specified variables has explicit data-sharing
    212   /// attributes which match specified \a CPred predicate at the specified
    213   /// OpenMP region.
    214   bool hasExplicitDSA(VarDecl *D,
    215                       const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
    216                       unsigned Level);
    217 
    218   /// \brief Returns true if the directive at level \Level matches in the
    219   /// specified \a DPred predicate.
    220   bool hasExplicitDirective(
    221       const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
    222       unsigned Level);
    223 
    224   /// \brief Finds a directive which matches specified \a DPred predicate.
    225   template <class NamedDirectivesPredicate>
    226   bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
    227 
    228   /// \brief Returns currently analyzed directive.
    229   OpenMPDirectiveKind getCurrentDirective() const {
    230     return Stack.back().Directive;
    231   }
    232   /// \brief Returns parent directive.
    233   OpenMPDirectiveKind getParentDirective() const {
    234     if (Stack.size() > 2)
    235       return Stack[Stack.size() - 2].Directive;
    236     return OMPD_unknown;
    237   }
    238   /// \brief Return the directive associated with the provided scope.
    239   OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
    240 
    241   /// \brief Set default data sharing attribute to none.
    242   void setDefaultDSANone(SourceLocation Loc) {
    243     Stack.back().DefaultAttr = DSA_none;
    244     Stack.back().DefaultAttrLoc = Loc;
    245   }
    246   /// \brief Set default data sharing attribute to shared.
    247   void setDefaultDSAShared(SourceLocation Loc) {
    248     Stack.back().DefaultAttr = DSA_shared;
    249     Stack.back().DefaultAttrLoc = Loc;
    250   }
    251 
    252   DefaultDataSharingAttributes getDefaultDSA() const {
    253     return Stack.back().DefaultAttr;
    254   }
    255   SourceLocation getDefaultDSALocation() const {
    256     return Stack.back().DefaultAttrLoc;
    257   }
    258 
    259   /// \brief Checks if the specified variable is a threadprivate.
    260   bool isThreadPrivate(VarDecl *D) {
    261     DSAVarData DVar = getTopDSA(D, false);
    262     return isOpenMPThreadPrivate(DVar.CKind);
    263   }
    264 
    265   /// \brief Marks current region as ordered (it has an 'ordered' clause).
    266   void setOrderedRegion(bool IsOrdered, Expr *Param) {
    267     Stack.back().OrderedRegion.setInt(IsOrdered);
    268     Stack.back().OrderedRegion.setPointer(Param);
    269   }
    270   /// \brief Returns true, if parent region is ordered (has associated
    271   /// 'ordered' clause), false - otherwise.
    272   bool isParentOrderedRegion() const {
    273     if (Stack.size() > 2)
    274       return Stack[Stack.size() - 2].OrderedRegion.getInt();
    275     return false;
    276   }
    277   /// \brief Returns optional parameter for the ordered region.
    278   Expr *getParentOrderedRegionParam() const {
    279     if (Stack.size() > 2)
    280       return Stack[Stack.size() - 2].OrderedRegion.getPointer();
    281     return nullptr;
    282   }
    283   /// \brief Marks current region as nowait (it has a 'nowait' clause).
    284   void setNowaitRegion(bool IsNowait = true) {
    285     Stack.back().NowaitRegion = IsNowait;
    286   }
    287   /// \brief Returns true, if parent region is nowait (has associated
    288   /// 'nowait' clause), false - otherwise.
    289   bool isParentNowaitRegion() const {
    290     if (Stack.size() > 2)
    291       return Stack[Stack.size() - 2].NowaitRegion;
    292     return false;
    293   }
    294   /// \brief Marks parent region as cancel region.
    295   void setParentCancelRegion(bool Cancel = true) {
    296     if (Stack.size() > 2)
    297       Stack[Stack.size() - 2].CancelRegion =
    298           Stack[Stack.size() - 2].CancelRegion || Cancel;
    299   }
    300   /// \brief Return true if current region has inner cancel construct.
    301   bool isCancelRegion() const {
    302     return Stack.back().CancelRegion;
    303   }
    304 
    305   /// \brief Set collapse value for the region.
    306   void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
    307   /// \brief Return collapse value for region.
    308   unsigned getCollapseNumber() const {
    309     return Stack.back().CollapseNumber;
    310   }
    311 
    312   /// \brief Marks current target region as one with closely nested teams
    313   /// region.
    314   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
    315     if (Stack.size() > 2)
    316       Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
    317   }
    318   /// \brief Returns true, if current region has closely nested teams region.
    319   bool hasInnerTeamsRegion() const {
    320     return getInnerTeamsRegionLoc().isValid();
    321   }
    322   /// \brief Returns location of the nested teams region (if any).
    323   SourceLocation getInnerTeamsRegionLoc() const {
    324     if (Stack.size() > 1)
    325       return Stack.back().InnerTeamsRegionLoc;
    326     return SourceLocation();
    327   }
    328 
    329   Scope *getCurScope() const { return Stack.back().CurScope; }
    330   Scope *getCurScope() { return Stack.back().CurScope; }
    331   SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
    332 
    333   MapInfo getMapInfoForVar(VarDecl *VD) {
    334     MapInfo VarMI = {0};
    335     for (auto Cnt = Stack.size() - 1; Cnt > 0; --Cnt) {
    336       if (Stack[Cnt].MappedDecls.count(VD)) {
    337         VarMI = Stack[Cnt].MappedDecls[VD];
    338         break;
    339       }
    340     }
    341     return VarMI;
    342   }
    343 
    344   void addMapInfoForVar(VarDecl *VD, MapInfo MI) {
    345     if (Stack.size() > 1) {
    346       Stack.back().MappedDecls[VD] = MI;
    347     }
    348   }
    349 
    350   MapInfo IsMappedInCurrentRegion(VarDecl *VD) {
    351     assert(Stack.size() > 1 && "Target level is 0");
    352     MapInfo VarMI = {0};
    353     if (Stack.size() > 1 && Stack.back().MappedDecls.count(VD)) {
    354       VarMI = Stack.back().MappedDecls[VD];
    355     }
    356     return VarMI;
    357   }
    358 };
    359 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
    360   return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
    361          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
    362          isOpenMPTaskLoopDirective(DKind);
    363 }
    364 } // namespace
    365 
    366 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
    367                                           VarDecl *D) {
    368   D = D->getCanonicalDecl();
    369   DSAVarData DVar;
    370   if (Iter == std::prev(Stack.rend())) {
    371     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    372     // in a region but not in construct]
    373     //  File-scope or namespace-scope variables referenced in called routines
    374     //  in the region are shared unless they appear in a threadprivate
    375     //  directive.
    376     if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
    377       DVar.CKind = OMPC_shared;
    378 
    379     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
    380     // in a region but not in construct]
    381     //  Variables with static storage duration that are declared in called
    382     //  routines in the region are shared.
    383     if (D->hasGlobalStorage())
    384       DVar.CKind = OMPC_shared;
    385 
    386     return DVar;
    387   }
    388 
    389   DVar.DKind = Iter->Directive;
    390   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    391   // in a Construct, C/C++, predetermined, p.1]
    392   // Variables with automatic storage duration that are declared in a scope
    393   // inside the construct are private.
    394   if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
    395       (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
    396     DVar.CKind = OMPC_private;
    397     return DVar;
    398   }
    399 
    400   // Explicitly specified attributes and local variables with predetermined
    401   // attributes.
    402   if (Iter->SharingMap.count(D)) {
    403     DVar.RefExpr = Iter->SharingMap[D].RefExpr;
    404     DVar.CKind = Iter->SharingMap[D].Attributes;
    405     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
    406     return DVar;
    407   }
    408 
    409   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    410   // in a Construct, C/C++, implicitly determined, p.1]
    411   //  In a parallel or task construct, the data-sharing attributes of these
    412   //  variables are determined by the default clause, if present.
    413   switch (Iter->DefaultAttr) {
    414   case DSA_shared:
    415     DVar.CKind = OMPC_shared;
    416     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
    417     return DVar;
    418   case DSA_none:
    419     return DVar;
    420   case DSA_unspecified:
    421     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    422     // in a Construct, implicitly determined, p.2]
    423     //  In a parallel construct, if no default clause is present, these
    424     //  variables are shared.
    425     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
    426     if (isOpenMPParallelDirective(DVar.DKind) ||
    427         isOpenMPTeamsDirective(DVar.DKind)) {
    428       DVar.CKind = OMPC_shared;
    429       return DVar;
    430     }
    431 
    432     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    433     // in a Construct, implicitly determined, p.4]
    434     //  In a task construct, if no default clause is present, a variable that in
    435     //  the enclosing context is determined to be shared by all implicit tasks
    436     //  bound to the current team is shared.
    437     if (DVar.DKind == OMPD_task) {
    438       DSAVarData DVarTemp;
    439       for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
    440            I != EE; ++I) {
    441         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
    442         // Referenced
    443         // in a Construct, implicitly determined, p.6]
    444         //  In a task construct, if no default clause is present, a variable
    445         //  whose data-sharing attribute is not determined by the rules above is
    446         //  firstprivate.
    447         DVarTemp = getDSA(I, D);
    448         if (DVarTemp.CKind != OMPC_shared) {
    449           DVar.RefExpr = nullptr;
    450           DVar.DKind = OMPD_task;
    451           DVar.CKind = OMPC_firstprivate;
    452           return DVar;
    453         }
    454         if (isParallelOrTaskRegion(I->Directive))
    455           break;
    456       }
    457       DVar.DKind = OMPD_task;
    458       DVar.CKind =
    459           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
    460       return DVar;
    461     }
    462   }
    463   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    464   // in a Construct, implicitly determined, p.3]
    465   //  For constructs other than task, if no default clause is present, these
    466   //  variables inherit their data-sharing attributes from the enclosing
    467   //  context.
    468   return getDSA(std::next(Iter), D);
    469 }
    470 
    471 DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
    472   assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
    473   D = D->getCanonicalDecl();
    474   auto It = Stack.back().AlignedMap.find(D);
    475   if (It == Stack.back().AlignedMap.end()) {
    476     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
    477     Stack.back().AlignedMap[D] = NewDE;
    478     return nullptr;
    479   } else {
    480     assert(It->second && "Unexpected nullptr expr in the aligned map");
    481     return It->second;
    482   }
    483   return nullptr;
    484 }
    485 
    486 void DSAStackTy::addLoopControlVariable(VarDecl *D) {
    487   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
    488   D = D->getCanonicalDecl();
    489   Stack.back().LCVSet.insert(D);
    490 }
    491 
    492 bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
    493   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
    494   D = D->getCanonicalDecl();
    495   return Stack.back().LCVSet.count(D) > 0;
    496 }
    497 
    498 void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
    499   D = D->getCanonicalDecl();
    500   if (A == OMPC_threadprivate) {
    501     Stack[0].SharingMap[D].Attributes = A;
    502     Stack[0].SharingMap[D].RefExpr = E;
    503   } else {
    504     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
    505     Stack.back().SharingMap[D].Attributes = A;
    506     Stack.back().SharingMap[D].RefExpr = E;
    507   }
    508 }
    509 
    510 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
    511   D = D->getCanonicalDecl();
    512   if (Stack.size() > 2) {
    513     reverse_iterator I = Iter, E = std::prev(Stack.rend());
    514     Scope *TopScope = nullptr;
    515     while (I != E && !isParallelOrTaskRegion(I->Directive)) {
    516       ++I;
    517     }
    518     if (I == E)
    519       return false;
    520     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
    521     Scope *CurScope = getCurScope();
    522     while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
    523       CurScope = CurScope->getParent();
    524     }
    525     return CurScope != TopScope;
    526   }
    527   return false;
    528 }
    529 
    530 /// \brief Build a variable declaration for OpenMP loop iteration variable.
    531 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
    532                              StringRef Name, const AttrVec *Attrs = nullptr) {
    533   DeclContext *DC = SemaRef.CurContext;
    534   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
    535   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
    536   VarDecl *Decl =
    537       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
    538   if (Attrs) {
    539     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
    540          I != E; ++I)
    541       Decl->addAttr(*I);
    542   }
    543   Decl->setImplicit();
    544   return Decl;
    545 }
    546 
    547 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
    548                                      SourceLocation Loc,
    549                                      bool RefersToCapture = false) {
    550   D->setReferenced();
    551   D->markUsed(S.Context);
    552   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
    553                              SourceLocation(), D, RefersToCapture, Loc, Ty,
    554                              VK_LValue);
    555 }
    556 
    557 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
    558   D = D->getCanonicalDecl();
    559   DSAVarData DVar;
    560 
    561   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    562   // in a Construct, C/C++, predetermined, p.1]
    563   //  Variables appearing in threadprivate directives are threadprivate.
    564   if ((D->getTLSKind() != VarDecl::TLS_None &&
    565        !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
    566          SemaRef.getLangOpts().OpenMPUseTLS &&
    567          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
    568       (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
    569        !D->isLocalVarDecl())) {
    570     addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
    571                                D->getLocation()),
    572            OMPC_threadprivate);
    573   }
    574   if (Stack[0].SharingMap.count(D)) {
    575     DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
    576     DVar.CKind = OMPC_threadprivate;
    577     return DVar;
    578   }
    579 
    580   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    581   // in a Construct, C/C++, predetermined, p.4]
    582   //  Static data members are shared.
    583   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    584   // in a Construct, C/C++, predetermined, p.7]
    585   //  Variables with static storage duration that are declared in a scope
    586   //  inside the construct are shared.
    587   if (D->isStaticDataMember()) {
    588     DSAVarData DVarTemp =
    589         hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
    590     if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
    591       return DVar;
    592 
    593     DVar.CKind = OMPC_shared;
    594     return DVar;
    595   }
    596 
    597   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
    598   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
    599   Type = SemaRef.getASTContext().getBaseElementType(Type);
    600   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
    601   // in a Construct, C/C++, predetermined, p.6]
    602   //  Variables with const qualified type having no mutable member are
    603   //  shared.
    604   CXXRecordDecl *RD =
    605       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
    606   if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
    607     if (auto *CTD = CTSD->getSpecializedTemplate())
    608       RD = CTD->getTemplatedDecl();
    609   if (IsConstant &&
    610       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
    611     // Variables with const-qualified type having no mutable member may be
    612     // listed in a firstprivate clause, even if they are static data members.
    613     DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
    614                                  MatchesAlways(), FromParent);
    615     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
    616       return DVar;
    617 
    618     DVar.CKind = OMPC_shared;
    619     return DVar;
    620   }
    621 
    622   // Explicitly specified attributes and local variables with predetermined
    623   // attributes.
    624   auto StartI = std::next(Stack.rbegin());
    625   auto EndI = std::prev(Stack.rend());
    626   if (FromParent && StartI != EndI) {
    627     StartI = std::next(StartI);
    628   }
    629   auto I = std::prev(StartI);
    630   if (I->SharingMap.count(D)) {
    631     DVar.RefExpr = I->SharingMap[D].RefExpr;
    632     DVar.CKind = I->SharingMap[D].Attributes;
    633     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
    634   }
    635 
    636   return DVar;
    637 }
    638 
    639 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
    640   D = D->getCanonicalDecl();
    641   auto StartI = Stack.rbegin();
    642   auto EndI = std::prev(Stack.rend());
    643   if (FromParent && StartI != EndI) {
    644     StartI = std::next(StartI);
    645   }
    646   return getDSA(StartI, D);
    647 }
    648 
    649 template <class ClausesPredicate, class DirectivesPredicate>
    650 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
    651                                           DirectivesPredicate DPred,
    652                                           bool FromParent) {
    653   D = D->getCanonicalDecl();
    654   auto StartI = std::next(Stack.rbegin());
    655   auto EndI = std::prev(Stack.rend());
    656   if (FromParent && StartI != EndI) {
    657     StartI = std::next(StartI);
    658   }
    659   for (auto I = StartI, EE = EndI; I != EE; ++I) {
    660     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
    661       continue;
    662     DSAVarData DVar = getDSA(I, D);
    663     if (CPred(DVar.CKind))
    664       return DVar;
    665   }
    666   return DSAVarData();
    667 }
    668 
    669 template <class ClausesPredicate, class DirectivesPredicate>
    670 DSAStackTy::DSAVarData
    671 DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
    672                             DirectivesPredicate DPred, bool FromParent) {
    673   D = D->getCanonicalDecl();
    674   auto StartI = std::next(Stack.rbegin());
    675   auto EndI = std::prev(Stack.rend());
    676   if (FromParent && StartI != EndI) {
    677     StartI = std::next(StartI);
    678   }
    679   for (auto I = StartI, EE = EndI; I != EE; ++I) {
    680     if (!DPred(I->Directive))
    681       break;
    682     DSAVarData DVar = getDSA(I, D);
    683     if (CPred(DVar.CKind))
    684       return DVar;
    685     return DSAVarData();
    686   }
    687   return DSAVarData();
    688 }
    689 
    690 bool DSAStackTy::hasExplicitDSA(
    691     VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
    692     unsigned Level) {
    693   if (CPred(ClauseKindMode))
    694     return true;
    695   if (isClauseParsingMode())
    696     ++Level;
    697   D = D->getCanonicalDecl();
    698   auto StartI = Stack.rbegin();
    699   auto EndI = std::prev(Stack.rend());
    700   if (std::distance(StartI, EndI) <= (int)Level)
    701     return false;
    702   std::advance(StartI, Level);
    703   return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
    704          CPred(StartI->SharingMap[D].Attributes);
    705 }
    706 
    707 bool DSAStackTy::hasExplicitDirective(
    708     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
    709     unsigned Level) {
    710   if (isClauseParsingMode())
    711     ++Level;
    712   auto StartI = Stack.rbegin();
    713   auto EndI = std::prev(Stack.rend());
    714   if (std::distance(StartI, EndI) <= (int)Level)
    715     return false;
    716   std::advance(StartI, Level);
    717   return DPred(StartI->Directive);
    718 }
    719 
    720 template <class NamedDirectivesPredicate>
    721 bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
    722   auto StartI = std::next(Stack.rbegin());
    723   auto EndI = std::prev(Stack.rend());
    724   if (FromParent && StartI != EndI) {
    725     StartI = std::next(StartI);
    726   }
    727   for (auto I = StartI, EE = EndI; I != EE; ++I) {
    728     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
    729       return true;
    730   }
    731   return false;
    732 }
    733 
    734 OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
    735   for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
    736     if (I->CurScope == S)
    737       return I->Directive;
    738   return OMPD_unknown;
    739 }
    740 
    741 void Sema::InitDataSharingAttributesStack() {
    742   VarDataSharingAttributesStack = new DSAStackTy(*this);
    743 }
    744 
    745 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
    746 
    747 bool Sema::IsOpenMPCapturedByRef(VarDecl *VD,
    748                                  const CapturedRegionScopeInfo *RSI) {
    749   assert(LangOpts.OpenMP && "OpenMP is not allowed");
    750 
    751   auto &Ctx = getASTContext();
    752   bool IsByRef = true;
    753 
    754   // Find the directive that is associated with the provided scope.
    755   auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
    756   auto Ty = VD->getType();
    757 
    758   if (isOpenMPTargetDirective(DKind)) {
    759     // This table summarizes how a given variable should be passed to the device
    760     // given its type and the clauses where it appears. This table is based on
    761     // the description in OpenMP 4.5 [2.10.4, target Construct] and
    762     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
    763     //
    764     // =========================================================================
    765     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
    766     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
    767     // =========================================================================
    768     // | scl  |               |     |       |       -       |          | bycopy|
    769     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
    770     // | scl  |               |  x  |   -   |       -       |     -    | null  |
    771     // | scl  |       x       |     |       |       -       |          | byref |
    772     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
    773     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
    774     // | scl  |               |  -  |   -   |       -       |     x    | byref |
    775     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
    776     //
    777     // | agg  |      n.a.     |     |       |       -       |          | byref |
    778     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
    779     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
    780     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
    781     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
    782     //
    783     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
    784     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
    785     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
    786     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
    787     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
    788     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
    789     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
    790     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
    791     // =========================================================================
    792     // Legend:
    793     //  scl - scalar
    794     //  ptr - pointer
    795     //  agg - aggregate
    796     //  x - applies
    797     //  - - invalid in this combination
    798     //  [] - mapped with an array section
    799     //  byref - should be mapped by reference
    800     //  byval - should be mapped by value
    801     //  null - initialize a local variable to null on the device
    802     //
    803     // Observations:
    804     //  - All scalar declarations that show up in a map clause have to be passed
    805     //    by reference, because they may have been mapped in the enclosing data
    806     //    environment.
    807     //  - If the scalar value does not fit the size of uintptr, it has to be
    808     //    passed by reference, regardless the result in the table above.
    809     //  - For pointers mapped by value that have either an implicit map or an
    810     //    array section, the runtime library may pass the NULL value to the
    811     //    device instead of the value passed to it by the compiler.
    812 
    813     // FIXME: Right now, only implicit maps are implemented. Properly mapping
    814     // values requires having the map, private, and firstprivate clauses SEMA
    815     // and parsing in place, which we don't yet.
    816 
    817     if (Ty->isReferenceType())
    818       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
    819     IsByRef = !Ty->isScalarType();
    820   }
    821 
    822   // When passing data by value, we need to make sure it fits the uintptr size
    823   // and alignment, because the runtime library only deals with uintptr types.
    824   // If it does not fit the uintptr size, we need to pass the data by reference
    825   // instead.
    826   if (!IsByRef &&
    827       (Ctx.getTypeSizeInChars(Ty) >
    828            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
    829        Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
    830     IsByRef = true;
    831 
    832   return IsByRef;
    833 }
    834 
    835 bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
    836   assert(LangOpts.OpenMP && "OpenMP is not allowed");
    837   VD = VD->getCanonicalDecl();
    838 
    839   // If we are attempting to capture a global variable in a directive with
    840   // 'target' we return true so that this global is also mapped to the device.
    841   //
    842   // FIXME: If the declaration is enclosed in a 'declare target' directive,
    843   // then it should not be captured. Therefore, an extra check has to be
    844   // inserted here once support for 'declare target' is added.
    845   //
    846   if (!VD->hasLocalStorage()) {
    847     if (DSAStack->getCurrentDirective() == OMPD_target &&
    848         !DSAStack->isClauseParsingMode()) {
    849       return true;
    850     }
    851     if (DSAStack->getCurScope() &&
    852         DSAStack->hasDirective(
    853             [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
    854                SourceLocation Loc) -> bool {
    855               return isOpenMPTargetDirective(K);
    856             },
    857             false)) {
    858       return true;
    859     }
    860   }
    861 
    862   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
    863       (!DSAStack->isClauseParsingMode() ||
    864        DSAStack->getParentDirective() != OMPD_unknown)) {
    865     if (DSAStack->isLoopControlVariable(VD) ||
    866         (VD->hasLocalStorage() &&
    867          isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
    868         DSAStack->isForceVarCapturing())
    869       return true;
    870     auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
    871     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
    872       return true;
    873     DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
    874                                    DSAStack->isClauseParsingMode());
    875     return DVarPrivate.CKind != OMPC_unknown;
    876   }
    877   return false;
    878 }
    879 
    880 bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
    881   assert(LangOpts.OpenMP && "OpenMP is not allowed");
    882   return DSAStack->hasExplicitDSA(
    883       VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
    884 }
    885 
    886 bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
    887   assert(LangOpts.OpenMP && "OpenMP is not allowed");
    888   // Return true if the current level is no longer enclosed in a target region.
    889 
    890   return !VD->hasLocalStorage() &&
    891          DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
    892 }
    893 
    894 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
    895 
    896 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
    897                                const DeclarationNameInfo &DirName,
    898                                Scope *CurScope, SourceLocation Loc) {
    899   DSAStack->push(DKind, DirName, CurScope, Loc);
    900   PushExpressionEvaluationContext(PotentiallyEvaluated);
    901 }
    902 
    903 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
    904   DSAStack->setClauseParsingMode(K);
    905 }
    906 
    907 void Sema::EndOpenMPClause() {
    908   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
    909 }
    910 
    911 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
    912   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
    913   //  A variable of class type (or array thereof) that appears in a lastprivate
    914   //  clause requires an accessible, unambiguous default constructor for the
    915   //  class type, unless the list item is also specified in a firstprivate
    916   //  clause.
    917   if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
    918     for (auto *C : D->clauses()) {
    919       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
    920         SmallVector<Expr *, 8> PrivateCopies;
    921         for (auto *DE : Clause->varlists()) {
    922           if (DE->isValueDependent() || DE->isTypeDependent()) {
    923             PrivateCopies.push_back(nullptr);
    924             continue;
    925           }
    926           auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
    927           QualType Type = VD->getType().getNonReferenceType();
    928           auto DVar = DSAStack->getTopDSA(VD, false);
    929           if (DVar.CKind == OMPC_lastprivate) {
    930             // Generate helper private variable and initialize it with the
    931             // default value. The address of the original variable is replaced
    932             // by the address of the new private variable in CodeGen. This new
    933             // variable is not added to IdResolver, so the code in the OpenMP
    934             // region uses original variable for proper diagnostics.
    935             auto *VDPrivate = buildVarDecl(
    936                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
    937                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
    938             ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
    939             if (VDPrivate->isInvalidDecl())
    940               continue;
    941             PrivateCopies.push_back(buildDeclRefExpr(
    942                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
    943           } else {
    944             // The variable is also a firstprivate, so initialization sequence
    945             // for private copy is generated already.
    946             PrivateCopies.push_back(nullptr);
    947           }
    948         }
    949         // Set initializers to private copies if no errors were found.
    950         if (PrivateCopies.size() == Clause->varlist_size()) {
    951           Clause->setPrivateCopies(PrivateCopies);
    952         }
    953       }
    954     }
    955   }
    956 
    957   DSAStack->pop();
    958   DiscardCleanupsInEvaluationContext();
    959   PopExpressionEvaluationContext();
    960 }
    961 
    962 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
    963                                      Expr *NumIterations, Sema &SemaRef,
    964                                      Scope *S);
    965 
    966 namespace {
    967 
    968 class VarDeclFilterCCC : public CorrectionCandidateCallback {
    969 private:
    970   Sema &SemaRef;
    971 
    972 public:
    973   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
    974   bool ValidateCandidate(const TypoCorrection &Candidate) override {
    975     NamedDecl *ND = Candidate.getCorrectionDecl();
    976     if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
    977       return VD->hasGlobalStorage() &&
    978              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
    979                                    SemaRef.getCurScope());
    980     }
    981     return false;
    982   }
    983 };
    984 } // namespace
    985 
    986 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
    987                                          CXXScopeSpec &ScopeSpec,
    988                                          const DeclarationNameInfo &Id) {
    989   LookupResult Lookup(*this, Id, LookupOrdinaryName);
    990   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
    991 
    992   if (Lookup.isAmbiguous())
    993     return ExprError();
    994 
    995   VarDecl *VD;
    996   if (!Lookup.isSingleResult()) {
    997     if (TypoCorrection Corrected = CorrectTypo(
    998             Id, LookupOrdinaryName, CurScope, nullptr,
    999             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
   1000       diagnoseTypo(Corrected,
   1001                    PDiag(Lookup.empty()
   1002                              ? diag::err_undeclared_var_use_suggest
   1003                              : diag::err_omp_expected_var_arg_suggest)
   1004                        << Id.getName());
   1005       VD = Corrected.getCorrectionDeclAs<VarDecl>();
   1006     } else {
   1007       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
   1008                                        : diag::err_omp_expected_var_arg)
   1009           << Id.getName();
   1010       return ExprError();
   1011     }
   1012   } else {
   1013     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
   1014       Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
   1015       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
   1016       return ExprError();
   1017     }
   1018   }
   1019   Lookup.suppressDiagnostics();
   1020 
   1021   // OpenMP [2.9.2, Syntax, C/C++]
   1022   //   Variables must be file-scope, namespace-scope, or static block-scope.
   1023   if (!VD->hasGlobalStorage()) {
   1024     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
   1025         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
   1026     bool IsDecl =
   1027         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   1028     Diag(VD->getLocation(),
   1029          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   1030         << VD;
   1031     return ExprError();
   1032   }
   1033 
   1034   VarDecl *CanonicalVD = VD->getCanonicalDecl();
   1035   NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
   1036   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
   1037   //   A threadprivate directive for file-scope variables must appear outside
   1038   //   any definition or declaration.
   1039   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
   1040       !getCurLexicalContext()->isTranslationUnit()) {
   1041     Diag(Id.getLoc(), diag::err_omp_var_scope)
   1042         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
   1043     bool IsDecl =
   1044         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   1045     Diag(VD->getLocation(),
   1046          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   1047         << VD;
   1048     return ExprError();
   1049   }
   1050   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
   1051   //   A threadprivate directive for static class member variables must appear
   1052   //   in the class definition, in the same scope in which the member
   1053   //   variables are declared.
   1054   if (CanonicalVD->isStaticDataMember() &&
   1055       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
   1056     Diag(Id.getLoc(), diag::err_omp_var_scope)
   1057         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
   1058     bool IsDecl =
   1059         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   1060     Diag(VD->getLocation(),
   1061          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   1062         << VD;
   1063     return ExprError();
   1064   }
   1065   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
   1066   //   A threadprivate directive for namespace-scope variables must appear
   1067   //   outside any definition or declaration other than the namespace
   1068   //   definition itself.
   1069   if (CanonicalVD->getDeclContext()->isNamespace() &&
   1070       (!getCurLexicalContext()->isFileContext() ||
   1071        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
   1072     Diag(Id.getLoc(), diag::err_omp_var_scope)
   1073         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
   1074     bool IsDecl =
   1075         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   1076     Diag(VD->getLocation(),
   1077          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   1078         << VD;
   1079     return ExprError();
   1080   }
   1081   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
   1082   //   A threadprivate directive for static block-scope variables must appear
   1083   //   in the scope of the variable and not in a nested scope.
   1084   if (CanonicalVD->isStaticLocal() && CurScope &&
   1085       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
   1086     Diag(Id.getLoc(), diag::err_omp_var_scope)
   1087         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
   1088     bool IsDecl =
   1089         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   1090     Diag(VD->getLocation(),
   1091          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   1092         << VD;
   1093     return ExprError();
   1094   }
   1095 
   1096   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
   1097   //   A threadprivate directive must lexically precede all references to any
   1098   //   of the variables in its list.
   1099   if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
   1100     Diag(Id.getLoc(), diag::err_omp_var_used)
   1101         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
   1102     return ExprError();
   1103   }
   1104 
   1105   QualType ExprType = VD->getType().getNonReferenceType();
   1106   ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
   1107   return DE;
   1108 }
   1109 
   1110 Sema::DeclGroupPtrTy
   1111 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
   1112                                         ArrayRef<Expr *> VarList) {
   1113   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
   1114     CurContext->addDecl(D);
   1115     return DeclGroupPtrTy::make(DeclGroupRef(D));
   1116   }
   1117   return DeclGroupPtrTy();
   1118 }
   1119 
   1120 namespace {
   1121 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
   1122   Sema &SemaRef;
   1123 
   1124 public:
   1125   bool VisitDeclRefExpr(const DeclRefExpr *E) {
   1126     if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
   1127       if (VD->hasLocalStorage()) {
   1128         SemaRef.Diag(E->getLocStart(),
   1129                      diag::err_omp_local_var_in_threadprivate_init)
   1130             << E->getSourceRange();
   1131         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
   1132             << VD << VD->getSourceRange();
   1133         return true;
   1134       }
   1135     }
   1136     return false;
   1137   }
   1138   bool VisitStmt(const Stmt *S) {
   1139     for (auto Child : S->children()) {
   1140       if (Child && Visit(Child))
   1141         return true;
   1142     }
   1143     return false;
   1144   }
   1145   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
   1146 };
   1147 } // namespace
   1148 
   1149 OMPThreadPrivateDecl *
   1150 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
   1151   SmallVector<Expr *, 8> Vars;
   1152   for (auto &RefExpr : VarList) {
   1153     DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
   1154     VarDecl *VD = cast<VarDecl>(DE->getDecl());
   1155     SourceLocation ILoc = DE->getExprLoc();
   1156 
   1157     QualType QType = VD->getType();
   1158     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
   1159       // It will be analyzed later.
   1160       Vars.push_back(DE);
   1161       continue;
   1162     }
   1163 
   1164     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
   1165     //   A threadprivate variable must not have an incomplete type.
   1166     if (RequireCompleteType(ILoc, VD->getType(),
   1167                             diag::err_omp_threadprivate_incomplete_type)) {
   1168       continue;
   1169     }
   1170 
   1171     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
   1172     //   A threadprivate variable must not have a reference type.
   1173     if (VD->getType()->isReferenceType()) {
   1174       Diag(ILoc, diag::err_omp_ref_type_arg)
   1175           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
   1176       bool IsDecl =
   1177           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   1178       Diag(VD->getLocation(),
   1179            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   1180           << VD;
   1181       continue;
   1182     }
   1183 
   1184     // Check if this is a TLS variable. If TLS is not being supported, produce
   1185     // the corresponding diagnostic.
   1186     if ((VD->getTLSKind() != VarDecl::TLS_None &&
   1187          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
   1188            getLangOpts().OpenMPUseTLS &&
   1189            getASTContext().getTargetInfo().isTLSSupported())) ||
   1190         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
   1191          !VD->isLocalVarDecl())) {
   1192       Diag(ILoc, diag::err_omp_var_thread_local)
   1193           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
   1194       bool IsDecl =
   1195           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   1196       Diag(VD->getLocation(),
   1197            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   1198           << VD;
   1199       continue;
   1200     }
   1201 
   1202     // Check if initial value of threadprivate variable reference variable with
   1203     // local storage (it is not supported by runtime).
   1204     if (auto Init = VD->getAnyInitializer()) {
   1205       LocalVarRefChecker Checker(*this);
   1206       if (Checker.Visit(Init))
   1207         continue;
   1208     }
   1209 
   1210     Vars.push_back(RefExpr);
   1211     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
   1212     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
   1213         Context, SourceRange(Loc, Loc)));
   1214     if (auto *ML = Context.getASTMutationListener())
   1215       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
   1216   }
   1217   OMPThreadPrivateDecl *D = nullptr;
   1218   if (!Vars.empty()) {
   1219     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
   1220                                      Vars);
   1221     D->setAccess(AS_public);
   1222   }
   1223   return D;
   1224 }
   1225 
   1226 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
   1227                               const VarDecl *VD, DSAStackTy::DSAVarData DVar,
   1228                               bool IsLoopIterVar = false) {
   1229   if (DVar.RefExpr) {
   1230     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
   1231         << getOpenMPClauseName(DVar.CKind);
   1232     return;
   1233   }
   1234   enum {
   1235     PDSA_StaticMemberShared,
   1236     PDSA_StaticLocalVarShared,
   1237     PDSA_LoopIterVarPrivate,
   1238     PDSA_LoopIterVarLinear,
   1239     PDSA_LoopIterVarLastprivate,
   1240     PDSA_ConstVarShared,
   1241     PDSA_GlobalVarShared,
   1242     PDSA_TaskVarFirstprivate,
   1243     PDSA_LocalVarPrivate,
   1244     PDSA_Implicit
   1245   } Reason = PDSA_Implicit;
   1246   bool ReportHint = false;
   1247   auto ReportLoc = VD->getLocation();
   1248   if (IsLoopIterVar) {
   1249     if (DVar.CKind == OMPC_private)
   1250       Reason = PDSA_LoopIterVarPrivate;
   1251     else if (DVar.CKind == OMPC_lastprivate)
   1252       Reason = PDSA_LoopIterVarLastprivate;
   1253     else
   1254       Reason = PDSA_LoopIterVarLinear;
   1255   } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
   1256     Reason = PDSA_TaskVarFirstprivate;
   1257     ReportLoc = DVar.ImplicitDSALoc;
   1258   } else if (VD->isStaticLocal())
   1259     Reason = PDSA_StaticLocalVarShared;
   1260   else if (VD->isStaticDataMember())
   1261     Reason = PDSA_StaticMemberShared;
   1262   else if (VD->isFileVarDecl())
   1263     Reason = PDSA_GlobalVarShared;
   1264   else if (VD->getType().isConstant(SemaRef.getASTContext()))
   1265     Reason = PDSA_ConstVarShared;
   1266   else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
   1267     ReportHint = true;
   1268     Reason = PDSA_LocalVarPrivate;
   1269   }
   1270   if (Reason != PDSA_Implicit) {
   1271     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
   1272         << Reason << ReportHint
   1273         << getOpenMPDirectiveName(Stack->getCurrentDirective());
   1274   } else if (DVar.ImplicitDSALoc.isValid()) {
   1275     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
   1276         << getOpenMPClauseName(DVar.CKind);
   1277   }
   1278 }
   1279 
   1280 namespace {
   1281 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
   1282   DSAStackTy *Stack;
   1283   Sema &SemaRef;
   1284   bool ErrorFound;
   1285   CapturedStmt *CS;
   1286   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
   1287   llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
   1288 
   1289 public:
   1290   void VisitDeclRefExpr(DeclRefExpr *E) {
   1291     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
   1292       // Skip internally declared variables.
   1293       if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
   1294         return;
   1295 
   1296       auto DVar = Stack->getTopDSA(VD, false);
   1297       // Check if the variable has explicit DSA set and stop analysis if it so.
   1298       if (DVar.RefExpr) return;
   1299 
   1300       auto ELoc = E->getExprLoc();
   1301       auto DKind = Stack->getCurrentDirective();
   1302       // The default(none) clause requires that each variable that is referenced
   1303       // in the construct, and does not have a predetermined data-sharing
   1304       // attribute, must have its data-sharing attribute explicitly determined
   1305       // by being listed in a data-sharing attribute clause.
   1306       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
   1307           isParallelOrTaskRegion(DKind) &&
   1308           VarsWithInheritedDSA.count(VD) == 0) {
   1309         VarsWithInheritedDSA[VD] = E;
   1310         return;
   1311       }
   1312 
   1313       // OpenMP [2.9.3.6, Restrictions, p.2]
   1314       //  A list item that appears in a reduction clause of the innermost
   1315       //  enclosing worksharing or parallel construct may not be accessed in an
   1316       //  explicit task.
   1317       DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
   1318                                     [](OpenMPDirectiveKind K) -> bool {
   1319                                       return isOpenMPParallelDirective(K) ||
   1320                                              isOpenMPWorksharingDirective(K) ||
   1321                                              isOpenMPTeamsDirective(K);
   1322                                     },
   1323                                     false);
   1324       if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
   1325         ErrorFound = true;
   1326         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
   1327         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
   1328         return;
   1329       }
   1330 
   1331       // Define implicit data-sharing attributes for task.
   1332       DVar = Stack->getImplicitDSA(VD, false);
   1333       if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
   1334         ImplicitFirstprivate.push_back(E);
   1335     }
   1336   }
   1337   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
   1338     for (auto *C : S->clauses()) {
   1339       // Skip analysis of arguments of implicitly defined firstprivate clause
   1340       // for task directives.
   1341       if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
   1342         for (auto *CC : C->children()) {
   1343           if (CC)
   1344             Visit(CC);
   1345         }
   1346     }
   1347   }
   1348   void VisitStmt(Stmt *S) {
   1349     for (auto *C : S->children()) {
   1350       if (C && !isa<OMPExecutableDirective>(C))
   1351         Visit(C);
   1352     }
   1353   }
   1354 
   1355   bool isErrorFound() { return ErrorFound; }
   1356   ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
   1357   llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
   1358     return VarsWithInheritedDSA;
   1359   }
   1360 
   1361   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
   1362       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
   1363 };
   1364 } // namespace
   1365 
   1366 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
   1367   switch (DKind) {
   1368   case OMPD_parallel: {
   1369     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
   1370     QualType KmpInt32PtrTy =
   1371         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
   1372     Sema::CapturedParamNameType Params[] = {
   1373         std::make_pair(".global_tid.", KmpInt32PtrTy),
   1374         std::make_pair(".bound_tid.", KmpInt32PtrTy),
   1375         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1376     };
   1377     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1378                              Params);
   1379     break;
   1380   }
   1381   case OMPD_simd: {
   1382     Sema::CapturedParamNameType Params[] = {
   1383         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1384     };
   1385     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1386                              Params);
   1387     break;
   1388   }
   1389   case OMPD_for: {
   1390     Sema::CapturedParamNameType Params[] = {
   1391         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1392     };
   1393     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1394                              Params);
   1395     break;
   1396   }
   1397   case OMPD_for_simd: {
   1398     Sema::CapturedParamNameType Params[] = {
   1399         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1400     };
   1401     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1402                              Params);
   1403     break;
   1404   }
   1405   case OMPD_sections: {
   1406     Sema::CapturedParamNameType Params[] = {
   1407         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1408     };
   1409     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1410                              Params);
   1411     break;
   1412   }
   1413   case OMPD_section: {
   1414     Sema::CapturedParamNameType Params[] = {
   1415         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1416     };
   1417     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1418                              Params);
   1419     break;
   1420   }
   1421   case OMPD_single: {
   1422     Sema::CapturedParamNameType Params[] = {
   1423         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1424     };
   1425     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1426                              Params);
   1427     break;
   1428   }
   1429   case OMPD_master: {
   1430     Sema::CapturedParamNameType Params[] = {
   1431         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1432     };
   1433     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1434                              Params);
   1435     break;
   1436   }
   1437   case OMPD_critical: {
   1438     Sema::CapturedParamNameType Params[] = {
   1439         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1440     };
   1441     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1442                              Params);
   1443     break;
   1444   }
   1445   case OMPD_parallel_for: {
   1446     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
   1447     QualType KmpInt32PtrTy =
   1448         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
   1449     Sema::CapturedParamNameType Params[] = {
   1450         std::make_pair(".global_tid.", KmpInt32PtrTy),
   1451         std::make_pair(".bound_tid.", KmpInt32PtrTy),
   1452         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1453     };
   1454     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1455                              Params);
   1456     break;
   1457   }
   1458   case OMPD_parallel_for_simd: {
   1459     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
   1460     QualType KmpInt32PtrTy =
   1461         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
   1462     Sema::CapturedParamNameType Params[] = {
   1463         std::make_pair(".global_tid.", KmpInt32PtrTy),
   1464         std::make_pair(".bound_tid.", KmpInt32PtrTy),
   1465         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1466     };
   1467     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1468                              Params);
   1469     break;
   1470   }
   1471   case OMPD_parallel_sections: {
   1472     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
   1473     QualType KmpInt32PtrTy =
   1474         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
   1475     Sema::CapturedParamNameType Params[] = {
   1476         std::make_pair(".global_tid.", KmpInt32PtrTy),
   1477         std::make_pair(".bound_tid.", KmpInt32PtrTy),
   1478         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1479     };
   1480     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1481                              Params);
   1482     break;
   1483   }
   1484   case OMPD_task: {
   1485     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
   1486     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
   1487     FunctionProtoType::ExtProtoInfo EPI;
   1488     EPI.Variadic = true;
   1489     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
   1490     Sema::CapturedParamNameType Params[] = {
   1491         std::make_pair(".global_tid.", KmpInt32Ty),
   1492         std::make_pair(".part_id.", KmpInt32Ty),
   1493         std::make_pair(".privates.",
   1494                        Context.VoidPtrTy.withConst().withRestrict()),
   1495         std::make_pair(
   1496             ".copy_fn.",
   1497             Context.getPointerType(CopyFnType).withConst().withRestrict()),
   1498         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1499     };
   1500     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1501                              Params);
   1502     // Mark this captured region as inlined, because we don't use outlined
   1503     // function directly.
   1504     getCurCapturedRegion()->TheCapturedDecl->addAttr(
   1505         AlwaysInlineAttr::CreateImplicit(
   1506             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
   1507     break;
   1508   }
   1509   case OMPD_ordered: {
   1510     Sema::CapturedParamNameType Params[] = {
   1511         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1512     };
   1513     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1514                              Params);
   1515     break;
   1516   }
   1517   case OMPD_atomic: {
   1518     Sema::CapturedParamNameType Params[] = {
   1519         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1520     };
   1521     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1522                              Params);
   1523     break;
   1524   }
   1525   case OMPD_target_data:
   1526   case OMPD_target: {
   1527     Sema::CapturedParamNameType Params[] = {
   1528         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1529     };
   1530     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1531                              Params);
   1532     break;
   1533   }
   1534   case OMPD_teams: {
   1535     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
   1536     QualType KmpInt32PtrTy =
   1537         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
   1538     Sema::CapturedParamNameType Params[] = {
   1539         std::make_pair(".global_tid.", KmpInt32PtrTy),
   1540         std::make_pair(".bound_tid.", KmpInt32PtrTy),
   1541         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1542     };
   1543     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1544                              Params);
   1545     break;
   1546   }
   1547   case OMPD_taskgroup: {
   1548     Sema::CapturedParamNameType Params[] = {
   1549         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1550     };
   1551     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1552                              Params);
   1553     break;
   1554   }
   1555   case OMPD_taskloop: {
   1556     Sema::CapturedParamNameType Params[] = {
   1557         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1558     };
   1559     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1560                              Params);
   1561     break;
   1562   }
   1563   case OMPD_taskloop_simd: {
   1564     Sema::CapturedParamNameType Params[] = {
   1565         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1566     };
   1567     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1568                              Params);
   1569     break;
   1570   }
   1571   case OMPD_distribute: {
   1572     Sema::CapturedParamNameType Params[] = {
   1573         std::make_pair(StringRef(), QualType()) // __context with shared vars
   1574     };
   1575     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
   1576                              Params);
   1577     break;
   1578   }
   1579   case OMPD_threadprivate:
   1580   case OMPD_taskyield:
   1581   case OMPD_barrier:
   1582   case OMPD_taskwait:
   1583   case OMPD_cancellation_point:
   1584   case OMPD_cancel:
   1585   case OMPD_flush:
   1586     llvm_unreachable("OpenMP Directive is not allowed");
   1587   case OMPD_unknown:
   1588     llvm_unreachable("Unknown OpenMP directive");
   1589   }
   1590 }
   1591 
   1592 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
   1593                                       ArrayRef<OMPClause *> Clauses) {
   1594   if (!S.isUsable()) {
   1595     ActOnCapturedRegionError();
   1596     return StmtError();
   1597   }
   1598   // This is required for proper codegen.
   1599   for (auto *Clause : Clauses) {
   1600     if (isOpenMPPrivate(Clause->getClauseKind()) ||
   1601         Clause->getClauseKind() == OMPC_copyprivate ||
   1602         (getLangOpts().OpenMPUseTLS &&
   1603          getASTContext().getTargetInfo().isTLSSupported() &&
   1604          Clause->getClauseKind() == OMPC_copyin)) {
   1605       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
   1606       // Mark all variables in private list clauses as used in inner region.
   1607       for (auto *VarRef : Clause->children()) {
   1608         if (auto *E = cast_or_null<Expr>(VarRef)) {
   1609           MarkDeclarationsReferencedInExpr(E);
   1610         }
   1611       }
   1612       DSAStack->setForceVarCapturing(/*V=*/false);
   1613     } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
   1614                Clause->getClauseKind() == OMPC_schedule) {
   1615       // Mark all variables in private list clauses as used in inner region.
   1616       // Required for proper codegen of combined directives.
   1617       // TODO: add processing for other clauses.
   1618       if (auto *E = cast_or_null<Expr>(
   1619               cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
   1620           MarkDeclarationsReferencedInExpr(E);
   1621         }
   1622     }
   1623   }
   1624   return ActOnCapturedRegionEnd(S.get());
   1625 }
   1626 
   1627 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
   1628                                   OpenMPDirectiveKind CurrentRegion,
   1629                                   const DeclarationNameInfo &CurrentName,
   1630                                   OpenMPDirectiveKind CancelRegion,
   1631                                   SourceLocation StartLoc) {
   1632   // Allowed nesting of constructs
   1633   // +------------------+-----------------+------------------------------------+
   1634   // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
   1635   // +------------------+-----------------+------------------------------------+
   1636   // | parallel         | parallel        | *                                  |
   1637   // | parallel         | for             | *                                  |
   1638   // | parallel         | for simd        | *                                  |
   1639   // | parallel         | master          | *                                  |
   1640   // | parallel         | critical        | *                                  |
   1641   // | parallel         | simd            | *                                  |
   1642   // | parallel         | sections        | *                                  |
   1643   // | parallel         | section         | +                                  |
   1644   // | parallel         | single          | *                                  |
   1645   // | parallel         | parallel for    | *                                  |
   1646   // | parallel         |parallel for simd| *                                  |
   1647   // | parallel         |parallel sections| *                                  |
   1648   // | parallel         | task            | *                                  |
   1649   // | parallel         | taskyield       | *                                  |
   1650   // | parallel         | barrier         | *                                  |
   1651   // | parallel         | taskwait        | *                                  |
   1652   // | parallel         | taskgroup       | *                                  |
   1653   // | parallel         | flush           | *                                  |
   1654   // | parallel         | ordered         | +                                  |
   1655   // | parallel         | atomic          | *                                  |
   1656   // | parallel         | target          | *                                  |
   1657   // | parallel         | teams           | +                                  |
   1658   // | parallel         | cancellation    |                                    |
   1659   // |                  | point           | !                                  |
   1660   // | parallel         | cancel          | !                                  |
   1661   // | parallel         | taskloop        | *                                  |
   1662   // | parallel         | taskloop simd   | *                                  |
   1663   // | parallel         | distribute      |                                    |
   1664   // +------------------+-----------------+------------------------------------+
   1665   // | for              | parallel        | *                                  |
   1666   // | for              | for             | +                                  |
   1667   // | for              | for simd        | +                                  |
   1668   // | for              | master          | +                                  |
   1669   // | for              | critical        | *                                  |
   1670   // | for              | simd            | *                                  |
   1671   // | for              | sections        | +                                  |
   1672   // | for              | section         | +                                  |
   1673   // | for              | single          | +                                  |
   1674   // | for              | parallel for    | *                                  |
   1675   // | for              |parallel for simd| *                                  |
   1676   // | for              |parallel sections| *                                  |
   1677   // | for              | task            | *                                  |
   1678   // | for              | taskyield       | *                                  |
   1679   // | for              | barrier         | +                                  |
   1680   // | for              | taskwait        | *                                  |
   1681   // | for              | taskgroup       | *                                  |
   1682   // | for              | flush           | *                                  |
   1683   // | for              | ordered         | * (if construct is ordered)        |
   1684   // | for              | atomic          | *                                  |
   1685   // | for              | target          | *                                  |
   1686   // | for              | teams           | +                                  |
   1687   // | for              | cancellation    |                                    |
   1688   // |                  | point           | !                                  |
   1689   // | for              | cancel          | !                                  |
   1690   // | for              | taskloop        | *                                  |
   1691   // | for              | taskloop simd   | *                                  |
   1692   // | for              | distribute      |                                    |
   1693   // +------------------+-----------------+------------------------------------+
   1694   // | master           | parallel        | *                                  |
   1695   // | master           | for             | +                                  |
   1696   // | master           | for simd        | +                                  |
   1697   // | master           | master          | *                                  |
   1698   // | master           | critical        | *                                  |
   1699   // | master           | simd            | *                                  |
   1700   // | master           | sections        | +                                  |
   1701   // | master           | section         | +                                  |
   1702   // | master           | single          | +                                  |
   1703   // | master           | parallel for    | *                                  |
   1704   // | master           |parallel for simd| *                                  |
   1705   // | master           |parallel sections| *                                  |
   1706   // | master           | task            | *                                  |
   1707   // | master           | taskyield       | *                                  |
   1708   // | master           | barrier         | +                                  |
   1709   // | master           | taskwait        | *                                  |
   1710   // | master           | taskgroup       | *                                  |
   1711   // | master           | flush           | *                                  |
   1712   // | master           | ordered         | +                                  |
   1713   // | master           | atomic          | *                                  |
   1714   // | master           | target          | *                                  |
   1715   // | master           | teams           | +                                  |
   1716   // | master           | cancellation    |                                    |
   1717   // |                  | point           |                                    |
   1718   // | master           | cancel          |                                    |
   1719   // | master           | taskloop        | *                                  |
   1720   // | master           | taskloop simd   | *                                  |
   1721   // | master           | distribute      |                                    |
   1722   // +------------------+-----------------+------------------------------------+
   1723   // | critical         | parallel        | *                                  |
   1724   // | critical         | for             | +                                  |
   1725   // | critical         | for simd        | +                                  |
   1726   // | critical         | master          | *                                  |
   1727   // | critical         | critical        | * (should have different names)    |
   1728   // | critical         | simd            | *                                  |
   1729   // | critical         | sections        | +                                  |
   1730   // | critical         | section         | +                                  |
   1731   // | critical         | single          | +                                  |
   1732   // | critical         | parallel for    | *                                  |
   1733   // | critical         |parallel for simd| *                                  |
   1734   // | critical         |parallel sections| *                                  |
   1735   // | critical         | task            | *                                  |
   1736   // | critical         | taskyield       | *                                  |
   1737   // | critical         | barrier         | +                                  |
   1738   // | critical         | taskwait        | *                                  |
   1739   // | critical         | taskgroup       | *                                  |
   1740   // | critical         | ordered         | +                                  |
   1741   // | critical         | atomic          | *                                  |
   1742   // | critical         | target          | *                                  |
   1743   // | critical         | teams           | +                                  |
   1744   // | critical         | cancellation    |                                    |
   1745   // |                  | point           |                                    |
   1746   // | critical         | cancel          |                                    |
   1747   // | critical         | taskloop        | *                                  |
   1748   // | critical         | taskloop simd   | *                                  |
   1749   // | critical         | distribute      |                                    |
   1750   // +------------------+-----------------+------------------------------------+
   1751   // | simd             | parallel        |                                    |
   1752   // | simd             | for             |                                    |
   1753   // | simd             | for simd        |                                    |
   1754   // | simd             | master          |                                    |
   1755   // | simd             | critical        |                                    |
   1756   // | simd             | simd            |                                    |
   1757   // | simd             | sections        |                                    |
   1758   // | simd             | section         |                                    |
   1759   // | simd             | single          |                                    |
   1760   // | simd             | parallel for    |                                    |
   1761   // | simd             |parallel for simd|                                    |
   1762   // | simd             |parallel sections|                                    |
   1763   // | simd             | task            |                                    |
   1764   // | simd             | taskyield       |                                    |
   1765   // | simd             | barrier         |                                    |
   1766   // | simd             | taskwait        |                                    |
   1767   // | simd             | taskgroup       |                                    |
   1768   // | simd             | flush           |                                    |
   1769   // | simd             | ordered         | + (with simd clause)               |
   1770   // | simd             | atomic          |                                    |
   1771   // | simd             | target          |                                    |
   1772   // | simd             | teams           |                                    |
   1773   // | simd             | cancellation    |                                    |
   1774   // |                  | point           |                                    |
   1775   // | simd             | cancel          |                                    |
   1776   // | simd             | taskloop        |                                    |
   1777   // | simd             | taskloop simd   |                                    |
   1778   // | simd             | distribute      |                                    |
   1779   // +------------------+-----------------+------------------------------------+
   1780   // | for simd         | parallel        |                                    |
   1781   // | for simd         | for             |                                    |
   1782   // | for simd         | for simd        |                                    |
   1783   // | for simd         | master          |                                    |
   1784   // | for simd         | critical        |                                    |
   1785   // | for simd         | simd            |                                    |
   1786   // | for simd         | sections        |                                    |
   1787   // | for simd         | section         |                                    |
   1788   // | for simd         | single          |                                    |
   1789   // | for simd         | parallel for    |                                    |
   1790   // | for simd         |parallel for simd|                                    |
   1791   // | for simd         |parallel sections|                                    |
   1792   // | for simd         | task            |                                    |
   1793   // | for simd         | taskyield       |                                    |
   1794   // | for simd         | barrier         |                                    |
   1795   // | for simd         | taskwait        |                                    |
   1796   // | for simd         | taskgroup       |                                    |
   1797   // | for simd         | flush           |                                    |
   1798   // | for simd         | ordered         | + (with simd clause)               |
   1799   // | for simd         | atomic          |                                    |
   1800   // | for simd         | target          |                                    |
   1801   // | for simd         | teams           |                                    |
   1802   // | for simd         | cancellation    |                                    |
   1803   // |                  | point           |                                    |
   1804   // | for simd         | cancel          |                                    |
   1805   // | for simd         | taskloop        |                                    |
   1806   // | for simd         | taskloop simd   |                                    |
   1807   // | for simd         | distribute      |                                    |
   1808   // +------------------+-----------------+------------------------------------+
   1809   // | parallel for simd| parallel        |                                    |
   1810   // | parallel for simd| for             |                                    |
   1811   // | parallel for simd| for simd        |                                    |
   1812   // | parallel for simd| master          |                                    |
   1813   // | parallel for simd| critical        |                                    |
   1814   // | parallel for simd| simd            |                                    |
   1815   // | parallel for simd| sections        |                                    |
   1816   // | parallel for simd| section         |                                    |
   1817   // | parallel for simd| single          |                                    |
   1818   // | parallel for simd| parallel for    |                                    |
   1819   // | parallel for simd|parallel for simd|                                    |
   1820   // | parallel for simd|parallel sections|                                    |
   1821   // | parallel for simd| task            |                                    |
   1822   // | parallel for simd| taskyield       |                                    |
   1823   // | parallel for simd| barrier         |                                    |
   1824   // | parallel for simd| taskwait        |                                    |
   1825   // | parallel for simd| taskgroup       |                                    |
   1826   // | parallel for simd| flush           |                                    |
   1827   // | parallel for simd| ordered         | + (with simd clause)               |
   1828   // | parallel for simd| atomic          |                                    |
   1829   // | parallel for simd| target          |                                    |
   1830   // | parallel for simd| teams           |                                    |
   1831   // | parallel for simd| cancellation    |                                    |
   1832   // |                  | point           |                                    |
   1833   // | parallel for simd| cancel          |                                    |
   1834   // | parallel for simd| taskloop        |                                    |
   1835   // | parallel for simd| taskloop simd   |                                    |
   1836   // | parallel for simd| distribute      |                                    |
   1837   // +------------------+-----------------+------------------------------------+
   1838   // | sections         | parallel        | *                                  |
   1839   // | sections         | for             | +                                  |
   1840   // | sections         | for simd        | +                                  |
   1841   // | sections         | master          | +                                  |
   1842   // | sections         | critical        | *                                  |
   1843   // | sections         | simd            | *                                  |
   1844   // | sections         | sections        | +                                  |
   1845   // | sections         | section         | *                                  |
   1846   // | sections         | single          | +                                  |
   1847   // | sections         | parallel for    | *                                  |
   1848   // | sections         |parallel for simd| *                                  |
   1849   // | sections         |parallel sections| *                                  |
   1850   // | sections         | task            | *                                  |
   1851   // | sections         | taskyield       | *                                  |
   1852   // | sections         | barrier         | +                                  |
   1853   // | sections         | taskwait        | *                                  |
   1854   // | sections         | taskgroup       | *                                  |
   1855   // | sections         | flush           | *                                  |
   1856   // | sections         | ordered         | +                                  |
   1857   // | sections         | atomic          | *                                  |
   1858   // | sections         | target          | *                                  |
   1859   // | sections         | teams           | +                                  |
   1860   // | sections         | cancellation    |                                    |
   1861   // |                  | point           | !                                  |
   1862   // | sections         | cancel          | !                                  |
   1863   // | sections         | taskloop        | *                                  |
   1864   // | sections         | taskloop simd   | *                                  |
   1865   // | sections         | distribute      |                                    |
   1866   // +------------------+-----------------+------------------------------------+
   1867   // | section          | parallel        | *                                  |
   1868   // | section          | for             | +                                  |
   1869   // | section          | for simd        | +                                  |
   1870   // | section          | master          | +                                  |
   1871   // | section          | critical        | *                                  |
   1872   // | section          | simd            | *                                  |
   1873   // | section          | sections        | +                                  |
   1874   // | section          | section         | +                                  |
   1875   // | section          | single          | +                                  |
   1876   // | section          | parallel for    | *                                  |
   1877   // | section          |parallel for simd| *                                  |
   1878   // | section          |parallel sections| *                                  |
   1879   // | section          | task            | *                                  |
   1880   // | section          | taskyield       | *                                  |
   1881   // | section          | barrier         | +                                  |
   1882   // | section          | taskwait        | *                                  |
   1883   // | section          | taskgroup       | *                                  |
   1884   // | section          | flush           | *                                  |
   1885   // | section          | ordered         | +                                  |
   1886   // | section          | atomic          | *                                  |
   1887   // | section          | target          | *                                  |
   1888   // | section          | teams           | +                                  |
   1889   // | section          | cancellation    |                                    |
   1890   // |                  | point           | !                                  |
   1891   // | section          | cancel          | !                                  |
   1892   // | section          | taskloop        | *                                  |
   1893   // | section          | taskloop simd   | *                                  |
   1894   // | section          | distribute      |                                    |
   1895   // +------------------+-----------------+------------------------------------+
   1896   // | single           | parallel        | *                                  |
   1897   // | single           | for             | +                                  |
   1898   // | single           | for simd        | +                                  |
   1899   // | single           | master          | +                                  |
   1900   // | single           | critical        | *                                  |
   1901   // | single           | simd            | *                                  |
   1902   // | single           | sections        | +                                  |
   1903   // | single           | section         | +                                  |
   1904   // | single           | single          | +                                  |
   1905   // | single           | parallel for    | *                                  |
   1906   // | single           |parallel for simd| *                                  |
   1907   // | single           |parallel sections| *                                  |
   1908   // | single           | task            | *                                  |
   1909   // | single           | taskyield       | *                                  |
   1910   // | single           | barrier         | +                                  |
   1911   // | single           | taskwait        | *                                  |
   1912   // | single           | taskgroup       | *                                  |
   1913   // | single           | flush           | *                                  |
   1914   // | single           | ordered         | +                                  |
   1915   // | single           | atomic          | *                                  |
   1916   // | single           | target          | *                                  |
   1917   // | single           | teams           | +                                  |
   1918   // | single           | cancellation    |                                    |
   1919   // |                  | point           |                                    |
   1920   // | single           | cancel          |                                    |
   1921   // | single           | taskloop        | *                                  |
   1922   // | single           | taskloop simd   | *                                  |
   1923   // | single           | distribute      |                                    |
   1924   // +------------------+-----------------+------------------------------------+
   1925   // | parallel for     | parallel        | *                                  |
   1926   // | parallel for     | for             | +                                  |
   1927   // | parallel for     | for simd        | +                                  |
   1928   // | parallel for     | master          | +                                  |
   1929   // | parallel for     | critical        | *                                  |
   1930   // | parallel for     | simd            | *                                  |
   1931   // | parallel for     | sections        | +                                  |
   1932   // | parallel for     | section         | +                                  |
   1933   // | parallel for     | single          | +                                  |
   1934   // | parallel for     | parallel for    | *                                  |
   1935   // | parallel for     |parallel for simd| *                                  |
   1936   // | parallel for     |parallel sections| *                                  |
   1937   // | parallel for     | task            | *                                  |
   1938   // | parallel for     | taskyield       | *                                  |
   1939   // | parallel for     | barrier         | +                                  |
   1940   // | parallel for     | taskwait        | *                                  |
   1941   // | parallel for     | taskgroup       | *                                  |
   1942   // | parallel for     | flush           | *                                  |
   1943   // | parallel for     | ordered         | * (if construct is ordered)        |
   1944   // | parallel for     | atomic          | *                                  |
   1945   // | parallel for     | target          | *                                  |
   1946   // | parallel for     | teams           | +                                  |
   1947   // | parallel for     | cancellation    |                                    |
   1948   // |                  | point           | !                                  |
   1949   // | parallel for     | cancel          | !                                  |
   1950   // | parallel for     | taskloop        | *                                  |
   1951   // | parallel for     | taskloop simd   | *                                  |
   1952   // | parallel for     | distribute      |                                    |
   1953   // +------------------+-----------------+------------------------------------+
   1954   // | parallel sections| parallel        | *                                  |
   1955   // | parallel sections| for             | +                                  |
   1956   // | parallel sections| for simd        | +                                  |
   1957   // | parallel sections| master          | +                                  |
   1958   // | parallel sections| critical        | +                                  |
   1959   // | parallel sections| simd            | *                                  |
   1960   // | parallel sections| sections        | +                                  |
   1961   // | parallel sections| section         | *                                  |
   1962   // | parallel sections| single          | +                                  |
   1963   // | parallel sections| parallel for    | *                                  |
   1964   // | parallel sections|parallel for simd| *                                  |
   1965   // | parallel sections|parallel sections| *                                  |
   1966   // | parallel sections| task            | *                                  |
   1967   // | parallel sections| taskyield       | *                                  |
   1968   // | parallel sections| barrier         | +                                  |
   1969   // | parallel sections| taskwait        | *                                  |
   1970   // | parallel sections| taskgroup       | *                                  |
   1971   // | parallel sections| flush           | *                                  |
   1972   // | parallel sections| ordered         | +                                  |
   1973   // | parallel sections| atomic          | *                                  |
   1974   // | parallel sections| target          | *                                  |
   1975   // | parallel sections| teams           | +                                  |
   1976   // | parallel sections| cancellation    |                                    |
   1977   // |                  | point           | !                                  |
   1978   // | parallel sections| cancel          | !                                  |
   1979   // | parallel sections| taskloop        | *                                  |
   1980   // | parallel sections| taskloop simd   | *                                  |
   1981   // | parallel sections| distribute      |                                    |
   1982   // +------------------+-----------------+------------------------------------+
   1983   // | task             | parallel        | *                                  |
   1984   // | task             | for             | +                                  |
   1985   // | task             | for simd        | +                                  |
   1986   // | task             | master          | +                                  |
   1987   // | task             | critical        | *                                  |
   1988   // | task             | simd            | *                                  |
   1989   // | task             | sections        | +                                  |
   1990   // | task             | section         | +                                  |
   1991   // | task             | single          | +                                  |
   1992   // | task             | parallel for    | *                                  |
   1993   // | task             |parallel for simd| *                                  |
   1994   // | task             |parallel sections| *                                  |
   1995   // | task             | task            | *                                  |
   1996   // | task             | taskyield       | *                                  |
   1997   // | task             | barrier         | +                                  |
   1998   // | task             | taskwait        | *                                  |
   1999   // | task             | taskgroup       | *                                  |
   2000   // | task             | flush           | *                                  |
   2001   // | task             | ordered         | +                                  |
   2002   // | task             | atomic          | *                                  |
   2003   // | task             | target          | *                                  |
   2004   // | task             | teams           | +                                  |
   2005   // | task             | cancellation    |                                    |
   2006   // |                  | point           | !                                  |
   2007   // | task             | cancel          | !                                  |
   2008   // | task             | taskloop        | *                                  |
   2009   // | task             | taskloop simd   | *                                  |
   2010   // | task             | distribute      |                                    |
   2011   // +------------------+-----------------+------------------------------------+
   2012   // | ordered          | parallel        | *                                  |
   2013   // | ordered          | for             | +                                  |
   2014   // | ordered          | for simd        | +                                  |
   2015   // | ordered          | master          | *                                  |
   2016   // | ordered          | critical        | *                                  |
   2017   // | ordered          | simd            | *                                  |
   2018   // | ordered          | sections        | +                                  |
   2019   // | ordered          | section         | +                                  |
   2020   // | ordered          | single          | +                                  |
   2021   // | ordered          | parallel for    | *                                  |
   2022   // | ordered          |parallel for simd| *                                  |
   2023   // | ordered          |parallel sections| *                                  |
   2024   // | ordered          | task            | *                                  |
   2025   // | ordered          | taskyield       | *                                  |
   2026   // | ordered          | barrier         | +                                  |
   2027   // | ordered          | taskwait        | *                                  |
   2028   // | ordered          | taskgroup       | *                                  |
   2029   // | ordered          | flush           | *                                  |
   2030   // | ordered          | ordered         | +                                  |
   2031   // | ordered          | atomic          | *                                  |
   2032   // | ordered          | target          | *                                  |
   2033   // | ordered          | teams           | +                                  |
   2034   // | ordered          | cancellation    |                                    |
   2035   // |                  | point           |                                    |
   2036   // | ordered          | cancel          |                                    |
   2037   // | ordered          | taskloop        | *                                  |
   2038   // | ordered          | taskloop simd   | *                                  |
   2039   // | ordered          | distribute      |                                    |
   2040   // +------------------+-----------------+------------------------------------+
   2041   // | atomic           | parallel        |                                    |
   2042   // | atomic           | for             |                                    |
   2043   // | atomic           | for simd        |                                    |
   2044   // | atomic           | master          |                                    |
   2045   // | atomic           | critical        |                                    |
   2046   // | atomic           | simd            |                                    |
   2047   // | atomic           | sections        |                                    |
   2048   // | atomic           | section         |                                    |
   2049   // | atomic           | single          |                                    |
   2050   // | atomic           | parallel for    |                                    |
   2051   // | atomic           |parallel for simd|                                    |
   2052   // | atomic           |parallel sections|                                    |
   2053   // | atomic           | task            |                                    |
   2054   // | atomic           | taskyield       |                                    |
   2055   // | atomic           | barrier         |                                    |
   2056   // | atomic           | taskwait        |                                    |
   2057   // | atomic           | taskgroup       |                                    |
   2058   // | atomic           | flush           |                                    |
   2059   // | atomic           | ordered         |                                    |
   2060   // | atomic           | atomic          |                                    |
   2061   // | atomic           | target          |                                    |
   2062   // | atomic           | teams           |                                    |
   2063   // | atomic           | cancellation    |                                    |
   2064   // |                  | point           |                                    |
   2065   // | atomic           | cancel          |                                    |
   2066   // | atomic           | taskloop        |                                    |
   2067   // | atomic           | taskloop simd   |                                    |
   2068   // | atomic           | distribute      |                                    |
   2069   // +------------------+-----------------+------------------------------------+
   2070   // | target           | parallel        | *                                  |
   2071   // | target           | for             | *                                  |
   2072   // | target           | for simd        | *                                  |
   2073   // | target           | master          | *                                  |
   2074   // | target           | critical        | *                                  |
   2075   // | target           | simd            | *                                  |
   2076   // | target           | sections        | *                                  |
   2077   // | target           | section         | *                                  |
   2078   // | target           | single          | *                                  |
   2079   // | target           | parallel for    | *                                  |
   2080   // | target           |parallel for simd| *                                  |
   2081   // | target           |parallel sections| *                                  |
   2082   // | target           | task            | *                                  |
   2083   // | target           | taskyield       | *                                  |
   2084   // | target           | barrier         | *                                  |
   2085   // | target           | taskwait        | *                                  |
   2086   // | target           | taskgroup       | *                                  |
   2087   // | target           | flush           | *                                  |
   2088   // | target           | ordered         | *                                  |
   2089   // | target           | atomic          | *                                  |
   2090   // | target           | target          | *                                  |
   2091   // | target           | teams           | *                                  |
   2092   // | target           | cancellation    |                                    |
   2093   // |                  | point           |                                    |
   2094   // | target           | cancel          |                                    |
   2095   // | target           | taskloop        | *                                  |
   2096   // | target           | taskloop simd   | *                                  |
   2097   // | target           | distribute      |                                    |
   2098   // +------------------+-----------------+------------------------------------+
   2099   // | teams            | parallel        | *                                  |
   2100   // | teams            | for             | +                                  |
   2101   // | teams            | for simd        | +                                  |
   2102   // | teams            | master          | +                                  |
   2103   // | teams            | critical        | +                                  |
   2104   // | teams            | simd            | +                                  |
   2105   // | teams            | sections        | +                                  |
   2106   // | teams            | section         | +                                  |
   2107   // | teams            | single          | +                                  |
   2108   // | teams            | parallel for    | *                                  |
   2109   // | teams            |parallel for simd| *                                  |
   2110   // | teams            |parallel sections| *                                  |
   2111   // | teams            | task            | +                                  |
   2112   // | teams            | taskyield       | +                                  |
   2113   // | teams            | barrier         | +                                  |
   2114   // | teams            | taskwait        | +                                  |
   2115   // | teams            | taskgroup       | +                                  |
   2116   // | teams            | flush           | +                                  |
   2117   // | teams            | ordered         | +                                  |
   2118   // | teams            | atomic          | +                                  |
   2119   // | teams            | target          | +                                  |
   2120   // | teams            | teams           | +                                  |
   2121   // | teams            | cancellation    |                                    |
   2122   // |                  | point           |                                    |
   2123   // | teams            | cancel          |                                    |
   2124   // | teams            | taskloop        | +                                  |
   2125   // | teams            | taskloop simd   | +                                  |
   2126   // | teams            | distribute      | !                                  |
   2127   // +------------------+-----------------+------------------------------------+
   2128   // | taskloop         | parallel        | *                                  |
   2129   // | taskloop         | for             | +                                  |
   2130   // | taskloop         | for simd        | +                                  |
   2131   // | taskloop         | master          | +                                  |
   2132   // | taskloop         | critical        | *                                  |
   2133   // | taskloop         | simd            | *                                  |
   2134   // | taskloop         | sections        | +                                  |
   2135   // | taskloop         | section         | +                                  |
   2136   // | taskloop         | single          | +                                  |
   2137   // | taskloop         | parallel for    | *                                  |
   2138   // | taskloop         |parallel for simd| *                                  |
   2139   // | taskloop         |parallel sections| *                                  |
   2140   // | taskloop         | task            | *                                  |
   2141   // | taskloop         | taskyield       | *                                  |
   2142   // | taskloop         | barrier         | +                                  |
   2143   // | taskloop         | taskwait        | *                                  |
   2144   // | taskloop         | taskgroup       | *                                  |
   2145   // | taskloop         | flush           | *                                  |
   2146   // | taskloop         | ordered         | +                                  |
   2147   // | taskloop         | atomic          | *                                  |
   2148   // | taskloop         | target          | *                                  |
   2149   // | taskloop         | teams           | +                                  |
   2150   // | taskloop         | cancellation    |                                    |
   2151   // |                  | point           |                                    |
   2152   // | taskloop         | cancel          |                                    |
   2153   // | taskloop         | taskloop        | *                                  |
   2154   // | taskloop         | distribute      |                                    |
   2155   // +------------------+-----------------+------------------------------------+
   2156   // | taskloop simd    | parallel        |                                    |
   2157   // | taskloop simd    | for             |                                    |
   2158   // | taskloop simd    | for simd        |                                    |
   2159   // | taskloop simd    | master          |                                    |
   2160   // | taskloop simd    | critical        |                                    |
   2161   // | taskloop simd    | simd            |                                    |
   2162   // | taskloop simd    | sections        |                                    |
   2163   // | taskloop simd    | section         |                                    |
   2164   // | taskloop simd    | single          |                                    |
   2165   // | taskloop simd    | parallel for    |                                    |
   2166   // | taskloop simd    |parallel for simd|                                    |
   2167   // | taskloop simd    |parallel sections|                                    |
   2168   // | taskloop simd    | task            |                                    |
   2169   // | taskloop simd    | taskyield       |                                    |
   2170   // | taskloop simd    | barrier         |                                    |
   2171   // | taskloop simd    | taskwait        |                                    |
   2172   // | taskloop simd    | taskgroup       |                                    |
   2173   // | taskloop simd    | flush           |                                    |
   2174   // | taskloop simd    | ordered         | + (with simd clause)               |
   2175   // | taskloop simd    | atomic          |                                    |
   2176   // | taskloop simd    | target          |                                    |
   2177   // | taskloop simd    | teams           |                                    |
   2178   // | taskloop simd    | cancellation    |                                    |
   2179   // |                  | point           |                                    |
   2180   // | taskloop simd    | cancel          |                                    |
   2181   // | taskloop simd    | taskloop        |                                    |
   2182   // | taskloop simd    | taskloop simd   |                                    |
   2183   // | taskloop simd    | distribute      |                                    |
   2184   // +------------------+-----------------+------------------------------------+
   2185   // | distribute       | parallel        | *                                  |
   2186   // | distribute       | for             | *                                  |
   2187   // | distribute       | for simd        | *                                  |
   2188   // | distribute       | master          | *                                  |
   2189   // | distribute       | critical        | *                                  |
   2190   // | distribute       | simd            | *                                  |
   2191   // | distribute       | sections        | *                                  |
   2192   // | distribute       | section         | *                                  |
   2193   // | distribute       | single          | *                                  |
   2194   // | distribute       | parallel for    | *                                  |
   2195   // | distribute       |parallel for simd| *                                  |
   2196   // | distribute       |parallel sections| *                                  |
   2197   // | distribute       | task            | *                                  |
   2198   // | distribute       | taskyield       | *                                  |
   2199   // | distribute       | barrier         | *                                  |
   2200   // | distribute       | taskwait        | *                                  |
   2201   // | distribute       | taskgroup       | *                                  |
   2202   // | distribute       | flush           | *                                  |
   2203   // | distribute       | ordered         | +                                  |
   2204   // | distribute       | atomic          | *                                  |
   2205   // | distribute       | target          |                                    |
   2206   // | distribute       | teams           |                                    |
   2207   // | distribute       | cancellation    | +                                  |
   2208   // |                  | point           |                                    |
   2209   // | distribute       | cancel          | +                                  |
   2210   // | distribute       | taskloop        | *                                  |
   2211   // | distribute       | taskloop simd   | *                                  |
   2212   // | distribute       | distribute      |                                    |
   2213   // +------------------+-----------------+------------------------------------+
   2214   if (Stack->getCurScope()) {
   2215     auto ParentRegion = Stack->getParentDirective();
   2216     bool NestingProhibited = false;
   2217     bool CloseNesting = true;
   2218     enum {
   2219       NoRecommend,
   2220       ShouldBeInParallelRegion,
   2221       ShouldBeInOrderedRegion,
   2222       ShouldBeInTargetRegion,
   2223       ShouldBeInTeamsRegion
   2224     } Recommend = NoRecommend;
   2225     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
   2226       // OpenMP [2.16, Nesting of Regions]
   2227       // OpenMP constructs may not be nested inside a simd region.
   2228       // OpenMP [2.8.1,simd Construct, Restrictions]
   2229       // An ordered construct with the simd clause is the only OpenMP construct
   2230       // that can appear in the simd region.
   2231       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
   2232       return true;
   2233     }
   2234     if (ParentRegion == OMPD_atomic) {
   2235       // OpenMP [2.16, Nesting of Regions]
   2236       // OpenMP constructs may not be nested inside an atomic region.
   2237       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
   2238       return true;
   2239     }
   2240     if (CurrentRegion == OMPD_section) {
   2241       // OpenMP [2.7.2, sections Construct, Restrictions]
   2242       // Orphaned section directives are prohibited. That is, the section
   2243       // directives must appear within the sections construct and must not be
   2244       // encountered elsewhere in the sections region.
   2245       if (ParentRegion != OMPD_sections &&
   2246           ParentRegion != OMPD_parallel_sections) {
   2247         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
   2248             << (ParentRegion != OMPD_unknown)
   2249             << getOpenMPDirectiveName(ParentRegion);
   2250         return true;
   2251       }
   2252       return false;
   2253     }
   2254     // Allow some constructs to be orphaned (they could be used in functions,
   2255     // called from OpenMP regions with the required preconditions).
   2256     if (ParentRegion == OMPD_unknown)
   2257       return false;
   2258     if (CurrentRegion == OMPD_cancellation_point ||
   2259         CurrentRegion == OMPD_cancel) {
   2260       // OpenMP [2.16, Nesting of Regions]
   2261       // A cancellation point construct for which construct-type-clause is
   2262       // taskgroup must be nested inside a task construct. A cancellation
   2263       // point construct for which construct-type-clause is not taskgroup must
   2264       // be closely nested inside an OpenMP construct that matches the type
   2265       // specified in construct-type-clause.
   2266       // A cancel construct for which construct-type-clause is taskgroup must be
   2267       // nested inside a task construct. A cancel construct for which
   2268       // construct-type-clause is not taskgroup must be closely nested inside an
   2269       // OpenMP construct that matches the type specified in
   2270       // construct-type-clause.
   2271       NestingProhibited =
   2272           !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
   2273             (CancelRegion == OMPD_for &&
   2274              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
   2275             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
   2276             (CancelRegion == OMPD_sections &&
   2277              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
   2278               ParentRegion == OMPD_parallel_sections)));
   2279     } else if (CurrentRegion == OMPD_master) {
   2280       // OpenMP [2.16, Nesting of Regions]
   2281       // A master region may not be closely nested inside a worksharing,
   2282       // atomic, or explicit task region.
   2283       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
   2284                           ParentRegion == OMPD_task ||
   2285                           isOpenMPTaskLoopDirective(ParentRegion);
   2286     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
   2287       // OpenMP [2.16, Nesting of Regions]
   2288       // A critical region may not be nested (closely or otherwise) inside a
   2289       // critical region with the same name. Note that this restriction is not
   2290       // sufficient to prevent deadlock.
   2291       SourceLocation PreviousCriticalLoc;
   2292       bool DeadLock =
   2293           Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
   2294                                   OpenMPDirectiveKind K,
   2295                                   const DeclarationNameInfo &DNI,
   2296                                   SourceLocation Loc)
   2297                                   ->bool {
   2298                                 if (K == OMPD_critical &&
   2299                                     DNI.getName() == CurrentName.getName()) {
   2300                                   PreviousCriticalLoc = Loc;
   2301                                   return true;
   2302                                 } else
   2303                                   return false;
   2304                               },
   2305                               false /* skip top directive */);
   2306       if (DeadLock) {
   2307         SemaRef.Diag(StartLoc,
   2308                      diag::err_omp_prohibited_region_critical_same_name)
   2309             << CurrentName.getName();
   2310         if (PreviousCriticalLoc.isValid())
   2311           SemaRef.Diag(PreviousCriticalLoc,
   2312                        diag::note_omp_previous_critical_region);
   2313         return true;
   2314       }
   2315     } else if (CurrentRegion == OMPD_barrier) {
   2316       // OpenMP [2.16, Nesting of Regions]
   2317       // A barrier region may not be closely nested inside a worksharing,
   2318       // explicit task, critical, ordered, atomic, or master region.
   2319       NestingProhibited =
   2320           isOpenMPWorksharingDirective(ParentRegion) ||
   2321           ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
   2322           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
   2323           isOpenMPTaskLoopDirective(ParentRegion);
   2324     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
   2325                !isOpenMPParallelDirective(CurrentRegion)) {
   2326       // OpenMP [2.16, Nesting of Regions]
   2327       // A worksharing region may not be closely nested inside a worksharing,
   2328       // explicit task, critical, ordered, atomic, or master region.
   2329       NestingProhibited =
   2330           isOpenMPWorksharingDirective(ParentRegion) ||
   2331           ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
   2332           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
   2333           isOpenMPTaskLoopDirective(ParentRegion);
   2334       Recommend = ShouldBeInParallelRegion;
   2335     } else if (CurrentRegion == OMPD_ordered) {
   2336       // OpenMP [2.16, Nesting of Regions]
   2337       // An ordered region may not be closely nested inside a critical,
   2338       // atomic, or explicit task region.
   2339       // An ordered region must be closely nested inside a loop region (or
   2340       // parallel loop region) with an ordered clause.
   2341       // OpenMP [2.8.1,simd Construct, Restrictions]
   2342       // An ordered construct with the simd clause is the only OpenMP construct
   2343       // that can appear in the simd region.
   2344       NestingProhibited = ParentRegion == OMPD_critical ||
   2345                           ParentRegion == OMPD_task ||
   2346                           isOpenMPTaskLoopDirective(ParentRegion) ||
   2347                           !(isOpenMPSimdDirective(ParentRegion) ||
   2348                             Stack->isParentOrderedRegion());
   2349       Recommend = ShouldBeInOrderedRegion;
   2350     } else if (isOpenMPTeamsDirective(CurrentRegion)) {
   2351       // OpenMP [2.16, Nesting of Regions]
   2352       // If specified, a teams construct must be contained within a target
   2353       // construct.
   2354       NestingProhibited = ParentRegion != OMPD_target;
   2355       Recommend = ShouldBeInTargetRegion;
   2356       Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
   2357     }
   2358     if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
   2359       // OpenMP [2.16, Nesting of Regions]
   2360       // distribute, parallel, parallel sections, parallel workshare, and the
   2361       // parallel loop and parallel loop SIMD constructs are the only OpenMP
   2362       // constructs that can be closely nested in the teams region.
   2363       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
   2364                           !isOpenMPDistributeDirective(CurrentRegion);
   2365       Recommend = ShouldBeInParallelRegion;
   2366     }
   2367     if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
   2368       // OpenMP 4.5 [2.17 Nesting of Regions]
   2369       // The region associated with the distribute construct must be strictly
   2370       // nested inside a teams region
   2371       NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
   2372       Recommend = ShouldBeInTeamsRegion;
   2373     }
   2374     if (NestingProhibited) {
   2375       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
   2376           << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
   2377           << getOpenMPDirectiveName(CurrentRegion);
   2378       return true;
   2379     }
   2380   }
   2381   return false;
   2382 }
   2383 
   2384 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
   2385                            ArrayRef<OMPClause *> Clauses,
   2386                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
   2387   bool ErrorFound = false;
   2388   unsigned NamedModifiersNumber = 0;
   2389   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
   2390       OMPD_unknown + 1);
   2391   SmallVector<SourceLocation, 4> NameModifierLoc;
   2392   for (const auto *C : Clauses) {
   2393     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
   2394       // At most one if clause without a directive-name-modifier can appear on
   2395       // the directive.
   2396       OpenMPDirectiveKind CurNM = IC->getNameModifier();
   2397       if (FoundNameModifiers[CurNM]) {
   2398         S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
   2399             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
   2400             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
   2401         ErrorFound = true;
   2402       } else if (CurNM != OMPD_unknown) {
   2403         NameModifierLoc.push_back(IC->getNameModifierLoc());
   2404         ++NamedModifiersNumber;
   2405       }
   2406       FoundNameModifiers[CurNM] = IC;
   2407       if (CurNM == OMPD_unknown)
   2408         continue;
   2409       // Check if the specified name modifier is allowed for the current
   2410       // directive.
   2411       // At most one if clause with the particular directive-name-modifier can
   2412       // appear on the directive.
   2413       bool MatchFound = false;
   2414       for (auto NM : AllowedNameModifiers) {
   2415         if (CurNM == NM) {
   2416           MatchFound = true;
   2417           break;
   2418         }
   2419       }
   2420       if (!MatchFound) {
   2421         S.Diag(IC->getNameModifierLoc(),
   2422                diag::err_omp_wrong_if_directive_name_modifier)
   2423             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
   2424         ErrorFound = true;
   2425       }
   2426     }
   2427   }
   2428   // If any if clause on the directive includes a directive-name-modifier then
   2429   // all if clauses on the directive must include a directive-name-modifier.
   2430   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
   2431     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
   2432       S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
   2433              diag::err_omp_no_more_if_clause);
   2434     } else {
   2435       std::string Values;
   2436       std::string Sep(", ");
   2437       unsigned AllowedCnt = 0;
   2438       unsigned TotalAllowedNum =
   2439           AllowedNameModifiers.size() - NamedModifiersNumber;
   2440       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
   2441            ++Cnt) {
   2442         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
   2443         if (!FoundNameModifiers[NM]) {
   2444           Values += "'";
   2445           Values += getOpenMPDirectiveName(NM);
   2446           Values += "'";
   2447           if (AllowedCnt + 2 == TotalAllowedNum)
   2448             Values += " or ";
   2449           else if (AllowedCnt + 1 != TotalAllowedNum)
   2450             Values += Sep;
   2451           ++AllowedCnt;
   2452         }
   2453       }
   2454       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
   2455              diag::err_omp_unnamed_if_clause)
   2456           << (TotalAllowedNum > 1) << Values;
   2457     }
   2458     for (auto Loc : NameModifierLoc) {
   2459       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
   2460     }
   2461     ErrorFound = true;
   2462   }
   2463   return ErrorFound;
   2464 }
   2465 
   2466 StmtResult Sema::ActOnOpenMPExecutableDirective(
   2467     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
   2468     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
   2469     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
   2470   StmtResult Res = StmtError();
   2471   if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
   2472                             StartLoc))
   2473     return StmtError();
   2474 
   2475   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
   2476   llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
   2477   bool ErrorFound = false;
   2478   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
   2479   if (AStmt) {
   2480     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   2481 
   2482     // Check default data sharing attributes for referenced variables.
   2483     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
   2484     DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
   2485     if (DSAChecker.isErrorFound())
   2486       return StmtError();
   2487     // Generate list of implicitly defined firstprivate variables.
   2488     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
   2489 
   2490     if (!DSAChecker.getImplicitFirstprivate().empty()) {
   2491       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
   2492               DSAChecker.getImplicitFirstprivate(), SourceLocation(),
   2493               SourceLocation(), SourceLocation())) {
   2494         ClausesWithImplicit.push_back(Implicit);
   2495         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
   2496                      DSAChecker.getImplicitFirstprivate().size();
   2497       } else
   2498         ErrorFound = true;
   2499     }
   2500   }
   2501 
   2502   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
   2503   switch (Kind) {
   2504   case OMPD_parallel:
   2505     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
   2506                                        EndLoc);
   2507     AllowedNameModifiers.push_back(OMPD_parallel);
   2508     break;
   2509   case OMPD_simd:
   2510     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
   2511                                    VarsWithInheritedDSA);
   2512     break;
   2513   case OMPD_for:
   2514     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
   2515                                   VarsWithInheritedDSA);
   2516     break;
   2517   case OMPD_for_simd:
   2518     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
   2519                                       EndLoc, VarsWithInheritedDSA);
   2520     break;
   2521   case OMPD_sections:
   2522     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
   2523                                        EndLoc);
   2524     break;
   2525   case OMPD_section:
   2526     assert(ClausesWithImplicit.empty() &&
   2527            "No clauses are allowed for 'omp section' directive");
   2528     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
   2529     break;
   2530   case OMPD_single:
   2531     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
   2532                                      EndLoc);
   2533     break;
   2534   case OMPD_master:
   2535     assert(ClausesWithImplicit.empty() &&
   2536            "No clauses are allowed for 'omp master' directive");
   2537     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
   2538     break;
   2539   case OMPD_critical:
   2540     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
   2541                                        StartLoc, EndLoc);
   2542     break;
   2543   case OMPD_parallel_for:
   2544     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
   2545                                           EndLoc, VarsWithInheritedDSA);
   2546     AllowedNameModifiers.push_back(OMPD_parallel);
   2547     break;
   2548   case OMPD_parallel_for_simd:
   2549     Res = ActOnOpenMPParallelForSimdDirective(
   2550         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
   2551     AllowedNameModifiers.push_back(OMPD_parallel);
   2552     break;
   2553   case OMPD_parallel_sections:
   2554     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
   2555                                                StartLoc, EndLoc);
   2556     AllowedNameModifiers.push_back(OMPD_parallel);
   2557     break;
   2558   case OMPD_task:
   2559     Res =
   2560         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
   2561     AllowedNameModifiers.push_back(OMPD_task);
   2562     break;
   2563   case OMPD_taskyield:
   2564     assert(ClausesWithImplicit.empty() &&
   2565            "No clauses are allowed for 'omp taskyield' directive");
   2566     assert(AStmt == nullptr &&
   2567            "No associated statement allowed for 'omp taskyield' directive");
   2568     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
   2569     break;
   2570   case OMPD_barrier:
   2571     assert(ClausesWithImplicit.empty() &&
   2572            "No clauses are allowed for 'omp barrier' directive");
   2573     assert(AStmt == nullptr &&
   2574            "No associated statement allowed for 'omp barrier' directive");
   2575     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
   2576     break;
   2577   case OMPD_taskwait:
   2578     assert(ClausesWithImplicit.empty() &&
   2579            "No clauses are allowed for 'omp taskwait' directive");
   2580     assert(AStmt == nullptr &&
   2581            "No associated statement allowed for 'omp taskwait' directive");
   2582     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
   2583     break;
   2584   case OMPD_taskgroup:
   2585     assert(ClausesWithImplicit.empty() &&
   2586            "No clauses are allowed for 'omp taskgroup' directive");
   2587     Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
   2588     break;
   2589   case OMPD_flush:
   2590     assert(AStmt == nullptr &&
   2591            "No associated statement allowed for 'omp flush' directive");
   2592     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
   2593     break;
   2594   case OMPD_ordered:
   2595     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
   2596                                       EndLoc);
   2597     break;
   2598   case OMPD_atomic:
   2599     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
   2600                                      EndLoc);
   2601     break;
   2602   case OMPD_teams:
   2603     Res =
   2604         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
   2605     break;
   2606   case OMPD_target:
   2607     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
   2608                                      EndLoc);
   2609     AllowedNameModifiers.push_back(OMPD_target);
   2610     break;
   2611   case OMPD_cancellation_point:
   2612     assert(ClausesWithImplicit.empty() &&
   2613            "No clauses are allowed for 'omp cancellation point' directive");
   2614     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
   2615                                "cancellation point' directive");
   2616     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
   2617     break;
   2618   case OMPD_cancel:
   2619     assert(AStmt == nullptr &&
   2620            "No associated statement allowed for 'omp cancel' directive");
   2621     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
   2622                                      CancelRegion);
   2623     AllowedNameModifiers.push_back(OMPD_cancel);
   2624     break;
   2625   case OMPD_target_data:
   2626     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
   2627                                          EndLoc);
   2628     AllowedNameModifiers.push_back(OMPD_target_data);
   2629     break;
   2630   case OMPD_taskloop:
   2631     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
   2632                                        EndLoc, VarsWithInheritedDSA);
   2633     AllowedNameModifiers.push_back(OMPD_taskloop);
   2634     break;
   2635   case OMPD_taskloop_simd:
   2636     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
   2637                                            EndLoc, VarsWithInheritedDSA);
   2638     AllowedNameModifiers.push_back(OMPD_taskloop);
   2639     break;
   2640   case OMPD_distribute:
   2641     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
   2642                                          EndLoc, VarsWithInheritedDSA);
   2643     break;
   2644   case OMPD_threadprivate:
   2645     llvm_unreachable("OpenMP Directive is not allowed");
   2646   case OMPD_unknown:
   2647     llvm_unreachable("Unknown OpenMP directive");
   2648   }
   2649 
   2650   for (auto P : VarsWithInheritedDSA) {
   2651     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
   2652         << P.first << P.second->getSourceRange();
   2653   }
   2654   ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
   2655 
   2656   if (!AllowedNameModifiers.empty())
   2657     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
   2658                  ErrorFound;
   2659 
   2660   if (ErrorFound)
   2661     return StmtError();
   2662   return Res;
   2663 }
   2664 
   2665 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
   2666                                               Stmt *AStmt,
   2667                                               SourceLocation StartLoc,
   2668                                               SourceLocation EndLoc) {
   2669   if (!AStmt)
   2670     return StmtError();
   2671 
   2672   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
   2673   // 1.2.2 OpenMP Language Terminology
   2674   // Structured block - An executable statement with a single entry at the
   2675   // top and a single exit at the bottom.
   2676   // The point of exit cannot be a branch out of the structured block.
   2677   // longjmp() and throw() must not violate the entry/exit criteria.
   2678   CS->getCapturedDecl()->setNothrow();
   2679 
   2680   getCurFunction()->setHasBranchProtectedScope();
   2681 
   2682   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
   2683                                       DSAStack->isCancelRegion());
   2684 }
   2685 
   2686 namespace {
   2687 /// \brief Helper class for checking canonical form of the OpenMP loops and
   2688 /// extracting iteration space of each loop in the loop nest, that will be used
   2689 /// for IR generation.
   2690 class OpenMPIterationSpaceChecker {
   2691   /// \brief Reference to Sema.
   2692   Sema &SemaRef;
   2693   /// \brief A location for diagnostics (when there is no some better location).
   2694   SourceLocation DefaultLoc;
   2695   /// \brief A location for diagnostics (when increment is not compatible).
   2696   SourceLocation ConditionLoc;
   2697   /// \brief A source location for referring to loop init later.
   2698   SourceRange InitSrcRange;
   2699   /// \brief A source location for referring to condition later.
   2700   SourceRange ConditionSrcRange;
   2701   /// \brief A source location for referring to increment later.
   2702   SourceRange IncrementSrcRange;
   2703   /// \brief Loop variable.
   2704   VarDecl *Var;
   2705   /// \brief Reference to loop variable.
   2706   DeclRefExpr *VarRef;
   2707   /// \brief Lower bound (initializer for the var).
   2708   Expr *LB;
   2709   /// \brief Upper bound.
   2710   Expr *UB;
   2711   /// \brief Loop step (increment).
   2712   Expr *Step;
   2713   /// \brief This flag is true when condition is one of:
   2714   ///   Var <  UB
   2715   ///   Var <= UB
   2716   ///   UB  >  Var
   2717   ///   UB  >= Var
   2718   bool TestIsLessOp;
   2719   /// \brief This flag is true when condition is strict ( < or > ).
   2720   bool TestIsStrictOp;
   2721   /// \brief This flag is true when step is subtracted on each iteration.
   2722   bool SubtractStep;
   2723 
   2724 public:
   2725   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
   2726       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
   2727         InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
   2728         IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
   2729         LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
   2730         TestIsStrictOp(false), SubtractStep(false) {}
   2731   /// \brief Check init-expr for canonical loop form and save loop counter
   2732   /// variable - #Var and its initialization value - #LB.
   2733   bool CheckInit(Stmt *S, bool EmitDiags = true);
   2734   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
   2735   /// for less/greater and for strict/non-strict comparison.
   2736   bool CheckCond(Expr *S);
   2737   /// \brief Check incr-expr for canonical loop form and return true if it
   2738   /// does not conform, otherwise save loop step (#Step).
   2739   bool CheckInc(Expr *S);
   2740   /// \brief Return the loop counter variable.
   2741   VarDecl *GetLoopVar() const { return Var; }
   2742   /// \brief Return the reference expression to loop counter variable.
   2743   DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
   2744   /// \brief Source range of the loop init.
   2745   SourceRange GetInitSrcRange() const { return InitSrcRange; }
   2746   /// \brief Source range of the loop condition.
   2747   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
   2748   /// \brief Source range of the loop increment.
   2749   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
   2750   /// \brief True if the step should be subtracted.
   2751   bool ShouldSubtractStep() const { return SubtractStep; }
   2752   /// \brief Build the expression to calculate the number of iterations.
   2753   Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
   2754   /// \brief Build the precondition expression for the loops.
   2755   Expr *BuildPreCond(Scope *S, Expr *Cond) const;
   2756   /// \brief Build reference expression to the counter be used for codegen.
   2757   Expr *BuildCounterVar() const;
   2758   /// \brief Build reference expression to the private counter be used for
   2759   /// codegen.
   2760   Expr *BuildPrivateCounterVar() const;
   2761   /// \brief Build initization of the counter be used for codegen.
   2762   Expr *BuildCounterInit() const;
   2763   /// \brief Build step of the counter be used for codegen.
   2764   Expr *BuildCounterStep() const;
   2765   /// \brief Return true if any expression is dependent.
   2766   bool Dependent() const;
   2767 
   2768 private:
   2769   /// \brief Check the right-hand side of an assignment in the increment
   2770   /// expression.
   2771   bool CheckIncRHS(Expr *RHS);
   2772   /// \brief Helper to set loop counter variable and its initializer.
   2773   bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
   2774   /// \brief Helper to set upper bound.
   2775   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
   2776              SourceLocation SL);
   2777   /// \brief Helper to set loop increment.
   2778   bool SetStep(Expr *NewStep, bool Subtract);
   2779 };
   2780 
   2781 bool OpenMPIterationSpaceChecker::Dependent() const {
   2782   if (!Var) {
   2783     assert(!LB && !UB && !Step);
   2784     return false;
   2785   }
   2786   return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
   2787          (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
   2788 }
   2789 
   2790 template <typename T>
   2791 static T *getExprAsWritten(T *E) {
   2792   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
   2793     E = ExprTemp->getSubExpr();
   2794 
   2795   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
   2796     E = MTE->GetTemporaryExpr();
   2797 
   2798   while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
   2799     E = Binder->getSubExpr();
   2800 
   2801   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
   2802     E = ICE->getSubExprAsWritten();
   2803   return E->IgnoreParens();
   2804 }
   2805 
   2806 bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
   2807                                               DeclRefExpr *NewVarRefExpr,
   2808                                               Expr *NewLB) {
   2809   // State consistency checking to ensure correct usage.
   2810   assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
   2811          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
   2812   if (!NewVar || !NewLB)
   2813     return true;
   2814   Var = NewVar;
   2815   VarRef = NewVarRefExpr;
   2816   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
   2817     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
   2818       if ((Ctor->isCopyOrMoveConstructor() ||
   2819            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
   2820           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
   2821         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
   2822   LB = NewLB;
   2823   return false;
   2824 }
   2825 
   2826 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
   2827                                         SourceRange SR, SourceLocation SL) {
   2828   // State consistency checking to ensure correct usage.
   2829   assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
   2830          !TestIsLessOp && !TestIsStrictOp);
   2831   if (!NewUB)
   2832     return true;
   2833   UB = NewUB;
   2834   TestIsLessOp = LessOp;
   2835   TestIsStrictOp = StrictOp;
   2836   ConditionSrcRange = SR;
   2837   ConditionLoc = SL;
   2838   return false;
   2839 }
   2840 
   2841 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
   2842   // State consistency checking to ensure correct usage.
   2843   assert(Var != nullptr && LB != nullptr && Step == nullptr);
   2844   if (!NewStep)
   2845     return true;
   2846   if (!NewStep->isValueDependent()) {
   2847     // Check that the step is integer expression.
   2848     SourceLocation StepLoc = NewStep->getLocStart();
   2849     ExprResult Val =
   2850         SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
   2851     if (Val.isInvalid())
   2852       return true;
   2853     NewStep = Val.get();
   2854 
   2855     // OpenMP [2.6, Canonical Loop Form, Restrictions]
   2856     //  If test-expr is of form var relational-op b and relational-op is < or
   2857     //  <= then incr-expr must cause var to increase on each iteration of the
   2858     //  loop. If test-expr is of form var relational-op b and relational-op is
   2859     //  > or >= then incr-expr must cause var to decrease on each iteration of
   2860     //  the loop.
   2861     //  If test-expr is of form b relational-op var and relational-op is < or
   2862     //  <= then incr-expr must cause var to decrease on each iteration of the
   2863     //  loop. If test-expr is of form b relational-op var and relational-op is
   2864     //  > or >= then incr-expr must cause var to increase on each iteration of
   2865     //  the loop.
   2866     llvm::APSInt Result;
   2867     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
   2868     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
   2869     bool IsConstNeg =
   2870         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
   2871     bool IsConstPos =
   2872         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
   2873     bool IsConstZero = IsConstant && !Result.getBoolValue();
   2874     if (UB && (IsConstZero ||
   2875                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
   2876                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
   2877       SemaRef.Diag(NewStep->getExprLoc(),
   2878                    diag::err_omp_loop_incr_not_compatible)
   2879           << Var << TestIsLessOp << NewStep->getSourceRange();
   2880       SemaRef.Diag(ConditionLoc,
   2881                    diag::note_omp_loop_cond_requres_compatible_incr)
   2882           << TestIsLessOp << ConditionSrcRange;
   2883       return true;
   2884     }
   2885     if (TestIsLessOp == Subtract) {
   2886       NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
   2887                                              NewStep).get();
   2888       Subtract = !Subtract;
   2889     }
   2890   }
   2891 
   2892   Step = NewStep;
   2893   SubtractStep = Subtract;
   2894   return false;
   2895 }
   2896 
   2897 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
   2898   // Check init-expr for canonical loop form and save loop counter
   2899   // variable - #Var and its initialization value - #LB.
   2900   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
   2901   //   var = lb
   2902   //   integer-type var = lb
   2903   //   random-access-iterator-type var = lb
   2904   //   pointer-type var = lb
   2905   //
   2906   if (!S) {
   2907     if (EmitDiags) {
   2908       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
   2909     }
   2910     return true;
   2911   }
   2912   InitSrcRange = S->getSourceRange();
   2913   if (Expr *E = dyn_cast<Expr>(S))
   2914     S = E->IgnoreParens();
   2915   if (auto BO = dyn_cast<BinaryOperator>(S)) {
   2916     if (BO->getOpcode() == BO_Assign)
   2917       if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
   2918         return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
   2919                            BO->getRHS());
   2920   } else if (auto DS = dyn_cast<DeclStmt>(S)) {
   2921     if (DS->isSingleDecl()) {
   2922       if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
   2923         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
   2924           // Accept non-canonical init form here but emit ext. warning.
   2925           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
   2926             SemaRef.Diag(S->getLocStart(),
   2927                          diag::ext_omp_loop_not_canonical_init)
   2928                 << S->getSourceRange();
   2929           return SetVarAndLB(Var, nullptr, Var->getInit());
   2930         }
   2931       }
   2932     }
   2933   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
   2934     if (CE->getOperator() == OO_Equal)
   2935       if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
   2936         return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
   2937                            CE->getArg(1));
   2938 
   2939   if (EmitDiags) {
   2940     SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
   2941         << S->getSourceRange();
   2942   }
   2943   return true;
   2944 }
   2945 
   2946 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
   2947 /// variable (which may be the loop variable) if possible.
   2948 static const VarDecl *GetInitVarDecl(const Expr *E) {
   2949   if (!E)
   2950     return nullptr;
   2951   E = getExprAsWritten(E);
   2952   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
   2953     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
   2954       if ((Ctor->isCopyOrMoveConstructor() ||
   2955            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
   2956           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
   2957         E = CE->getArg(0)->IgnoreParenImpCasts();
   2958   auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
   2959   if (!DRE)
   2960     return nullptr;
   2961   return dyn_cast<VarDecl>(DRE->getDecl());
   2962 }
   2963 
   2964 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
   2965   // Check test-expr for canonical form, save upper-bound UB, flags for
   2966   // less/greater and for strict/non-strict comparison.
   2967   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
   2968   //   var relational-op b
   2969   //   b relational-op var
   2970   //
   2971   if (!S) {
   2972     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
   2973     return true;
   2974   }
   2975   S = getExprAsWritten(S);
   2976   SourceLocation CondLoc = S->getLocStart();
   2977   if (auto BO = dyn_cast<BinaryOperator>(S)) {
   2978     if (BO->isRelationalOp()) {
   2979       if (GetInitVarDecl(BO->getLHS()) == Var)
   2980         return SetUB(BO->getRHS(),
   2981                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
   2982                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
   2983                      BO->getSourceRange(), BO->getOperatorLoc());
   2984       if (GetInitVarDecl(BO->getRHS()) == Var)
   2985         return SetUB(BO->getLHS(),
   2986                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
   2987                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
   2988                      BO->getSourceRange(), BO->getOperatorLoc());
   2989     }
   2990   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
   2991     if (CE->getNumArgs() == 2) {
   2992       auto Op = CE->getOperator();
   2993       switch (Op) {
   2994       case OO_Greater:
   2995       case OO_GreaterEqual:
   2996       case OO_Less:
   2997       case OO_LessEqual:
   2998         if (GetInitVarDecl(CE->getArg(0)) == Var)
   2999           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
   3000                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
   3001                        CE->getOperatorLoc());
   3002         if (GetInitVarDecl(CE->getArg(1)) == Var)
   3003           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
   3004                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
   3005                        CE->getOperatorLoc());
   3006         break;
   3007       default:
   3008         break;
   3009       }
   3010     }
   3011   }
   3012   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
   3013       << S->getSourceRange() << Var;
   3014   return true;
   3015 }
   3016 
   3017 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
   3018   // RHS of canonical loop form increment can be:
   3019   //   var + incr
   3020   //   incr + var
   3021   //   var - incr
   3022   //
   3023   RHS = RHS->IgnoreParenImpCasts();
   3024   if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
   3025     if (BO->isAdditiveOp()) {
   3026       bool IsAdd = BO->getOpcode() == BO_Add;
   3027       if (GetInitVarDecl(BO->getLHS()) == Var)
   3028         return SetStep(BO->getRHS(), !IsAdd);
   3029       if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
   3030         return SetStep(BO->getLHS(), false);
   3031     }
   3032   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
   3033     bool IsAdd = CE->getOperator() == OO_Plus;
   3034     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
   3035       if (GetInitVarDecl(CE->getArg(0)) == Var)
   3036         return SetStep(CE->getArg(1), !IsAdd);
   3037       if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
   3038         return SetStep(CE->getArg(0), false);
   3039     }
   3040   }
   3041   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
   3042       << RHS->getSourceRange() << Var;
   3043   return true;
   3044 }
   3045 
   3046 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
   3047   // Check incr-expr for canonical loop form and return true if it
   3048   // does not conform.
   3049   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
   3050   //   ++var
   3051   //   var++
   3052   //   --var
   3053   //   var--
   3054   //   var += incr
   3055   //   var -= incr
   3056   //   var = var + incr
   3057   //   var = incr + var
   3058   //   var = var - incr
   3059   //
   3060   if (!S) {
   3061     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
   3062     return true;
   3063   }
   3064   IncrementSrcRange = S->getSourceRange();
   3065   S = S->IgnoreParens();
   3066   if (auto UO = dyn_cast<UnaryOperator>(S)) {
   3067     if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
   3068       return SetStep(
   3069           SemaRef.ActOnIntegerConstant(UO->getLocStart(),
   3070                                        (UO->isDecrementOp() ? -1 : 1)).get(),
   3071           false);
   3072   } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
   3073     switch (BO->getOpcode()) {
   3074     case BO_AddAssign:
   3075     case BO_SubAssign:
   3076       if (GetInitVarDecl(BO->getLHS()) == Var)
   3077         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
   3078       break;
   3079     case BO_Assign:
   3080       if (GetInitVarDecl(BO->getLHS()) == Var)
   3081         return CheckIncRHS(BO->getRHS());
   3082       break;
   3083     default:
   3084       break;
   3085     }
   3086   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
   3087     switch (CE->getOperator()) {
   3088     case OO_PlusPlus:
   3089     case OO_MinusMinus:
   3090       if (GetInitVarDecl(CE->getArg(0)) == Var)
   3091         return SetStep(
   3092             SemaRef.ActOnIntegerConstant(
   3093                         CE->getLocStart(),
   3094                         ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
   3095             false);
   3096       break;
   3097     case OO_PlusEqual:
   3098     case OO_MinusEqual:
   3099       if (GetInitVarDecl(CE->getArg(0)) == Var)
   3100         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
   3101       break;
   3102     case OO_Equal:
   3103       if (GetInitVarDecl(CE->getArg(0)) == Var)
   3104         return CheckIncRHS(CE->getArg(1));
   3105       break;
   3106     default:
   3107       break;
   3108     }
   3109   }
   3110   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
   3111       << S->getSourceRange() << Var;
   3112   return true;
   3113 }
   3114 
   3115 namespace {
   3116 // Transform variables declared in GNU statement expressions to new ones to
   3117 // avoid crash on codegen.
   3118 class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
   3119   typedef TreeTransform<TransformToNewDefs> BaseTransform;
   3120 
   3121 public:
   3122   TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
   3123 
   3124   Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
   3125     if (auto *VD = cast<VarDecl>(D))
   3126       if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
   3127           !isa<ImplicitParamDecl>(D)) {
   3128         auto *NewVD = VarDecl::Create(
   3129             SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
   3130             VD->getLocation(), VD->getIdentifier(), VD->getType(),
   3131             VD->getTypeSourceInfo(), VD->getStorageClass());
   3132         NewVD->setTSCSpec(VD->getTSCSpec());
   3133         NewVD->setInit(VD->getInit());
   3134         NewVD->setInitStyle(VD->getInitStyle());
   3135         NewVD->setExceptionVariable(VD->isExceptionVariable());
   3136         NewVD->setNRVOVariable(VD->isNRVOVariable());
   3137         NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
   3138         NewVD->setConstexpr(VD->isConstexpr());
   3139         NewVD->setInitCapture(VD->isInitCapture());
   3140         NewVD->setPreviousDeclInSameBlockScope(
   3141             VD->isPreviousDeclInSameBlockScope());
   3142         VD->getDeclContext()->addHiddenDecl(NewVD);
   3143         if (VD->hasAttrs())
   3144           NewVD->setAttrs(VD->getAttrs());
   3145         transformedLocalDecl(VD, NewVD);
   3146         return NewVD;
   3147       }
   3148     return BaseTransform::TransformDefinition(Loc, D);
   3149   }
   3150 
   3151   ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
   3152     if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
   3153       if (E->getDecl() != NewD) {
   3154         NewD->setReferenced();
   3155         NewD->markUsed(SemaRef.Context);
   3156         return DeclRefExpr::Create(
   3157             SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
   3158             cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
   3159             E->getNameInfo(), E->getType(), E->getValueKind());
   3160       }
   3161     return BaseTransform::TransformDeclRefExpr(E);
   3162   }
   3163 };
   3164 }
   3165 
   3166 /// \brief Build the expression to calculate the number of iterations.
   3167 Expr *
   3168 OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
   3169                                                 const bool LimitedType) const {
   3170   TransformToNewDefs Transform(SemaRef);
   3171   ExprResult Diff;
   3172   auto VarType = Var->getType().getNonReferenceType();
   3173   if (VarType->isIntegerType() || VarType->isPointerType() ||
   3174       SemaRef.getLangOpts().CPlusPlus) {
   3175     // Upper - Lower
   3176     auto *UBExpr = TestIsLessOp ? UB : LB;
   3177     auto *LBExpr = TestIsLessOp ? LB : UB;
   3178     Expr *Upper = Transform.TransformExpr(UBExpr).get();
   3179     Expr *Lower = Transform.TransformExpr(LBExpr).get();
   3180     if (!Upper || !Lower)
   3181       return nullptr;
   3182     Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
   3183                                                     Sema::AA_Converting,
   3184                                                     /*AllowExplicit=*/true)
   3185                       .get();
   3186     Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
   3187                                               Sema::AA_Converting,
   3188                                               /*AllowExplicit=*/true)
   3189                 .get();
   3190     if (!Upper || !Lower)
   3191       return nullptr;
   3192 
   3193     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
   3194 
   3195     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
   3196       // BuildBinOp already emitted error, this one is to point user to upper
   3197       // and lower bound, and to tell what is passed to 'operator-'.
   3198       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
   3199           << Upper->getSourceRange() << Lower->getSourceRange();
   3200       return nullptr;
   3201     }
   3202   }
   3203 
   3204   if (!Diff.isUsable())
   3205     return nullptr;
   3206 
   3207   // Upper - Lower [- 1]
   3208   if (TestIsStrictOp)
   3209     Diff = SemaRef.BuildBinOp(
   3210         S, DefaultLoc, BO_Sub, Diff.get(),
   3211         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
   3212   if (!Diff.isUsable())
   3213     return nullptr;
   3214 
   3215   // Upper - Lower [- 1] + Step
   3216   auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
   3217   if (NewStep.isInvalid())
   3218     return nullptr;
   3219   NewStep = SemaRef.PerformImplicitConversion(
   3220       NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
   3221       /*AllowExplicit=*/true);
   3222   if (NewStep.isInvalid())
   3223     return nullptr;
   3224   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
   3225   if (!Diff.isUsable())
   3226     return nullptr;
   3227 
   3228   // Parentheses (for dumping/debugging purposes only).
   3229   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
   3230   if (!Diff.isUsable())
   3231     return nullptr;
   3232 
   3233   // (Upper - Lower [- 1] + Step) / Step
   3234   NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
   3235   if (NewStep.isInvalid())
   3236     return nullptr;
   3237   NewStep = SemaRef.PerformImplicitConversion(
   3238       NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
   3239       /*AllowExplicit=*/true);
   3240   if (NewStep.isInvalid())
   3241     return nullptr;
   3242   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
   3243   if (!Diff.isUsable())
   3244     return nullptr;
   3245 
   3246   // OpenMP runtime requires 32-bit or 64-bit loop variables.
   3247   QualType Type = Diff.get()->getType();
   3248   auto &C = SemaRef.Context;
   3249   bool UseVarType = VarType->hasIntegerRepresentation() &&
   3250                     C.getTypeSize(Type) > C.getTypeSize(VarType);
   3251   if (!Type->isIntegerType() || UseVarType) {
   3252     unsigned NewSize =
   3253         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
   3254     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
   3255                                : Type->hasSignedIntegerRepresentation();
   3256     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
   3257     Diff = SemaRef.PerformImplicitConversion(
   3258         Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
   3259     if (!Diff.isUsable())
   3260       return nullptr;
   3261   }
   3262   if (LimitedType) {
   3263     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
   3264     if (NewSize != C.getTypeSize(Type)) {
   3265       if (NewSize < C.getTypeSize(Type)) {
   3266         assert(NewSize == 64 && "incorrect loop var size");
   3267         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
   3268             << InitSrcRange << ConditionSrcRange;
   3269       }
   3270       QualType NewType = C.getIntTypeForBitwidth(
   3271           NewSize, Type->hasSignedIntegerRepresentation() ||
   3272                        C.getTypeSize(Type) < NewSize);
   3273       Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
   3274                                                Sema::AA_Converting, true);
   3275       if (!Diff.isUsable())
   3276         return nullptr;
   3277     }
   3278   }
   3279 
   3280   return Diff.get();
   3281 }
   3282 
   3283 Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
   3284   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
   3285   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
   3286   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
   3287   TransformToNewDefs Transform(SemaRef);
   3288 
   3289   auto NewLB = Transform.TransformExpr(LB);
   3290   auto NewUB = Transform.TransformExpr(UB);
   3291   if (NewLB.isInvalid() || NewUB.isInvalid())
   3292     return Cond;
   3293   NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
   3294                                             Sema::AA_Converting,
   3295                                             /*AllowExplicit=*/true);
   3296   NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
   3297                                             Sema::AA_Converting,
   3298                                             /*AllowExplicit=*/true);
   3299   if (NewLB.isInvalid() || NewUB.isInvalid())
   3300     return Cond;
   3301   auto CondExpr = SemaRef.BuildBinOp(
   3302       S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
   3303                                   : (TestIsStrictOp ? BO_GT : BO_GE),
   3304       NewLB.get(), NewUB.get());
   3305   if (CondExpr.isUsable()) {
   3306     CondExpr = SemaRef.PerformImplicitConversion(
   3307         CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
   3308         /*AllowExplicit=*/true);
   3309   }
   3310   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
   3311   // Otherwise use original loop conditon and evaluate it in runtime.
   3312   return CondExpr.isUsable() ? CondExpr.get() : Cond;
   3313 }
   3314 
   3315 /// \brief Build reference expression to the counter be used for codegen.
   3316 Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
   3317   return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
   3318                           DefaultLoc);
   3319 }
   3320 
   3321 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
   3322   if (Var && !Var->isInvalidDecl()) {
   3323     auto Type = Var->getType().getNonReferenceType();
   3324     auto *PrivateVar =
   3325         buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
   3326                      Var->hasAttrs() ? &Var->getAttrs() : nullptr);
   3327     if (PrivateVar->isInvalidDecl())
   3328       return nullptr;
   3329     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
   3330   }
   3331   return nullptr;
   3332 }
   3333 
   3334 /// \brief Build initization of the counter be used for codegen.
   3335 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
   3336 
   3337 /// \brief Build step of the counter be used for codegen.
   3338 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
   3339 
   3340 /// \brief Iteration space of a single for loop.
   3341 struct LoopIterationSpace {
   3342   /// \brief Condition of the loop.
   3343   Expr *PreCond;
   3344   /// \brief This expression calculates the number of iterations in the loop.
   3345   /// It is always possible to calculate it before starting the loop.
   3346   Expr *NumIterations;
   3347   /// \brief The loop counter variable.
   3348   Expr *CounterVar;
   3349   /// \brief Private loop counter variable.
   3350   Expr *PrivateCounterVar;
   3351   /// \brief This is initializer for the initial value of #CounterVar.
   3352   Expr *CounterInit;
   3353   /// \brief This is step for the #CounterVar used to generate its update:
   3354   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
   3355   Expr *CounterStep;
   3356   /// \brief Should step be subtracted?
   3357   bool Subtract;
   3358   /// \brief Source range of the loop init.
   3359   SourceRange InitSrcRange;
   3360   /// \brief Source range of the loop condition.
   3361   SourceRange CondSrcRange;
   3362   /// \brief Source range of the loop increment.
   3363   SourceRange IncSrcRange;
   3364 };
   3365 
   3366 } // namespace
   3367 
   3368 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
   3369   assert(getLangOpts().OpenMP && "OpenMP is not active.");
   3370   assert(Init && "Expected loop in canonical form.");
   3371   unsigned CollapseIteration = DSAStack->getCollapseNumber();
   3372   if (CollapseIteration > 0 &&
   3373       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
   3374     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
   3375     if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
   3376       DSAStack->addLoopControlVariable(ISC.GetLoopVar());
   3377     }
   3378     DSAStack->setCollapseNumber(CollapseIteration - 1);
   3379   }
   3380 }
   3381 
   3382 /// \brief Called on a for stmt to check and extract its iteration space
   3383 /// for further processing (such as collapsing).
   3384 static bool CheckOpenMPIterationSpace(
   3385     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
   3386     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
   3387     Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
   3388     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
   3389     LoopIterationSpace &ResultIterSpace) {
   3390   // OpenMP [2.6, Canonical Loop Form]
   3391   //   for (init-expr; test-expr; incr-expr) structured-block
   3392   auto For = dyn_cast_or_null<ForStmt>(S);
   3393   if (!For) {
   3394     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
   3395         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
   3396         << getOpenMPDirectiveName(DKind) << NestedLoopCount
   3397         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
   3398     if (NestedLoopCount > 1) {
   3399       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
   3400         SemaRef.Diag(DSA.getConstructLoc(),
   3401                      diag::note_omp_collapse_ordered_expr)
   3402             << 2 << CollapseLoopCountExpr->getSourceRange()
   3403             << OrderedLoopCountExpr->getSourceRange();
   3404       else if (CollapseLoopCountExpr)
   3405         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
   3406                      diag::note_omp_collapse_ordered_expr)
   3407             << 0 << CollapseLoopCountExpr->getSourceRange();
   3408       else
   3409         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
   3410                      diag::note_omp_collapse_ordered_expr)
   3411             << 1 << OrderedLoopCountExpr->getSourceRange();
   3412     }
   3413     return true;
   3414   }
   3415   assert(For->getBody());
   3416 
   3417   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
   3418 
   3419   // Check init.
   3420   auto Init = For->getInit();
   3421   if (ISC.CheckInit(Init)) {
   3422     return true;
   3423   }
   3424 
   3425   bool HasErrors = false;
   3426 
   3427   // Check loop variable's type.
   3428   auto Var = ISC.GetLoopVar();
   3429 
   3430   // OpenMP [2.6, Canonical Loop Form]
   3431   // Var is one of the following:
   3432   //   A variable of signed or unsigned integer type.
   3433   //   For C++, a variable of a random access iterator type.
   3434   //   For C, a variable of a pointer type.
   3435   auto VarType = Var->getType().getNonReferenceType();
   3436   if (!VarType->isDependentType() && !VarType->isIntegerType() &&
   3437       !VarType->isPointerType() &&
   3438       !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
   3439     SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
   3440         << SemaRef.getLangOpts().CPlusPlus;
   3441     HasErrors = true;
   3442   }
   3443 
   3444   // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
   3445   // Construct
   3446   // The loop iteration variable(s) in the associated for-loop(s) of a for or
   3447   // parallel for construct is (are) private.
   3448   // The loop iteration variable in the associated for-loop of a simd construct
   3449   // with just one associated for-loop is linear with a constant-linear-step
   3450   // that is the increment of the associated for-loop.
   3451   // Exclude loop var from the list of variables with implicitly defined data
   3452   // sharing attributes.
   3453   VarsWithImplicitDSA.erase(Var);
   3454 
   3455   // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
   3456   // a Construct, C/C++].
   3457   // The loop iteration variable in the associated for-loop of a simd construct
   3458   // with just one associated for-loop may be listed in a linear clause with a
   3459   // constant-linear-step that is the increment of the associated for-loop.
   3460   // The loop iteration variable(s) in the associated for-loop(s) of a for or
   3461   // parallel for construct may be listed in a private or lastprivate clause.
   3462   DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
   3463   auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
   3464   // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
   3465   // declared in the loop and it is predetermined as a private.
   3466   auto PredeterminedCKind =
   3467       isOpenMPSimdDirective(DKind)
   3468           ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
   3469           : OMPC_private;
   3470   if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
   3471         DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
   3472        ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
   3473         isOpenMPDistributeDirective(DKind)) &&
   3474         !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
   3475         DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate &&
   3476         DVar.CKind != OMPC_threadprivate)) &&
   3477       ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
   3478        DVar.RefExpr != nullptr)) {
   3479     SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
   3480         << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
   3481         << getOpenMPClauseName(PredeterminedCKind);
   3482     if (DVar.RefExpr == nullptr)
   3483       DVar.CKind = PredeterminedCKind;
   3484     ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
   3485     HasErrors = true;
   3486   } else if (LoopVarRefExpr != nullptr) {
   3487     // Make the loop iteration variable private (for worksharing constructs),
   3488     // linear (for simd directives with the only one associated loop) or
   3489     // lastprivate (for simd directives with several collapsed or ordered
   3490     // loops).
   3491     if (DVar.CKind == OMPC_unknown)
   3492       DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
   3493                         /*FromParent=*/false);
   3494     DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
   3495   }
   3496 
   3497   assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
   3498 
   3499   // Check test-expr.
   3500   HasErrors |= ISC.CheckCond(For->getCond());
   3501 
   3502   // Check incr-expr.
   3503   HasErrors |= ISC.CheckInc(For->getInc());
   3504 
   3505   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
   3506     return HasErrors;
   3507 
   3508   // Build the loop's iteration space representation.
   3509   ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
   3510   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
   3511       DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
   3512                           isOpenMPTaskLoopDirective(DKind) ||
   3513                           isOpenMPDistributeDirective(DKind)));
   3514   ResultIterSpace.CounterVar = ISC.BuildCounterVar();
   3515   ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
   3516   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
   3517   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
   3518   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
   3519   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
   3520   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
   3521   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
   3522 
   3523   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
   3524                 ResultIterSpace.NumIterations == nullptr ||
   3525                 ResultIterSpace.CounterVar == nullptr ||
   3526                 ResultIterSpace.PrivateCounterVar == nullptr ||
   3527                 ResultIterSpace.CounterInit == nullptr ||
   3528                 ResultIterSpace.CounterStep == nullptr);
   3529 
   3530   return HasErrors;
   3531 }
   3532 
   3533 /// \brief Build 'VarRef = Start.
   3534 static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
   3535                                    ExprResult VarRef, ExprResult Start) {
   3536   TransformToNewDefs Transform(SemaRef);
   3537   // Build 'VarRef = Start.
   3538   auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
   3539   if (NewStart.isInvalid())
   3540     return ExprError();
   3541   NewStart = SemaRef.PerformImplicitConversion(
   3542       NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
   3543       Sema::AA_Converting,
   3544       /*AllowExplicit=*/true);
   3545   if (NewStart.isInvalid())
   3546     return ExprError();
   3547   NewStart = SemaRef.PerformImplicitConversion(
   3548       NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
   3549       /*AllowExplicit=*/true);
   3550   if (!NewStart.isUsable())
   3551     return ExprError();
   3552 
   3553   auto Init =
   3554       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
   3555   return Init;
   3556 }
   3557 
   3558 /// \brief Build 'VarRef = Start + Iter * Step'.
   3559 static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
   3560                                      SourceLocation Loc, ExprResult VarRef,
   3561                                      ExprResult Start, ExprResult Iter,
   3562                                      ExprResult Step, bool Subtract) {
   3563   // Add parentheses (for debugging purposes only).
   3564   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
   3565   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
   3566       !Step.isUsable())
   3567     return ExprError();
   3568 
   3569   TransformToNewDefs Transform(SemaRef);
   3570   auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
   3571   if (NewStep.isInvalid())
   3572     return ExprError();
   3573   NewStep = SemaRef.PerformImplicitConversion(
   3574       NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
   3575       Sema::AA_Converting,
   3576       /*AllowExplicit=*/true);
   3577   if (NewStep.isInvalid())
   3578     return ExprError();
   3579   ExprResult Update =
   3580       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
   3581   if (!Update.isUsable())
   3582     return ExprError();
   3583 
   3584   // Build 'VarRef = Start + Iter * Step'.
   3585   auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
   3586   if (NewStart.isInvalid())
   3587     return ExprError();
   3588   NewStart = SemaRef.PerformImplicitConversion(
   3589       NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
   3590       Sema::AA_Converting,
   3591       /*AllowExplicit=*/true);
   3592   if (NewStart.isInvalid())
   3593     return ExprError();
   3594   Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
   3595                               NewStart.get(), Update.get());
   3596   if (!Update.isUsable())
   3597     return ExprError();
   3598 
   3599   Update = SemaRef.PerformImplicitConversion(
   3600       Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
   3601   if (!Update.isUsable())
   3602     return ExprError();
   3603 
   3604   Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
   3605   return Update;
   3606 }
   3607 
   3608 /// \brief Convert integer expression \a E to make it have at least \a Bits
   3609 /// bits.
   3610 static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
   3611                                       Sema &SemaRef) {
   3612   if (E == nullptr)
   3613     return ExprError();
   3614   auto &C = SemaRef.Context;
   3615   QualType OldType = E->getType();
   3616   unsigned HasBits = C.getTypeSize(OldType);
   3617   if (HasBits >= Bits)
   3618     return ExprResult(E);
   3619   // OK to convert to signed, because new type has more bits than old.
   3620   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
   3621   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
   3622                                            true);
   3623 }
   3624 
   3625 /// \brief Check if the given expression \a E is a constant integer that fits
   3626 /// into \a Bits bits.
   3627 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
   3628   if (E == nullptr)
   3629     return false;
   3630   llvm::APSInt Result;
   3631   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
   3632     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
   3633   return false;
   3634 }
   3635 
   3636 /// \brief Called on a for stmt to check itself and nested loops (if any).
   3637 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
   3638 /// number of collapsed loops otherwise.
   3639 static unsigned
   3640 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
   3641                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
   3642                 DSAStackTy &DSA,
   3643                 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
   3644                 OMPLoopDirective::HelperExprs &Built) {
   3645   unsigned NestedLoopCount = 1;
   3646   if (CollapseLoopCountExpr) {
   3647     // Found 'collapse' clause - calculate collapse number.
   3648     llvm::APSInt Result;
   3649     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
   3650       NestedLoopCount = Result.getLimitedValue();
   3651   }
   3652   if (OrderedLoopCountExpr) {
   3653     // Found 'ordered' clause - calculate collapse number.
   3654     llvm::APSInt Result;
   3655     if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
   3656       if (Result.getLimitedValue() < NestedLoopCount) {
   3657         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
   3658                      diag::err_omp_wrong_ordered_loop_count)
   3659             << OrderedLoopCountExpr->getSourceRange();
   3660         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
   3661                      diag::note_collapse_loop_count)
   3662             << CollapseLoopCountExpr->getSourceRange();
   3663       }
   3664       NestedLoopCount = Result.getLimitedValue();
   3665     }
   3666   }
   3667   // This is helper routine for loop directives (e.g., 'for', 'simd',
   3668   // 'for simd', etc.).
   3669   SmallVector<LoopIterationSpace, 4> IterSpaces;
   3670   IterSpaces.resize(NestedLoopCount);
   3671   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
   3672   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
   3673     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
   3674                                   NestedLoopCount, CollapseLoopCountExpr,
   3675                                   OrderedLoopCountExpr, VarsWithImplicitDSA,
   3676                                   IterSpaces[Cnt]))
   3677       return 0;
   3678     // Move on to the next nested for loop, or to the loop body.
   3679     // OpenMP [2.8.1, simd construct, Restrictions]
   3680     // All loops associated with the construct must be perfectly nested; that
   3681     // is, there must be no intervening code nor any OpenMP directive between
   3682     // any two loops.
   3683     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
   3684   }
   3685 
   3686   Built.clear(/* size */ NestedLoopCount);
   3687 
   3688   if (SemaRef.CurContext->isDependentContext())
   3689     return NestedLoopCount;
   3690 
   3691   // An example of what is generated for the following code:
   3692   //
   3693   //   #pragma omp simd collapse(2) ordered(2)
   3694   //   for (i = 0; i < NI; ++i)
   3695   //     for (k = 0; k < NK; ++k)
   3696   //       for (j = J0; j < NJ; j+=2) {
   3697   //         <loop body>
   3698   //       }
   3699   //
   3700   // We generate the code below.
   3701   // Note: the loop body may be outlined in CodeGen.
   3702   // Note: some counters may be C++ classes, operator- is used to find number of
   3703   // iterations and operator+= to calculate counter value.
   3704   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
   3705   // or i64 is currently supported).
   3706   //
   3707   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
   3708   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
   3709   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
   3710   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
   3711   //     // similar updates for vars in clauses (e.g. 'linear')
   3712   //     <loop body (using local i and j)>
   3713   //   }
   3714   //   i = NI; // assign final values of counters
   3715   //   j = NJ;
   3716   //
   3717 
   3718   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
   3719   // the iteration counts of the collapsed for loops.
   3720   // Precondition tests if there is at least one iteration (all conditions are
   3721   // true).
   3722   auto PreCond = ExprResult(IterSpaces[0].PreCond);
   3723   auto N0 = IterSpaces[0].NumIterations;
   3724   ExprResult LastIteration32 = WidenIterationCount(
   3725       32 /* Bits */, SemaRef.PerformImplicitConversion(
   3726                                 N0->IgnoreImpCasts(), N0->getType(),
   3727                                 Sema::AA_Converting, /*AllowExplicit=*/true)
   3728                          .get(),
   3729       SemaRef);
   3730   ExprResult LastIteration64 = WidenIterationCount(
   3731       64 /* Bits */, SemaRef.PerformImplicitConversion(
   3732                                 N0->IgnoreImpCasts(), N0->getType(),
   3733                                 Sema::AA_Converting, /*AllowExplicit=*/true)
   3734                          .get(),
   3735       SemaRef);
   3736 
   3737   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
   3738     return NestedLoopCount;
   3739 
   3740   auto &C = SemaRef.Context;
   3741   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
   3742 
   3743   Scope *CurScope = DSA.getCurScope();
   3744   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
   3745     if (PreCond.isUsable()) {
   3746       PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
   3747                                    PreCond.get(), IterSpaces[Cnt].PreCond);
   3748     }
   3749     auto N = IterSpaces[Cnt].NumIterations;
   3750     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
   3751     if (LastIteration32.isUsable())
   3752       LastIteration32 = SemaRef.BuildBinOp(
   3753           CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
   3754           SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
   3755                                             Sema::AA_Converting,
   3756                                             /*AllowExplicit=*/true)
   3757               .get());
   3758     if (LastIteration64.isUsable())
   3759       LastIteration64 = SemaRef.BuildBinOp(
   3760           CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
   3761           SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
   3762                                             Sema::AA_Converting,
   3763                                             /*AllowExplicit=*/true)
   3764               .get());
   3765   }
   3766 
   3767   // Choose either the 32-bit or 64-bit version.
   3768   ExprResult LastIteration = LastIteration64;
   3769   if (LastIteration32.isUsable() &&
   3770       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
   3771       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
   3772        FitsInto(
   3773            32 /* Bits */,
   3774            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
   3775            LastIteration64.get(), SemaRef)))
   3776     LastIteration = LastIteration32;
   3777 
   3778   if (!LastIteration.isUsable())
   3779     return 0;
   3780 
   3781   // Save the number of iterations.
   3782   ExprResult NumIterations = LastIteration;
   3783   {
   3784     LastIteration = SemaRef.BuildBinOp(
   3785         CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
   3786         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
   3787     if (!LastIteration.isUsable())
   3788       return 0;
   3789   }
   3790 
   3791   // Calculate the last iteration number beforehand instead of doing this on
   3792   // each iteration. Do not do this if the number of iterations may be kfold-ed.
   3793   llvm::APSInt Result;
   3794   bool IsConstant =
   3795       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
   3796   ExprResult CalcLastIteration;
   3797   if (!IsConstant) {
   3798     SourceLocation SaveLoc;
   3799     VarDecl *SaveVar =
   3800         buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
   3801                      ".omp.last.iteration");
   3802     ExprResult SaveRef = buildDeclRefExpr(
   3803         SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
   3804     CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
   3805                                            SaveRef.get(), LastIteration.get());
   3806     LastIteration = SaveRef;
   3807 
   3808     // Prepare SaveRef + 1.
   3809     NumIterations = SemaRef.BuildBinOp(
   3810         CurScope, SaveLoc, BO_Add, SaveRef.get(),
   3811         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
   3812     if (!NumIterations.isUsable())
   3813       return 0;
   3814   }
   3815 
   3816   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
   3817 
   3818   QualType VType = LastIteration.get()->getType();
   3819   // Build variables passed into runtime, nesessary for worksharing directives.
   3820   ExprResult LB, UB, IL, ST, EUB;
   3821   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
   3822       isOpenMPDistributeDirective(DKind)) {
   3823     // Lower bound variable, initialized with zero.
   3824     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
   3825     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
   3826     SemaRef.AddInitializerToDecl(
   3827         LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
   3828         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
   3829 
   3830     // Upper bound variable, initialized with last iteration number.
   3831     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
   3832     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
   3833     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
   3834                                  /*DirectInit*/ false,
   3835                                  /*TypeMayContainAuto*/ false);
   3836 
   3837     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
   3838     // This will be used to implement clause 'lastprivate'.
   3839     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
   3840     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
   3841     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
   3842     SemaRef.AddInitializerToDecl(
   3843         ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
   3844         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
   3845 
   3846     // Stride variable returned by runtime (we initialize it to 1 by default).
   3847     VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
   3848     ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
   3849     SemaRef.AddInitializerToDecl(
   3850         STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
   3851         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
   3852 
   3853     // Build expression: UB = min(UB, LastIteration)
   3854     // It is nesessary for CodeGen of directives with static scheduling.
   3855     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
   3856                                                 UB.get(), LastIteration.get());
   3857     ExprResult CondOp = SemaRef.ActOnConditionalOp(
   3858         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
   3859     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
   3860                              CondOp.get());
   3861     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
   3862   }
   3863 
   3864   // Build the iteration variable and its initialization before loop.
   3865   ExprResult IV;
   3866   ExprResult Init;
   3867   {
   3868     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
   3869     IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
   3870     Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
   3871                  isOpenMPTaskLoopDirective(DKind) ||
   3872                  isOpenMPDistributeDirective(DKind))
   3873                     ? LB.get()
   3874                     : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
   3875     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
   3876     Init = SemaRef.ActOnFinishFullExpr(Init.get());
   3877   }
   3878 
   3879   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
   3880   SourceLocation CondLoc;
   3881   ExprResult Cond =
   3882       (isOpenMPWorksharingDirective(DKind) ||
   3883        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
   3884           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
   3885           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
   3886                                NumIterations.get());
   3887 
   3888   // Loop increment (IV = IV + 1)
   3889   SourceLocation IncLoc;
   3890   ExprResult Inc =
   3891       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
   3892                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
   3893   if (!Inc.isUsable())
   3894     return 0;
   3895   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
   3896   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
   3897   if (!Inc.isUsable())
   3898     return 0;
   3899 
   3900   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
   3901   // Used for directives with static scheduling.
   3902   ExprResult NextLB, NextUB;
   3903   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
   3904       isOpenMPDistributeDirective(DKind)) {
   3905     // LB + ST
   3906     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
   3907     if (!NextLB.isUsable())
   3908       return 0;
   3909     // LB = LB + ST
   3910     NextLB =
   3911         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
   3912     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
   3913     if (!NextLB.isUsable())
   3914       return 0;
   3915     // UB + ST
   3916     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
   3917     if (!NextUB.isUsable())
   3918       return 0;
   3919     // UB = UB + ST
   3920     NextUB =
   3921         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
   3922     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
   3923     if (!NextUB.isUsable())
   3924       return 0;
   3925   }
   3926 
   3927   // Build updates and final values of the loop counters.
   3928   bool HasErrors = false;
   3929   Built.Counters.resize(NestedLoopCount);
   3930   Built.Inits.resize(NestedLoopCount);
   3931   Built.Updates.resize(NestedLoopCount);
   3932   Built.Finals.resize(NestedLoopCount);
   3933   {
   3934     ExprResult Div;
   3935     // Go from inner nested loop to outer.
   3936     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
   3937       LoopIterationSpace &IS = IterSpaces[Cnt];
   3938       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
   3939       // Build: Iter = (IV / Div) % IS.NumIters
   3940       // where Div is product of previous iterations' IS.NumIters.
   3941       ExprResult Iter;
   3942       if (Div.isUsable()) {
   3943         Iter =
   3944             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
   3945       } else {
   3946         Iter = IV;
   3947         assert((Cnt == (int)NestedLoopCount - 1) &&
   3948                "unusable div expected on first iteration only");
   3949       }
   3950 
   3951       if (Cnt != 0 && Iter.isUsable())
   3952         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
   3953                                   IS.NumIterations);
   3954       if (!Iter.isUsable()) {
   3955         HasErrors = true;
   3956         break;
   3957       }
   3958 
   3959       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
   3960       auto *CounterVar = buildDeclRefExpr(
   3961           SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
   3962           IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
   3963           /*RefersToCapture=*/true);
   3964       ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
   3965                                          IS.CounterInit);
   3966       if (!Init.isUsable()) {
   3967         HasErrors = true;
   3968         break;
   3969       }
   3970       ExprResult Update =
   3971           BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
   3972                              IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
   3973       if (!Update.isUsable()) {
   3974         HasErrors = true;
   3975         break;
   3976       }
   3977 
   3978       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
   3979       ExprResult Final = BuildCounterUpdate(
   3980           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
   3981           IS.NumIterations, IS.CounterStep, IS.Subtract);
   3982       if (!Final.isUsable()) {
   3983         HasErrors = true;
   3984         break;
   3985       }
   3986 
   3987       // Build Div for the next iteration: Div <- Div * IS.NumIters
   3988       if (Cnt != 0) {
   3989         if (Div.isUnset())
   3990           Div = IS.NumIterations;
   3991         else
   3992           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
   3993                                    IS.NumIterations);
   3994 
   3995         // Add parentheses (for debugging purposes only).
   3996         if (Div.isUsable())
   3997           Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
   3998         if (!Div.isUsable()) {
   3999           HasErrors = true;
   4000           break;
   4001         }
   4002       }
   4003       if (!Update.isUsable() || !Final.isUsable()) {
   4004         HasErrors = true;
   4005         break;
   4006       }
   4007       // Save results
   4008       Built.Counters[Cnt] = IS.CounterVar;
   4009       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
   4010       Built.Inits[Cnt] = Init.get();
   4011       Built.Updates[Cnt] = Update.get();
   4012       Built.Finals[Cnt] = Final.get();
   4013     }
   4014   }
   4015 
   4016   if (HasErrors)
   4017     return 0;
   4018 
   4019   // Save results
   4020   Built.IterationVarRef = IV.get();
   4021   Built.LastIteration = LastIteration.get();
   4022   Built.NumIterations = NumIterations.get();
   4023   Built.CalcLastIteration =
   4024       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
   4025   Built.PreCond = PreCond.get();
   4026   Built.Cond = Cond.get();
   4027   Built.Init = Init.get();
   4028   Built.Inc = Inc.get();
   4029   Built.LB = LB.get();
   4030   Built.UB = UB.get();
   4031   Built.IL = IL.get();
   4032   Built.ST = ST.get();
   4033   Built.EUB = EUB.get();
   4034   Built.NLB = NextLB.get();
   4035   Built.NUB = NextUB.get();
   4036 
   4037   return NestedLoopCount;
   4038 }
   4039 
   4040 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
   4041   auto CollapseClauses =
   4042       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
   4043   if (CollapseClauses.begin() != CollapseClauses.end())
   4044     return (*CollapseClauses.begin())->getNumForLoops();
   4045   return nullptr;
   4046 }
   4047 
   4048 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
   4049   auto OrderedClauses =
   4050       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
   4051   if (OrderedClauses.begin() != OrderedClauses.end())
   4052     return (*OrderedClauses.begin())->getNumForLoops();
   4053   return nullptr;
   4054 }
   4055 
   4056 static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
   4057                                       const Expr *Safelen) {
   4058   llvm::APSInt SimdlenRes, SafelenRes;
   4059   if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
   4060       Simdlen->isInstantiationDependent() ||
   4061       Simdlen->containsUnexpandedParameterPack())
   4062     return false;
   4063   if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
   4064       Safelen->isInstantiationDependent() ||
   4065       Safelen->containsUnexpandedParameterPack())
   4066     return false;
   4067   Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
   4068   Safelen->EvaluateAsInt(SafelenRes, S.Context);
   4069   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
   4070   // If both simdlen and safelen clauses are specified, the value of the simdlen
   4071   // parameter must be less than or equal to the value of the safelen parameter.
   4072   if (SimdlenRes > SafelenRes) {
   4073     S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
   4074         << Simdlen->getSourceRange() << Safelen->getSourceRange();
   4075     return true;
   4076   }
   4077   return false;
   4078 }
   4079 
   4080 StmtResult Sema::ActOnOpenMPSimdDirective(
   4081     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
   4082     SourceLocation EndLoc,
   4083     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
   4084   if (!AStmt)
   4085     return StmtError();
   4086 
   4087   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4088   OMPLoopDirective::HelperExprs B;
   4089   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
   4090   // define the nested loops number.
   4091   unsigned NestedLoopCount = CheckOpenMPLoop(
   4092       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
   4093       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
   4094   if (NestedLoopCount == 0)
   4095     return StmtError();
   4096 
   4097   assert((CurContext->isDependentContext() || B.builtAll()) &&
   4098          "omp simd loop exprs were not built");
   4099 
   4100   if (!CurContext->isDependentContext()) {
   4101     // Finalize the clauses that need pre-built expressions for CodeGen.
   4102     for (auto C : Clauses) {
   4103       if (auto LC = dyn_cast<OMPLinearClause>(C))
   4104         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
   4105                                      B.NumIterations, *this, CurScope))
   4106           return StmtError();
   4107     }
   4108   }
   4109 
   4110   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
   4111   // If both simdlen and safelen clauses are specified, the value of the simdlen
   4112   // parameter must be less than or equal to the value of the safelen parameter.
   4113   OMPSafelenClause *Safelen = nullptr;
   4114   OMPSimdlenClause *Simdlen = nullptr;
   4115   for (auto *Clause : Clauses) {
   4116     if (Clause->getClauseKind() == OMPC_safelen)
   4117       Safelen = cast<OMPSafelenClause>(Clause);
   4118     else if (Clause->getClauseKind() == OMPC_simdlen)
   4119       Simdlen = cast<OMPSimdlenClause>(Clause);
   4120     if (Safelen && Simdlen)
   4121       break;
   4122   }
   4123   if (Simdlen && Safelen &&
   4124       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
   4125                                 Safelen->getSafelen()))
   4126     return StmtError();
   4127 
   4128   getCurFunction()->setHasBranchProtectedScope();
   4129   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
   4130                                   Clauses, AStmt, B);
   4131 }
   4132 
   4133 StmtResult Sema::ActOnOpenMPForDirective(
   4134     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
   4135     SourceLocation EndLoc,
   4136     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
   4137   if (!AStmt)
   4138     return StmtError();
   4139 
   4140   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4141   OMPLoopDirective::HelperExprs B;
   4142   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
   4143   // define the nested loops number.
   4144   unsigned NestedLoopCount = CheckOpenMPLoop(
   4145       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
   4146       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
   4147   if (NestedLoopCount == 0)
   4148     return StmtError();
   4149 
   4150   assert((CurContext->isDependentContext() || B.builtAll()) &&
   4151          "omp for loop exprs were not built");
   4152 
   4153   if (!CurContext->isDependentContext()) {
   4154     // Finalize the clauses that need pre-built expressions for CodeGen.
   4155     for (auto C : Clauses) {
   4156       if (auto LC = dyn_cast<OMPLinearClause>(C))
   4157         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
   4158                                      B.NumIterations, *this, CurScope))
   4159           return StmtError();
   4160     }
   4161   }
   4162 
   4163   getCurFunction()->setHasBranchProtectedScope();
   4164   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
   4165                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
   4166 }
   4167 
   4168 StmtResult Sema::ActOnOpenMPForSimdDirective(
   4169     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
   4170     SourceLocation EndLoc,
   4171     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
   4172   if (!AStmt)
   4173     return StmtError();
   4174 
   4175   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4176   OMPLoopDirective::HelperExprs B;
   4177   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
   4178   // define the nested loops number.
   4179   unsigned NestedLoopCount =
   4180       CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
   4181                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
   4182                       VarsWithImplicitDSA, B);
   4183   if (NestedLoopCount == 0)
   4184     return StmtError();
   4185 
   4186   assert((CurContext->isDependentContext() || B.builtAll()) &&
   4187          "omp for simd loop exprs were not built");
   4188 
   4189   if (!CurContext->isDependentContext()) {
   4190     // Finalize the clauses that need pre-built expressions for CodeGen.
   4191     for (auto C : Clauses) {
   4192       if (auto LC = dyn_cast<OMPLinearClause>(C))
   4193         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
   4194                                      B.NumIterations, *this, CurScope))
   4195           return StmtError();
   4196     }
   4197   }
   4198 
   4199   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
   4200   // If both simdlen and safelen clauses are specified, the value of the simdlen
   4201   // parameter must be less than or equal to the value of the safelen parameter.
   4202   OMPSafelenClause *Safelen = nullptr;
   4203   OMPSimdlenClause *Simdlen = nullptr;
   4204   for (auto *Clause : Clauses) {
   4205     if (Clause->getClauseKind() == OMPC_safelen)
   4206       Safelen = cast<OMPSafelenClause>(Clause);
   4207     else if (Clause->getClauseKind() == OMPC_simdlen)
   4208       Simdlen = cast<OMPSimdlenClause>(Clause);
   4209     if (Safelen && Simdlen)
   4210       break;
   4211   }
   4212   if (Simdlen && Safelen &&
   4213       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
   4214                                 Safelen->getSafelen()))
   4215     return StmtError();
   4216 
   4217   getCurFunction()->setHasBranchProtectedScope();
   4218   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
   4219                                      Clauses, AStmt, B);
   4220 }
   4221 
   4222 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
   4223                                               Stmt *AStmt,
   4224                                               SourceLocation StartLoc,
   4225                                               SourceLocation EndLoc) {
   4226   if (!AStmt)
   4227     return StmtError();
   4228 
   4229   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4230   auto BaseStmt = AStmt;
   4231   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
   4232     BaseStmt = CS->getCapturedStmt();
   4233   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
   4234     auto S = C->children();
   4235     if (S.begin() == S.end())
   4236       return StmtError();
   4237     // All associated statements must be '#pragma omp section' except for
   4238     // the first one.
   4239     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
   4240       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
   4241         if (SectionStmt)
   4242           Diag(SectionStmt->getLocStart(),
   4243                diag::err_omp_sections_substmt_not_section);
   4244         return StmtError();
   4245       }
   4246       cast<OMPSectionDirective>(SectionStmt)
   4247           ->setHasCancel(DSAStack->isCancelRegion());
   4248     }
   4249   } else {
   4250     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
   4251     return StmtError();
   4252   }
   4253 
   4254   getCurFunction()->setHasBranchProtectedScope();
   4255 
   4256   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
   4257                                       DSAStack->isCancelRegion());
   4258 }
   4259 
   4260 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
   4261                                              SourceLocation StartLoc,
   4262                                              SourceLocation EndLoc) {
   4263   if (!AStmt)
   4264     return StmtError();
   4265 
   4266   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4267 
   4268   getCurFunction()->setHasBranchProtectedScope();
   4269   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
   4270 
   4271   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
   4272                                      DSAStack->isCancelRegion());
   4273 }
   4274 
   4275 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
   4276                                             Stmt *AStmt,
   4277                                             SourceLocation StartLoc,
   4278                                             SourceLocation EndLoc) {
   4279   if (!AStmt)
   4280     return StmtError();
   4281 
   4282   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4283 
   4284   getCurFunction()->setHasBranchProtectedScope();
   4285 
   4286   // OpenMP [2.7.3, single Construct, Restrictions]
   4287   // The copyprivate clause must not be used with the nowait clause.
   4288   OMPClause *Nowait = nullptr;
   4289   OMPClause *Copyprivate = nullptr;
   4290   for (auto *Clause : Clauses) {
   4291     if (Clause->getClauseKind() == OMPC_nowait)
   4292       Nowait = Clause;
   4293     else if (Clause->getClauseKind() == OMPC_copyprivate)
   4294       Copyprivate = Clause;
   4295     if (Copyprivate && Nowait) {
   4296       Diag(Copyprivate->getLocStart(),
   4297            diag::err_omp_single_copyprivate_with_nowait);
   4298       Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
   4299       return StmtError();
   4300     }
   4301   }
   4302 
   4303   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
   4304 }
   4305 
   4306 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
   4307                                             SourceLocation StartLoc,
   4308                                             SourceLocation EndLoc) {
   4309   if (!AStmt)
   4310     return StmtError();
   4311 
   4312   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4313 
   4314   getCurFunction()->setHasBranchProtectedScope();
   4315 
   4316   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
   4317 }
   4318 
   4319 StmtResult Sema::ActOnOpenMPCriticalDirective(
   4320     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
   4321     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
   4322   if (!AStmt)
   4323     return StmtError();
   4324 
   4325   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4326 
   4327   bool ErrorFound = false;
   4328   llvm::APSInt Hint;
   4329   SourceLocation HintLoc;
   4330   bool DependentHint = false;
   4331   for (auto *C : Clauses) {
   4332     if (C->getClauseKind() == OMPC_hint) {
   4333       if (!DirName.getName()) {
   4334         Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
   4335         ErrorFound = true;
   4336       }
   4337       Expr *E = cast<OMPHintClause>(C)->getHint();
   4338       if (E->isTypeDependent() || E->isValueDependent() ||
   4339           E->isInstantiationDependent())
   4340         DependentHint = true;
   4341       else {
   4342         Hint = E->EvaluateKnownConstInt(Context);
   4343         HintLoc = C->getLocStart();
   4344       }
   4345     }
   4346   }
   4347   if (ErrorFound)
   4348     return StmtError();
   4349   auto Pair = DSAStack->getCriticalWithHint(DirName);
   4350   if (Pair.first && DirName.getName() && !DependentHint) {
   4351     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
   4352       Diag(StartLoc, diag::err_omp_critical_with_hint);
   4353       if (HintLoc.isValid()) {
   4354         Diag(HintLoc, diag::note_omp_critical_hint_here)
   4355             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
   4356       } else
   4357         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
   4358       if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
   4359         Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
   4360             << 1
   4361             << C->getHint()->EvaluateKnownConstInt(Context).toString(
   4362                    /*Radix=*/10, /*Signed=*/false);
   4363       } else
   4364         Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
   4365     }
   4366   }
   4367 
   4368   getCurFunction()->setHasBranchProtectedScope();
   4369 
   4370   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
   4371                                            Clauses, AStmt);
   4372   if (!Pair.first && DirName.getName() && !DependentHint)
   4373     DSAStack->addCriticalWithHint(Dir, Hint);
   4374   return Dir;
   4375 }
   4376 
   4377 StmtResult Sema::ActOnOpenMPParallelForDirective(
   4378     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
   4379     SourceLocation EndLoc,
   4380     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
   4381   if (!AStmt)
   4382     return StmtError();
   4383 
   4384   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
   4385   // 1.2.2 OpenMP Language Terminology
   4386   // Structured block - An executable statement with a single entry at the
   4387   // top and a single exit at the bottom.
   4388   // The point of exit cannot be a branch out of the structured block.
   4389   // longjmp() and throw() must not violate the entry/exit criteria.
   4390   CS->getCapturedDecl()->setNothrow();
   4391 
   4392   OMPLoopDirective::HelperExprs B;
   4393   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
   4394   // define the nested loops number.
   4395   unsigned NestedLoopCount =
   4396       CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
   4397                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
   4398                       VarsWithImplicitDSA, B);
   4399   if (NestedLoopCount == 0)
   4400     return StmtError();
   4401 
   4402   assert((CurContext->isDependentContext() || B.builtAll()) &&
   4403          "omp parallel for loop exprs were not built");
   4404 
   4405   if (!CurContext->isDependentContext()) {
   4406     // Finalize the clauses that need pre-built expressions for CodeGen.
   4407     for (auto C : Clauses) {
   4408       if (auto LC = dyn_cast<OMPLinearClause>(C))
   4409         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
   4410                                      B.NumIterations, *this, CurScope))
   4411           return StmtError();
   4412     }
   4413   }
   4414 
   4415   getCurFunction()->setHasBranchProtectedScope();
   4416   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
   4417                                          NestedLoopCount, Clauses, AStmt, B,
   4418                                          DSAStack->isCancelRegion());
   4419 }
   4420 
   4421 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
   4422     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
   4423     SourceLocation EndLoc,
   4424     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
   4425   if (!AStmt)
   4426     return StmtError();
   4427 
   4428   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
   4429   // 1.2.2 OpenMP Language Terminology
   4430   // Structured block - An executable statement with a single entry at the
   4431   // top and a single exit at the bottom.
   4432   // The point of exit cannot be a branch out of the structured block.
   4433   // longjmp() and throw() must not violate the entry/exit criteria.
   4434   CS->getCapturedDecl()->setNothrow();
   4435 
   4436   OMPLoopDirective::HelperExprs B;
   4437   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
   4438   // define the nested loops number.
   4439   unsigned NestedLoopCount =
   4440       CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
   4441                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
   4442                       VarsWithImplicitDSA, B);
   4443   if (NestedLoopCount == 0)
   4444     return StmtError();
   4445 
   4446   if (!CurContext->isDependentContext()) {
   4447     // Finalize the clauses that need pre-built expressions for CodeGen.
   4448     for (auto C : Clauses) {
   4449       if (auto LC = dyn_cast<OMPLinearClause>(C))
   4450         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
   4451                                      B.NumIterations, *this, CurScope))
   4452           return StmtError();
   4453     }
   4454   }
   4455 
   4456   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
   4457   // If both simdlen and safelen clauses are specified, the value of the simdlen
   4458   // parameter must be less than or equal to the value of the safelen parameter.
   4459   OMPSafelenClause *Safelen = nullptr;
   4460   OMPSimdlenClause *Simdlen = nullptr;
   4461   for (auto *Clause : Clauses) {
   4462     if (Clause->getClauseKind() == OMPC_safelen)
   4463       Safelen = cast<OMPSafelenClause>(Clause);
   4464     else if (Clause->getClauseKind() == OMPC_simdlen)
   4465       Simdlen = cast<OMPSimdlenClause>(Clause);
   4466     if (Safelen && Simdlen)
   4467       break;
   4468   }
   4469   if (Simdlen && Safelen &&
   4470       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
   4471                                 Safelen->getSafelen()))
   4472     return StmtError();
   4473 
   4474   getCurFunction()->setHasBranchProtectedScope();
   4475   return OMPParallelForSimdDirective::Create(
   4476       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
   4477 }
   4478 
   4479 StmtResult
   4480 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
   4481                                            Stmt *AStmt, SourceLocation StartLoc,
   4482                                            SourceLocation EndLoc) {
   4483   if (!AStmt)
   4484     return StmtError();
   4485 
   4486   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4487   auto BaseStmt = AStmt;
   4488   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
   4489     BaseStmt = CS->getCapturedStmt();
   4490   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
   4491     auto S = C->children();
   4492     if (S.begin() == S.end())
   4493       return StmtError();
   4494     // All associated statements must be '#pragma omp section' except for
   4495     // the first one.
   4496     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
   4497       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
   4498         if (SectionStmt)
   4499           Diag(SectionStmt->getLocStart(),
   4500                diag::err_omp_parallel_sections_substmt_not_section);
   4501         return StmtError();
   4502       }
   4503       cast<OMPSectionDirective>(SectionStmt)
   4504           ->setHasCancel(DSAStack->isCancelRegion());
   4505     }
   4506   } else {
   4507     Diag(AStmt->getLocStart(),
   4508          diag::err_omp_parallel_sections_not_compound_stmt);
   4509     return StmtError();
   4510   }
   4511 
   4512   getCurFunction()->setHasBranchProtectedScope();
   4513 
   4514   return OMPParallelSectionsDirective::Create(
   4515       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
   4516 }
   4517 
   4518 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
   4519                                           Stmt *AStmt, SourceLocation StartLoc,
   4520                                           SourceLocation EndLoc) {
   4521   if (!AStmt)
   4522     return StmtError();
   4523 
   4524   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
   4525   // 1.2.2 OpenMP Language Terminology
   4526   // Structured block - An executable statement with a single entry at the
   4527   // top and a single exit at the bottom.
   4528   // The point of exit cannot be a branch out of the structured block.
   4529   // longjmp() and throw() must not violate the entry/exit criteria.
   4530   CS->getCapturedDecl()->setNothrow();
   4531 
   4532   getCurFunction()->setHasBranchProtectedScope();
   4533 
   4534   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
   4535                                   DSAStack->isCancelRegion());
   4536 }
   4537 
   4538 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
   4539                                                SourceLocation EndLoc) {
   4540   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
   4541 }
   4542 
   4543 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
   4544                                              SourceLocation EndLoc) {
   4545   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
   4546 }
   4547 
   4548 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
   4549                                               SourceLocation EndLoc) {
   4550   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
   4551 }
   4552 
   4553 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
   4554                                                SourceLocation StartLoc,
   4555                                                SourceLocation EndLoc) {
   4556   if (!AStmt)
   4557     return StmtError();
   4558 
   4559   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4560 
   4561   getCurFunction()->setHasBranchProtectedScope();
   4562 
   4563   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
   4564 }
   4565 
   4566 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
   4567                                            SourceLocation StartLoc,
   4568                                            SourceLocation EndLoc) {
   4569   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
   4570   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
   4571 }
   4572 
   4573 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
   4574                                              Stmt *AStmt,
   4575                                              SourceLocation StartLoc,
   4576                                              SourceLocation EndLoc) {
   4577   OMPClause *DependFound = nullptr;
   4578   OMPClause *DependSourceClause = nullptr;
   4579   bool ErrorFound = false;
   4580   OMPThreadsClause *TC = nullptr;
   4581   OMPSIMDClause *SC = nullptr;
   4582   for (auto *C : Clauses) {
   4583     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
   4584       DependFound = C;
   4585       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
   4586         if (DependSourceClause) {
   4587           Diag(C->getLocStart(), diag::err_omp_more_one_clause)
   4588               << getOpenMPDirectiveName(OMPD_ordered)
   4589               << getOpenMPClauseName(OMPC_depend) << 2;
   4590           ErrorFound = true;
   4591         } else
   4592           DependSourceClause = C;
   4593       }
   4594     } else if (C->getClauseKind() == OMPC_threads)
   4595       TC = cast<OMPThreadsClause>(C);
   4596     else if (C->getClauseKind() == OMPC_simd)
   4597       SC = cast<OMPSIMDClause>(C);
   4598   }
   4599   if (!ErrorFound && !SC &&
   4600       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
   4601     // OpenMP [2.8.1,simd Construct, Restrictions]
   4602     // An ordered construct with the simd clause is the only OpenMP construct
   4603     // that can appear in the simd region.
   4604     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
   4605     ErrorFound = true;
   4606   } else if (DependFound && (TC || SC)) {
   4607     Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
   4608         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
   4609     ErrorFound = true;
   4610   } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
   4611     Diag(DependFound->getLocStart(),
   4612          diag::err_omp_ordered_directive_without_param);
   4613     ErrorFound = true;
   4614   } else if (TC || Clauses.empty()) {
   4615     if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
   4616       SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
   4617       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
   4618           << (TC != nullptr);
   4619       Diag(Param->getLocStart(), diag::note_omp_ordered_param);
   4620       ErrorFound = true;
   4621     }
   4622   }
   4623   if ((!AStmt && !DependFound) || ErrorFound)
   4624     return StmtError();
   4625 
   4626   if (AStmt) {
   4627     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   4628 
   4629     getCurFunction()->setHasBranchProtectedScope();
   4630   }
   4631 
   4632   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
   4633 }
   4634 
   4635 namespace {
   4636 /// \brief Helper class for checking expression in 'omp atomic [update]'
   4637 /// construct.
   4638 class OpenMPAtomicUpdateChecker {
   4639   /// \brief Error results for atomic update expressions.
   4640   enum ExprAnalysisErrorCode {
   4641     /// \brief A statement is not an expression statement.
   4642     NotAnExpression,
   4643     /// \brief Expression is not builtin binary or unary operation.
   4644     NotABinaryOrUnaryExpression,
   4645     /// \brief Unary operation is not post-/pre- increment/decrement operation.
   4646     NotAnUnaryIncDecExpression,
   4647     /// \brief An expression is not of scalar type.
   4648     NotAScalarType,
   4649     /// \brief A binary operation is not an assignment operation.
   4650     NotAnAssignmentOp,
   4651     /// \brief RHS part of the binary operation is not a binary expression.
   4652     NotABinaryExpression,
   4653     /// \brief RHS part is not additive/multiplicative/shift/biwise binary
   4654     /// expression.
   4655     NotABinaryOperator,
   4656     /// \brief RHS binary operation does not have reference to the updated LHS
   4657     /// part.
   4658     NotAnUpdateExpression,
   4659     /// \brief No errors is found.
   4660     NoError
   4661   };
   4662   /// \brief Reference to Sema.
   4663   Sema &SemaRef;
   4664   /// \brief A location for note diagnostics (when error is found).
   4665   SourceLocation NoteLoc;
   4666   /// \brief 'x' lvalue part of the source atomic expression.
   4667   Expr *X;
   4668   /// \brief 'expr' rvalue part of the source atomic expression.
   4669   Expr *E;
   4670   /// \brief Helper expression of the form
   4671   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
   4672   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
   4673   Expr *UpdateExpr;
   4674   /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
   4675   /// important for non-associative operations.
   4676   bool IsXLHSInRHSPart;
   4677   BinaryOperatorKind Op;
   4678   SourceLocation OpLoc;
   4679   /// \brief true if the source expression is a postfix unary operation, false
   4680   /// if it is a prefix unary operation.
   4681   bool IsPostfixUpdate;
   4682 
   4683 public:
   4684   OpenMPAtomicUpdateChecker(Sema &SemaRef)
   4685       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
   4686         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
   4687   /// \brief Check specified statement that it is suitable for 'atomic update'
   4688   /// constructs and extract 'x', 'expr' and Operation from the original
   4689   /// expression. If DiagId and NoteId == 0, then only check is performed
   4690   /// without error notification.
   4691   /// \param DiagId Diagnostic which should be emitted if error is found.
   4692   /// \param NoteId Diagnostic note for the main error message.
   4693   /// \return true if statement is not an update expression, false otherwise.
   4694   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
   4695   /// \brief Return the 'x' lvalue part of the source atomic expression.
   4696   Expr *getX() const { return X; }
   4697   /// \brief Return the 'expr' rvalue part of the source atomic expression.
   4698   Expr *getExpr() const { return E; }
   4699   /// \brief Return the update expression used in calculation of the updated
   4700   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
   4701   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
   4702   Expr *getUpdateExpr() const { return UpdateExpr; }
   4703   /// \brief Return true if 'x' is LHS in RHS part of full update expression,
   4704   /// false otherwise.
   4705   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
   4706 
   4707   /// \brief true if the source expression is a postfix unary operation, false
   4708   /// if it is a prefix unary operation.
   4709   bool isPostfixUpdate() const { return IsPostfixUpdate; }
   4710 
   4711 private:
   4712   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
   4713                             unsigned NoteId = 0);
   4714 };
   4715 } // namespace
   4716 
   4717 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
   4718     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
   4719   ExprAnalysisErrorCode ErrorFound = NoError;
   4720   SourceLocation ErrorLoc, NoteLoc;
   4721   SourceRange ErrorRange, NoteRange;
   4722   // Allowed constructs are:
   4723   //  x = x binop expr;
   4724   //  x = expr binop x;
   4725   if (AtomicBinOp->getOpcode() == BO_Assign) {
   4726     X = AtomicBinOp->getLHS();
   4727     if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
   4728             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
   4729       if (AtomicInnerBinOp->isMultiplicativeOp() ||
   4730           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
   4731           AtomicInnerBinOp->isBitwiseOp()) {
   4732         Op = AtomicInnerBinOp->getOpcode();
   4733         OpLoc = AtomicInnerBinOp->getOperatorLoc();
   4734         auto *LHS = AtomicInnerBinOp->getLHS();
   4735         auto *RHS = AtomicInnerBinOp->getRHS();
   4736         llvm::FoldingSetNodeID XId, LHSId, RHSId;
   4737         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
   4738                                           /*Canonical=*/true);
   4739         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
   4740                                             /*Canonical=*/true);
   4741         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
   4742                                             /*Canonical=*/true);
   4743         if (XId == LHSId) {
   4744           E = RHS;
   4745           IsXLHSInRHSPart = true;
   4746         } else if (XId == RHSId) {
   4747           E = LHS;
   4748           IsXLHSInRHSPart = false;
   4749         } else {
   4750           ErrorLoc = AtomicInnerBinOp->getExprLoc();
   4751           ErrorRange = AtomicInnerBinOp->getSourceRange();
   4752           NoteLoc = X->getExprLoc();
   4753           NoteRange = X->getSourceRange();
   4754           ErrorFound = NotAnUpdateExpression;
   4755         }
   4756       } else {
   4757         ErrorLoc = AtomicInnerBinOp->getExprLoc();
   4758         ErrorRange = AtomicInnerBinOp->getSourceRange();
   4759         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
   4760         NoteRange = SourceRange(NoteLoc, NoteLoc);
   4761         ErrorFound = NotABinaryOperator;
   4762       }
   4763     } else {
   4764       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
   4765       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
   4766       ErrorFound = NotABinaryExpression;
   4767     }
   4768   } else {
   4769     ErrorLoc = AtomicBinOp->getExprLoc();
   4770     ErrorRange = AtomicBinOp->getSourceRange();
   4771     NoteLoc = AtomicBinOp->getOperatorLoc();
   4772     NoteRange = SourceRange(NoteLoc, NoteLoc);
   4773     ErrorFound = NotAnAssignmentOp;
   4774   }
   4775   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
   4776     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
   4777     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
   4778     return true;
   4779   } else if (SemaRef.CurContext->isDependentContext())
   4780     E = X = UpdateExpr = nullptr;
   4781   return ErrorFound != NoError;
   4782 }
   4783 
   4784 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
   4785                                                unsigned NoteId) {
   4786   ExprAnalysisErrorCode ErrorFound = NoError;
   4787   SourceLocation ErrorLoc, NoteLoc;
   4788   SourceRange ErrorRange, NoteRange;
   4789   // Allowed constructs are:
   4790   //  x++;
   4791   //  x--;
   4792   //  ++x;
   4793   //  --x;
   4794   //  x binop= expr;
   4795   //  x = x binop expr;
   4796   //  x = expr binop x;
   4797   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
   4798     AtomicBody = AtomicBody->IgnoreParenImpCasts();
   4799     if (AtomicBody->getType()->isScalarType() ||
   4800         AtomicBody->isInstantiationDependent()) {
   4801       if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
   4802               AtomicBody->IgnoreParenImpCasts())) {
   4803         // Check for Compound Assignment Operation
   4804         Op = BinaryOperator::getOpForCompoundAssignment(
   4805             AtomicCompAssignOp->getOpcode());
   4806         OpLoc = AtomicCompAssignOp->getOperatorLoc();
   4807         E = AtomicCompAssignOp->getRHS();
   4808         X = AtomicCompAssignOp->getLHS();
   4809         IsXLHSInRHSPart = true;
   4810       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
   4811                      AtomicBody->IgnoreParenImpCasts())) {
   4812         // Check for Binary Operation
   4813         if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
   4814           return true;
   4815       } else if (auto *AtomicUnaryOp =
   4816                  dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
   4817         // Check for Unary Operation
   4818         if (AtomicUnaryOp->isIncrementDecrementOp()) {
   4819           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
   4820           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
   4821           OpLoc = AtomicUnaryOp->getOperatorLoc();
   4822           X = AtomicUnaryOp->getSubExpr();
   4823           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
   4824           IsXLHSInRHSPart = true;
   4825         } else {
   4826           ErrorFound = NotAnUnaryIncDecExpression;
   4827           ErrorLoc = AtomicUnaryOp->getExprLoc();
   4828           ErrorRange = AtomicUnaryOp->getSourceRange();
   4829           NoteLoc = AtomicUnaryOp->getOperatorLoc();
   4830           NoteRange = SourceRange(NoteLoc, NoteLoc);
   4831         }
   4832       } else if (!AtomicBody->isInstantiationDependent()) {
   4833         ErrorFound = NotABinaryOrUnaryExpression;
   4834         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
   4835         NoteRange = ErrorRange = AtomicBody->getSourceRange();
   4836       }
   4837     } else {
   4838       ErrorFound = NotAScalarType;
   4839       NoteLoc = ErrorLoc = AtomicBody->getLocStart();
   4840       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
   4841     }
   4842   } else {
   4843     ErrorFound = NotAnExpression;
   4844     NoteLoc = ErrorLoc = S->getLocStart();
   4845     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
   4846   }
   4847   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
   4848     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
   4849     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
   4850     return true;
   4851   } else if (SemaRef.CurContext->isDependentContext())
   4852     E = X = UpdateExpr = nullptr;
   4853   if (ErrorFound == NoError && E && X) {
   4854     // Build an update expression of form 'OpaqueValueExpr(x) binop
   4855     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
   4856     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
   4857     auto *OVEX = new (SemaRef.getASTContext())
   4858         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
   4859     auto *OVEExpr = new (SemaRef.getASTContext())
   4860         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
   4861     auto Update =
   4862         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
   4863                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
   4864     if (Update.isInvalid())
   4865       return true;
   4866     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
   4867                                                Sema::AA_Casting);
   4868     if (Update.isInvalid())
   4869       return true;
   4870     UpdateExpr = Update.get();
   4871   }
   4872   return ErrorFound != NoError;
   4873 }
   4874 
   4875 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
   4876                                             Stmt *AStmt,
   4877                                             SourceLocation StartLoc,
   4878                                             SourceLocation EndLoc) {
   4879   if (!AStmt)
   4880     return StmtError();
   4881 
   4882   auto CS = cast<CapturedStmt>(AStmt);
   4883   // 1.2.2 OpenMP Language Terminology
   4884   // Structured block - An executable statement with a single entry at the
   4885   // top and a single exit at the bottom.
   4886   // The point of exit cannot be a branch out of the structured block.
   4887   // longjmp() and throw() must not violate the entry/exit criteria.
   4888   OpenMPClauseKind AtomicKind = OMPC_unknown;
   4889   SourceLocation AtomicKindLoc;
   4890   for (auto *C : Clauses) {
   4891     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
   4892         C->getClauseKind() == OMPC_update ||
   4893         C->getClauseKind() == OMPC_capture) {
   4894       if (AtomicKind != OMPC_unknown) {
   4895         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
   4896             << SourceRange(C->getLocStart(), C->getLocEnd());
   4897         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
   4898             << getOpenMPClauseName(AtomicKind);
   4899       } else {
   4900         AtomicKind = C->getClauseKind();
   4901         AtomicKindLoc = C->getLocStart();
   4902       }
   4903     }
   4904   }
   4905 
   4906   auto Body = CS->getCapturedStmt();
   4907   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
   4908     Body = EWC->getSubExpr();
   4909 
   4910   Expr *X = nullptr;
   4911   Expr *V = nullptr;
   4912   Expr *E = nullptr;
   4913   Expr *UE = nullptr;
   4914   bool IsXLHSInRHSPart = false;
   4915   bool IsPostfixUpdate = false;
   4916   // OpenMP [2.12.6, atomic Construct]
   4917   // In the next expressions:
   4918   // * x and v (as applicable) are both l-value expressions with scalar type.
   4919   // * During the execution of an atomic region, multiple syntactic
   4920   // occurrences of x must designate the same storage location.
   4921   // * Neither of v and expr (as applicable) may access the storage location
   4922   // designated by x.
   4923   // * Neither of x and expr (as applicable) may access the storage location
   4924   // designated by v.
   4925   // * expr is an expression with scalar type.
   4926   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
   4927   // * binop, binop=, ++, and -- are not overloaded operators.
   4928   // * The expression x binop expr must be numerically equivalent to x binop
   4929   // (expr). This requirement is satisfied if the operators in expr have
   4930   // precedence greater than binop, or by using parentheses around expr or
   4931   // subexpressions of expr.
   4932   // * The expression expr binop x must be numerically equivalent to (expr)
   4933   // binop x. This requirement is satisfied if the operators in expr have
   4934   // precedence equal to or greater than binop, or by using parentheses around
   4935   // expr or subexpressions of expr.
   4936   // * For forms that allow multiple occurrences of x, the number of times
   4937   // that x is evaluated is unspecified.
   4938   if (AtomicKind == OMPC_read) {
   4939     enum {
   4940       NotAnExpression,
   4941       NotAnAssignmentOp,
   4942       NotAScalarType,
   4943       NotAnLValue,
   4944       NoError
   4945     } ErrorFound = NoError;
   4946     SourceLocation ErrorLoc, NoteLoc;
   4947     SourceRange ErrorRange, NoteRange;
   4948     // If clause is read:
   4949     //  v = x;
   4950     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
   4951       auto AtomicBinOp =
   4952           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
   4953       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
   4954         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
   4955         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
   4956         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
   4957             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
   4958           if (!X->isLValue() || !V->isLValue()) {
   4959             auto NotLValueExpr = X->isLValue() ? V : X;
   4960             ErrorFound = NotAnLValue;
   4961             ErrorLoc = AtomicBinOp->getExprLoc();
   4962             ErrorRange = AtomicBinOp->getSourceRange();
   4963             NoteLoc = NotLValueExpr->getExprLoc();
   4964             NoteRange = NotLValueExpr->getSourceRange();
   4965           }
   4966         } else if (!X->isInstantiationDependent() ||
   4967                    !V->isInstantiationDependent()) {
   4968           auto NotScalarExpr =
   4969               (X->isInstantiationDependent() || X->getType()->isScalarType())
   4970                   ? V
   4971                   : X;
   4972           ErrorFound = NotAScalarType;
   4973           ErrorLoc = AtomicBinOp->getExprLoc();
   4974           ErrorRange = AtomicBinOp->getSourceRange();
   4975           NoteLoc = NotScalarExpr->getExprLoc();
   4976           NoteRange = NotScalarExpr->getSourceRange();
   4977         }
   4978       } else if (!AtomicBody->isInstantiationDependent()) {
   4979         ErrorFound = NotAnAssignmentOp;
   4980         ErrorLoc = AtomicBody->getExprLoc();
   4981         ErrorRange = AtomicBody->getSourceRange();
   4982         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
   4983                               : AtomicBody->getExprLoc();
   4984         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
   4985                                 : AtomicBody->getSourceRange();
   4986       }
   4987     } else {
   4988       ErrorFound = NotAnExpression;
   4989       NoteLoc = ErrorLoc = Body->getLocStart();
   4990       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
   4991     }
   4992     if (ErrorFound != NoError) {
   4993       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
   4994           << ErrorRange;
   4995       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
   4996                                                       << NoteRange;
   4997       return StmtError();
   4998     } else if (CurContext->isDependentContext())
   4999       V = X = nullptr;
   5000   } else if (AtomicKind == OMPC_write) {
   5001     enum {
   5002       NotAnExpression,
   5003       NotAnAssignmentOp,
   5004       NotAScalarType,
   5005       NotAnLValue,
   5006       NoError
   5007     } ErrorFound = NoError;
   5008     SourceLocation ErrorLoc, NoteLoc;
   5009     SourceRange ErrorRange, NoteRange;
   5010     // If clause is write:
   5011     //  x = expr;
   5012     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
   5013       auto AtomicBinOp =
   5014           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
   5015       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
   5016         X = AtomicBinOp->getLHS();
   5017         E = AtomicBinOp->getRHS();
   5018         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
   5019             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
   5020           if (!X->isLValue()) {
   5021             ErrorFound = NotAnLValue;
   5022             ErrorLoc = AtomicBinOp->getExprLoc();
   5023             ErrorRange = AtomicBinOp->getSourceRange();
   5024             NoteLoc = X->getExprLoc();
   5025             NoteRange = X->getSourceRange();
   5026           }
   5027         } else if (!X->isInstantiationDependent() ||
   5028                    !E->isInstantiationDependent()) {
   5029           auto NotScalarExpr =
   5030               (X->isInstantiationDependent() || X->getType()->isScalarType())
   5031                   ? E
   5032                   : X;
   5033           ErrorFound = NotAScalarType;
   5034           ErrorLoc = AtomicBinOp->getExprLoc();
   5035           ErrorRange = AtomicBinOp->getSourceRange();
   5036           NoteLoc = NotScalarExpr->getExprLoc();
   5037           NoteRange = NotScalarExpr->getSourceRange();
   5038         }
   5039       } else if (!AtomicBody->isInstantiationDependent()) {
   5040         ErrorFound = NotAnAssignmentOp;
   5041         ErrorLoc = AtomicBody->getExprLoc();
   5042         ErrorRange = AtomicBody->getSourceRange();
   5043         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
   5044                               : AtomicBody->getExprLoc();
   5045         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
   5046                                 : AtomicBody->getSourceRange();
   5047       }
   5048     } else {
   5049       ErrorFound = NotAnExpression;
   5050       NoteLoc = ErrorLoc = Body->getLocStart();
   5051       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
   5052     }
   5053     if (ErrorFound != NoError) {
   5054       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
   5055           << ErrorRange;
   5056       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
   5057                                                       << NoteRange;
   5058       return StmtError();
   5059     } else if (CurContext->isDependentContext())
   5060       E = X = nullptr;
   5061   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
   5062     // If clause is update:
   5063     //  x++;
   5064     //  x--;
   5065     //  ++x;
   5066     //  --x;
   5067     //  x binop= expr;
   5068     //  x = x binop expr;
   5069     //  x = expr binop x;
   5070     OpenMPAtomicUpdateChecker Checker(*this);
   5071     if (Checker.checkStatement(
   5072             Body, (AtomicKind == OMPC_update)
   5073                       ? diag::err_omp_atomic_update_not_expression_statement
   5074                       : diag::err_omp_atomic_not_expression_statement,
   5075             diag::note_omp_atomic_update))
   5076       return StmtError();
   5077     if (!CurContext->isDependentContext()) {
   5078       E = Checker.getExpr();
   5079       X = Checker.getX();
   5080       UE = Checker.getUpdateExpr();
   5081       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
   5082     }
   5083   } else if (AtomicKind == OMPC_capture) {
   5084     enum {
   5085       NotAnAssignmentOp,
   5086       NotACompoundStatement,
   5087       NotTwoSubstatements,
   5088       NotASpecificExpression,
   5089       NoError
   5090     } ErrorFound = NoError;
   5091     SourceLocation ErrorLoc, NoteLoc;
   5092     SourceRange ErrorRange, NoteRange;
   5093     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
   5094       // If clause is a capture:
   5095       //  v = x++;
   5096       //  v = x--;
   5097       //  v = ++x;
   5098       //  v = --x;
   5099       //  v = x binop= expr;
   5100       //  v = x = x binop expr;
   5101       //  v = x = expr binop x;
   5102       auto *AtomicBinOp =
   5103           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
   5104       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
   5105         V = AtomicBinOp->getLHS();
   5106         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
   5107         OpenMPAtomicUpdateChecker Checker(*this);
   5108         if (Checker.checkStatement(
   5109                 Body, diag::err_omp_atomic_capture_not_expression_statement,
   5110                 diag::note_omp_atomic_update))
   5111           return StmtError();
   5112         E = Checker.getExpr();
   5113         X = Checker.getX();
   5114         UE = Checker.getUpdateExpr();
   5115         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
   5116         IsPostfixUpdate = Checker.isPostfixUpdate();
   5117       } else if (!AtomicBody->isInstantiationDependent()) {
   5118         ErrorLoc = AtomicBody->getExprLoc();
   5119         ErrorRange = AtomicBody->getSourceRange();
   5120         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
   5121                               : AtomicBody->getExprLoc();
   5122         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
   5123                                 : AtomicBody->getSourceRange();
   5124         ErrorFound = NotAnAssignmentOp;
   5125       }
   5126       if (ErrorFound != NoError) {
   5127         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
   5128             << ErrorRange;
   5129         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
   5130         return StmtError();
   5131       } else if (CurContext->isDependentContext()) {
   5132         UE = V = E = X = nullptr;
   5133       }
   5134     } else {
   5135       // If clause is a capture:
   5136       //  { v = x; x = expr; }
   5137       //  { v = x; x++; }
   5138       //  { v = x; x--; }
   5139       //  { v = x; ++x; }
   5140       //  { v = x; --x; }
   5141       //  { v = x; x binop= expr; }
   5142       //  { v = x; x = x binop expr; }
   5143       //  { v = x; x = expr binop x; }
   5144       //  { x++; v = x; }
   5145       //  { x--; v = x; }
   5146       //  { ++x; v = x; }
   5147       //  { --x; v = x; }
   5148       //  { x binop= expr; v = x; }
   5149       //  { x = x binop expr; v = x; }
   5150       //  { x = expr binop x; v = x; }
   5151       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
   5152         // Check that this is { expr1; expr2; }
   5153         if (CS->size() == 2) {
   5154           auto *First = CS->body_front();
   5155           auto *Second = CS->body_back();
   5156           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
   5157             First = EWC->getSubExpr()->IgnoreParenImpCasts();
   5158           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
   5159             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
   5160           // Need to find what subexpression is 'v' and what is 'x'.
   5161           OpenMPAtomicUpdateChecker Checker(*this);
   5162           bool IsUpdateExprFound = !Checker.checkStatement(Second);
   5163           BinaryOperator *BinOp = nullptr;
   5164           if (IsUpdateExprFound) {
   5165             BinOp = dyn_cast<BinaryOperator>(First);
   5166             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
   5167           }
   5168           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
   5169             //  { v = x; x++; }
   5170             //  { v = x; x--; }
   5171             //  { v = x; ++x; }
   5172             //  { v = x; --x; }
   5173             //  { v = x; x binop= expr; }
   5174             //  { v = x; x = x binop expr; }
   5175             //  { v = x; x = expr binop x; }
   5176             // Check that the first expression has form v = x.
   5177             auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
   5178             llvm::FoldingSetNodeID XId, PossibleXId;
   5179             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
   5180             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
   5181             IsUpdateExprFound = XId == PossibleXId;
   5182             if (IsUpdateExprFound) {
   5183               V = BinOp->getLHS();
   5184               X = Checker.getX();
   5185               E = Checker.getExpr();
   5186               UE = Checker.getUpdateExpr();
   5187               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
   5188               IsPostfixUpdate = true;
   5189             }
   5190           }
   5191           if (!IsUpdateExprFound) {
   5192             IsUpdateExprFound = !Checker.checkStatement(First);
   5193             BinOp = nullptr;
   5194             if (IsUpdateExprFound) {
   5195               BinOp = dyn_cast<BinaryOperator>(Second);
   5196               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
   5197             }
   5198             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
   5199               //  { x++; v = x; }
   5200               //  { x--; v = x; }
   5201               //  { ++x; v = x; }
   5202               //  { --x; v = x; }
   5203               //  { x binop= expr; v = x; }
   5204               //  { x = x binop expr; v = x; }
   5205               //  { x = expr binop x; v = x; }
   5206               // Check that the second expression has form v = x.
   5207               auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
   5208               llvm::FoldingSetNodeID XId, PossibleXId;
   5209               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
   5210               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
   5211               IsUpdateExprFound = XId == PossibleXId;
   5212               if (IsUpdateExprFound) {
   5213                 V = BinOp->getLHS();
   5214                 X = Checker.getX();
   5215                 E = Checker.getExpr();
   5216                 UE = Checker.getUpdateExpr();
   5217                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
   5218                 IsPostfixUpdate = false;
   5219               }
   5220             }
   5221           }
   5222           if (!IsUpdateExprFound) {
   5223             //  { v = x; x = expr; }
   5224             auto *FirstExpr = dyn_cast<Expr>(First);
   5225             auto *SecondExpr = dyn_cast<Expr>(Second);
   5226             if (!FirstExpr || !SecondExpr ||
   5227                 !(FirstExpr->isInstantiationDependent() ||
   5228                   SecondExpr->isInstantiationDependent())) {
   5229               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
   5230               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
   5231                 ErrorFound = NotAnAssignmentOp;
   5232                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
   5233                                                 : First->getLocStart();
   5234                 NoteRange = ErrorRange = FirstBinOp
   5235                                              ? FirstBinOp->getSourceRange()
   5236                                              : SourceRange(ErrorLoc, ErrorLoc);
   5237               } else {
   5238                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
   5239                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
   5240                   ErrorFound = NotAnAssignmentOp;
   5241                   NoteLoc = ErrorLoc = SecondBinOp
   5242                                            ? SecondBinOp->getOperatorLoc()
   5243                                            : Second->getLocStart();
   5244                   NoteRange = ErrorRange =
   5245                       SecondBinOp ? SecondBinOp->getSourceRange()
   5246                                   : SourceRange(ErrorLoc, ErrorLoc);
   5247                 } else {
   5248                   auto *PossibleXRHSInFirst =
   5249                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
   5250                   auto *PossibleXLHSInSecond =
   5251                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
   5252                   llvm::FoldingSetNodeID X1Id, X2Id;
   5253                   PossibleXRHSInFirst->Profile(X1Id, Context,
   5254                                                /*Canonical=*/true);
   5255                   PossibleXLHSInSecond->Profile(X2Id, Context,
   5256                                                 /*Canonical=*/true);
   5257                   IsUpdateExprFound = X1Id == X2Id;
   5258                   if (IsUpdateExprFound) {
   5259                     V = FirstBinOp->getLHS();
   5260                     X = SecondBinOp->getLHS();
   5261                     E = SecondBinOp->getRHS();
   5262                     UE = nullptr;
   5263                     IsXLHSInRHSPart = false;
   5264                     IsPostfixUpdate = true;
   5265                   } else {
   5266                     ErrorFound = NotASpecificExpression;
   5267                     ErrorLoc = FirstBinOp->getExprLoc();
   5268                     ErrorRange = FirstBinOp->getSourceRange();
   5269                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
   5270                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
   5271                   }
   5272                 }
   5273               }
   5274             }
   5275           }
   5276         } else {
   5277           NoteLoc = ErrorLoc = Body->getLocStart();
   5278           NoteRange = ErrorRange =
   5279               SourceRange(Body->getLocStart(), Body->getLocStart());
   5280           ErrorFound = NotTwoSubstatements;
   5281         }
   5282       } else {
   5283         NoteLoc = ErrorLoc = Body->getLocStart();
   5284         NoteRange = ErrorRange =
   5285             SourceRange(Body->getLocStart(), Body->getLocStart());
   5286         ErrorFound = NotACompoundStatement;
   5287       }
   5288       if (ErrorFound != NoError) {
   5289         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
   5290             << ErrorRange;
   5291         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
   5292         return StmtError();
   5293       } else if (CurContext->isDependentContext()) {
   5294         UE = V = E = X = nullptr;
   5295       }
   5296     }
   5297   }
   5298 
   5299   getCurFunction()->setHasBranchProtectedScope();
   5300 
   5301   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
   5302                                     X, V, E, UE, IsXLHSInRHSPart,
   5303                                     IsPostfixUpdate);
   5304 }
   5305 
   5306 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
   5307                                             Stmt *AStmt,
   5308                                             SourceLocation StartLoc,
   5309                                             SourceLocation EndLoc) {
   5310   if (!AStmt)
   5311     return StmtError();
   5312 
   5313   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
   5314   // 1.2.2 OpenMP Language Terminology
   5315   // Structured block - An executable statement with a single entry at the
   5316   // top and a single exit at the bottom.
   5317   // The point of exit cannot be a branch out of the structured block.
   5318   // longjmp() and throw() must not violate the entry/exit criteria.
   5319   CS->getCapturedDecl()->setNothrow();
   5320 
   5321   // OpenMP [2.16, Nesting of Regions]
   5322   // If specified, a teams construct must be contained within a target
   5323   // construct. That target construct must contain no statements or directives
   5324   // outside of the teams construct.
   5325   if (DSAStack->hasInnerTeamsRegion()) {
   5326     auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
   5327     bool OMPTeamsFound = true;
   5328     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
   5329       auto I = CS->body_begin();
   5330       while (I != CS->body_end()) {
   5331         auto OED = dyn_cast<OMPExecutableDirective>(*I);
   5332         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
   5333           OMPTeamsFound = false;
   5334           break;
   5335         }
   5336         ++I;
   5337       }
   5338       assert(I != CS->body_end() && "Not found statement");
   5339       S = *I;
   5340     }
   5341     if (!OMPTeamsFound) {
   5342       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
   5343       Diag(DSAStack->getInnerTeamsRegionLoc(),
   5344            diag::note_omp_nested_teams_construct_here);
   5345       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
   5346           << isa<OMPExecutableDirective>(S);
   5347       return StmtError();
   5348     }
   5349   }
   5350 
   5351   getCurFunction()->setHasBranchProtectedScope();
   5352 
   5353   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
   5354 }
   5355 
   5356 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
   5357                                                 Stmt *AStmt,
   5358                                                 SourceLocation StartLoc,
   5359                                                 SourceLocation EndLoc) {
   5360   if (!AStmt)
   5361     return StmtError();
   5362 
   5363   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   5364 
   5365   getCurFunction()->setHasBranchProtectedScope();
   5366 
   5367   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
   5368                                         AStmt);
   5369 }
   5370 
   5371 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
   5372                                            Stmt *AStmt, SourceLocation StartLoc,
   5373                                            SourceLocation EndLoc) {
   5374   if (!AStmt)
   5375     return StmtError();
   5376 
   5377   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
   5378   // 1.2.2 OpenMP Language Terminology
   5379   // Structured block - An executable statement with a single entry at the
   5380   // top and a single exit at the bottom.
   5381   // The point of exit cannot be a branch out of the structured block.
   5382   // longjmp() and throw() must not violate the entry/exit criteria.
   5383   CS->getCapturedDecl()->setNothrow();
   5384 
   5385   getCurFunction()->setHasBranchProtectedScope();
   5386 
   5387   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
   5388 }
   5389 
   5390 StmtResult
   5391 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
   5392                                             SourceLocation EndLoc,
   5393                                             OpenMPDirectiveKind CancelRegion) {
   5394   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
   5395       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
   5396     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
   5397         << getOpenMPDirectiveName(CancelRegion);
   5398     return StmtError();
   5399   }
   5400   if (DSAStack->isParentNowaitRegion()) {
   5401     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
   5402     return StmtError();
   5403   }
   5404   if (DSAStack->isParentOrderedRegion()) {
   5405     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
   5406     return StmtError();
   5407   }
   5408   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
   5409                                                CancelRegion);
   5410 }
   5411 
   5412 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
   5413                                             SourceLocation StartLoc,
   5414                                             SourceLocation EndLoc,
   5415                                             OpenMPDirectiveKind CancelRegion) {
   5416   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
   5417       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
   5418     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
   5419         << getOpenMPDirectiveName(CancelRegion);
   5420     return StmtError();
   5421   }
   5422   if (DSAStack->isParentNowaitRegion()) {
   5423     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
   5424     return StmtError();
   5425   }
   5426   if (DSAStack->isParentOrderedRegion()) {
   5427     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
   5428     return StmtError();
   5429   }
   5430   DSAStack->setParentCancelRegion(/*Cancel=*/true);
   5431   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
   5432                                     CancelRegion);
   5433 }
   5434 
   5435 static bool checkGrainsizeNumTasksClauses(Sema &S,
   5436                                           ArrayRef<OMPClause *> Clauses) {
   5437   OMPClause *PrevClause = nullptr;
   5438   bool ErrorFound = false;
   5439   for (auto *C : Clauses) {
   5440     if (C->getClauseKind() == OMPC_grainsize ||
   5441         C->getClauseKind() == OMPC_num_tasks) {
   5442       if (!PrevClause)
   5443         PrevClause = C;
   5444       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
   5445         S.Diag(C->getLocStart(),
   5446                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
   5447             << getOpenMPClauseName(C->getClauseKind())
   5448             << getOpenMPClauseName(PrevClause->getClauseKind());
   5449         S.Diag(PrevClause->getLocStart(),
   5450                diag::note_omp_previous_grainsize_num_tasks)
   5451             << getOpenMPClauseName(PrevClause->getClauseKind());
   5452         ErrorFound = true;
   5453       }
   5454     }
   5455   }
   5456   return ErrorFound;
   5457 }
   5458 
   5459 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
   5460     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
   5461     SourceLocation EndLoc,
   5462     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
   5463   if (!AStmt)
   5464     return StmtError();
   5465 
   5466   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   5467   OMPLoopDirective::HelperExprs B;
   5468   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
   5469   // define the nested loops number.
   5470   unsigned NestedLoopCount =
   5471       CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
   5472                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
   5473                       VarsWithImplicitDSA, B);
   5474   if (NestedLoopCount == 0)
   5475     return StmtError();
   5476 
   5477   assert((CurContext->isDependentContext() || B.builtAll()) &&
   5478          "omp for loop exprs were not built");
   5479 
   5480   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
   5481   // The grainsize clause and num_tasks clause are mutually exclusive and may
   5482   // not appear on the same taskloop directive.
   5483   if (checkGrainsizeNumTasksClauses(*this, Clauses))
   5484     return StmtError();
   5485 
   5486   getCurFunction()->setHasBranchProtectedScope();
   5487   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
   5488                                       NestedLoopCount, Clauses, AStmt, B);
   5489 }
   5490 
   5491 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
   5492     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
   5493     SourceLocation EndLoc,
   5494     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
   5495   if (!AStmt)
   5496     return StmtError();
   5497 
   5498   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   5499   OMPLoopDirective::HelperExprs B;
   5500   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
   5501   // define the nested loops number.
   5502   unsigned NestedLoopCount =
   5503       CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
   5504                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
   5505                       VarsWithImplicitDSA, B);
   5506   if (NestedLoopCount == 0)
   5507     return StmtError();
   5508 
   5509   assert((CurContext->isDependentContext() || B.builtAll()) &&
   5510          "omp for loop exprs were not built");
   5511 
   5512   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
   5513   // The grainsize clause and num_tasks clause are mutually exclusive and may
   5514   // not appear on the same taskloop directive.
   5515   if (checkGrainsizeNumTasksClauses(*this, Clauses))
   5516     return StmtError();
   5517 
   5518   getCurFunction()->setHasBranchProtectedScope();
   5519   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
   5520                                           NestedLoopCount, Clauses, AStmt, B);
   5521 }
   5522 
   5523 StmtResult Sema::ActOnOpenMPDistributeDirective(
   5524     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
   5525     SourceLocation EndLoc,
   5526     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
   5527   if (!AStmt)
   5528     return StmtError();
   5529 
   5530   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
   5531   OMPLoopDirective::HelperExprs B;
   5532   // In presence of clause 'collapse' with number of loops, it will
   5533   // define the nested loops number.
   5534   unsigned NestedLoopCount =
   5535       CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
   5536                       nullptr /*ordered not a clause on distribute*/, AStmt,
   5537                       *this, *DSAStack, VarsWithImplicitDSA, B);
   5538   if (NestedLoopCount == 0)
   5539     return StmtError();
   5540 
   5541   assert((CurContext->isDependentContext() || B.builtAll()) &&
   5542          "omp for loop exprs were not built");
   5543 
   5544   getCurFunction()->setHasBranchProtectedScope();
   5545   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
   5546                                         NestedLoopCount, Clauses, AStmt, B);
   5547 }
   5548 
   5549 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
   5550                                              SourceLocation StartLoc,
   5551                                              SourceLocation LParenLoc,
   5552                                              SourceLocation EndLoc) {
   5553   OMPClause *Res = nullptr;
   5554   switch (Kind) {
   5555   case OMPC_final:
   5556     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
   5557     break;
   5558   case OMPC_num_threads:
   5559     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
   5560     break;
   5561   case OMPC_safelen:
   5562     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
   5563     break;
   5564   case OMPC_simdlen:
   5565     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
   5566     break;
   5567   case OMPC_collapse:
   5568     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
   5569     break;
   5570   case OMPC_ordered:
   5571     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
   5572     break;
   5573   case OMPC_device:
   5574     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
   5575     break;
   5576   case OMPC_num_teams:
   5577     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
   5578     break;
   5579   case OMPC_thread_limit:
   5580     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
   5581     break;
   5582   case OMPC_priority:
   5583     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
   5584     break;
   5585   case OMPC_grainsize:
   5586     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
   5587     break;
   5588   case OMPC_num_tasks:
   5589     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
   5590     break;
   5591   case OMPC_hint:
   5592     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
   5593     break;
   5594   case OMPC_if:
   5595   case OMPC_default:
   5596   case OMPC_proc_bind:
   5597   case OMPC_schedule:
   5598   case OMPC_private:
   5599   case OMPC_firstprivate:
   5600   case OMPC_lastprivate:
   5601   case OMPC_shared:
   5602   case OMPC_reduction:
   5603   case OMPC_linear:
   5604   case OMPC_aligned:
   5605   case OMPC_copyin:
   5606   case OMPC_copyprivate:
   5607   case OMPC_nowait:
   5608   case OMPC_untied:
   5609   case OMPC_mergeable:
   5610   case OMPC_threadprivate:
   5611   case OMPC_flush:
   5612   case OMPC_read:
   5613   case OMPC_write:
   5614   case OMPC_update:
   5615   case OMPC_capture:
   5616   case OMPC_seq_cst:
   5617   case OMPC_depend:
   5618   case OMPC_threads:
   5619   case OMPC_simd:
   5620   case OMPC_map:
   5621   case OMPC_nogroup:
   5622   case OMPC_unknown:
   5623     llvm_unreachable("Clause is not allowed.");
   5624   }
   5625   return Res;
   5626 }
   5627 
   5628 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
   5629                                      Expr *Condition, SourceLocation StartLoc,
   5630                                      SourceLocation LParenLoc,
   5631                                      SourceLocation NameModifierLoc,
   5632                                      SourceLocation ColonLoc,
   5633                                      SourceLocation EndLoc) {
   5634   Expr *ValExpr = Condition;
   5635   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
   5636       !Condition->isInstantiationDependent() &&
   5637       !Condition->containsUnexpandedParameterPack()) {
   5638     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
   5639                                            Condition->getExprLoc(), Condition);
   5640     if (Val.isInvalid())
   5641       return nullptr;
   5642 
   5643     ValExpr = Val.get();
   5644   }
   5645 
   5646   return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
   5647                                    NameModifierLoc, ColonLoc, EndLoc);
   5648 }
   5649 
   5650 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
   5651                                         SourceLocation StartLoc,
   5652                                         SourceLocation LParenLoc,
   5653                                         SourceLocation EndLoc) {
   5654   Expr *ValExpr = Condition;
   5655   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
   5656       !Condition->isInstantiationDependent() &&
   5657       !Condition->containsUnexpandedParameterPack()) {
   5658     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
   5659                                            Condition->getExprLoc(), Condition);
   5660     if (Val.isInvalid())
   5661       return nullptr;
   5662 
   5663     ValExpr = Val.get();
   5664   }
   5665 
   5666   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
   5667 }
   5668 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
   5669                                                         Expr *Op) {
   5670   if (!Op)
   5671     return ExprError();
   5672 
   5673   class IntConvertDiagnoser : public ICEConvertDiagnoser {
   5674   public:
   5675     IntConvertDiagnoser()
   5676         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
   5677     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
   5678                                          QualType T) override {
   5679       return S.Diag(Loc, diag::err_omp_not_integral) << T;
   5680     }
   5681     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
   5682                                              QualType T) override {
   5683       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
   5684     }
   5685     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
   5686                                                QualType T,
   5687                                                QualType ConvTy) override {
   5688       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
   5689     }
   5690     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
   5691                                            QualType ConvTy) override {
   5692       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
   5693              << ConvTy->isEnumeralType() << ConvTy;
   5694     }
   5695     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
   5696                                             QualType T) override {
   5697       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
   5698     }
   5699     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
   5700                                         QualType ConvTy) override {
   5701       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
   5702              << ConvTy->isEnumeralType() << ConvTy;
   5703     }
   5704     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
   5705                                              QualType) override {
   5706       llvm_unreachable("conversion functions are permitted");
   5707     }
   5708   } ConvertDiagnoser;
   5709   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
   5710 }
   5711 
   5712 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
   5713                                       OpenMPClauseKind CKind,
   5714                                       bool StrictlyPositive) {
   5715   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
   5716       !ValExpr->isInstantiationDependent()) {
   5717     SourceLocation Loc = ValExpr->getExprLoc();
   5718     ExprResult Value =
   5719         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
   5720     if (Value.isInvalid())
   5721       return false;
   5722 
   5723     ValExpr = Value.get();
   5724     // The expression must evaluate to a non-negative integer value.
   5725     llvm::APSInt Result;
   5726     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
   5727         Result.isSigned() &&
   5728         !((!StrictlyPositive && Result.isNonNegative()) ||
   5729           (StrictlyPositive && Result.isStrictlyPositive()))) {
   5730       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
   5731           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
   5732           << ValExpr->getSourceRange();
   5733       return false;
   5734     }
   5735   }
   5736   return true;
   5737 }
   5738 
   5739 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
   5740                                              SourceLocation StartLoc,
   5741                                              SourceLocation LParenLoc,
   5742                                              SourceLocation EndLoc) {
   5743   Expr *ValExpr = NumThreads;
   5744 
   5745   // OpenMP [2.5, Restrictions]
   5746   //  The num_threads expression must evaluate to a positive integer value.
   5747   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
   5748                                  /*StrictlyPositive=*/true))
   5749     return nullptr;
   5750 
   5751   return new (Context)
   5752       OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
   5753 }
   5754 
   5755 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
   5756                                                        OpenMPClauseKind CKind) {
   5757   if (!E)
   5758     return ExprError();
   5759   if (E->isValueDependent() || E->isTypeDependent() ||
   5760       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
   5761     return E;
   5762   llvm::APSInt Result;
   5763   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
   5764   if (ICE.isInvalid())
   5765     return ExprError();
   5766   if (!Result.isStrictlyPositive()) {
   5767     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
   5768         << getOpenMPClauseName(CKind) << 1 << E->getSourceRange();
   5769     return ExprError();
   5770   }
   5771   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
   5772     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
   5773         << E->getSourceRange();
   5774     return ExprError();
   5775   }
   5776   if (CKind == OMPC_collapse)
   5777     DSAStack->setCollapseNumber(Result.getExtValue());
   5778   else if (CKind == OMPC_ordered)
   5779     DSAStack->setCollapseNumber(Result.getExtValue());
   5780   return ICE;
   5781 }
   5782 
   5783 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
   5784                                           SourceLocation LParenLoc,
   5785                                           SourceLocation EndLoc) {
   5786   // OpenMP [2.8.1, simd construct, Description]
   5787   // The parameter of the safelen clause must be a constant
   5788   // positive integer expression.
   5789   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
   5790   if (Safelen.isInvalid())
   5791     return nullptr;
   5792   return new (Context)
   5793       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
   5794 }
   5795 
   5796 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
   5797                                           SourceLocation LParenLoc,
   5798                                           SourceLocation EndLoc) {
   5799   // OpenMP [2.8.1, simd construct, Description]
   5800   // The parameter of the simdlen clause must be a constant
   5801   // positive integer expression.
   5802   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
   5803   if (Simdlen.isInvalid())
   5804     return nullptr;
   5805   return new (Context)
   5806       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
   5807 }
   5808 
   5809 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
   5810                                            SourceLocation StartLoc,
   5811                                            SourceLocation LParenLoc,
   5812                                            SourceLocation EndLoc) {
   5813   // OpenMP [2.7.1, loop construct, Description]
   5814   // OpenMP [2.8.1, simd construct, Description]
   5815   // OpenMP [2.9.6, distribute construct, Description]
   5816   // The parameter of the collapse clause must be a constant
   5817   // positive integer expression.
   5818   ExprResult NumForLoopsResult =
   5819       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
   5820   if (NumForLoopsResult.isInvalid())
   5821     return nullptr;
   5822   return new (Context)
   5823       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
   5824 }
   5825 
   5826 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
   5827                                           SourceLocation EndLoc,
   5828                                           SourceLocation LParenLoc,
   5829                                           Expr *NumForLoops) {
   5830   // OpenMP [2.7.1, loop construct, Description]
   5831   // OpenMP [2.8.1, simd construct, Description]
   5832   // OpenMP [2.9.6, distribute construct, Description]
   5833   // The parameter of the ordered clause must be a constant
   5834   // positive integer expression if any.
   5835   if (NumForLoops && LParenLoc.isValid()) {
   5836     ExprResult NumForLoopsResult =
   5837         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
   5838     if (NumForLoopsResult.isInvalid())
   5839       return nullptr;
   5840     NumForLoops = NumForLoopsResult.get();
   5841   } else
   5842     NumForLoops = nullptr;
   5843   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
   5844   return new (Context)
   5845       OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
   5846 }
   5847 
   5848 OMPClause *Sema::ActOnOpenMPSimpleClause(
   5849     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
   5850     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
   5851   OMPClause *Res = nullptr;
   5852   switch (Kind) {
   5853   case OMPC_default:
   5854     Res =
   5855         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
   5856                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
   5857     break;
   5858   case OMPC_proc_bind:
   5859     Res = ActOnOpenMPProcBindClause(
   5860         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
   5861         LParenLoc, EndLoc);
   5862     break;
   5863   case OMPC_if:
   5864   case OMPC_final:
   5865   case OMPC_num_threads:
   5866   case OMPC_safelen:
   5867   case OMPC_simdlen:
   5868   case OMPC_collapse:
   5869   case OMPC_schedule:
   5870   case OMPC_private:
   5871   case OMPC_firstprivate:
   5872   case OMPC_lastprivate:
   5873   case OMPC_shared:
   5874   case OMPC_reduction:
   5875   case OMPC_linear:
   5876   case OMPC_aligned:
   5877   case OMPC_copyin:
   5878   case OMPC_copyprivate:
   5879   case OMPC_ordered:
   5880   case OMPC_nowait:
   5881   case OMPC_untied:
   5882   case OMPC_mergeable:
   5883   case OMPC_threadprivate:
   5884   case OMPC_flush:
   5885   case OMPC_read:
   5886   case OMPC_write:
   5887   case OMPC_update:
   5888   case OMPC_capture:
   5889   case OMPC_seq_cst:
   5890   case OMPC_depend:
   5891   case OMPC_device:
   5892   case OMPC_threads:
   5893   case OMPC_simd:
   5894   case OMPC_map:
   5895   case OMPC_num_teams:
   5896   case OMPC_thread_limit:
   5897   case OMPC_priority:
   5898   case OMPC_grainsize:
   5899   case OMPC_nogroup:
   5900   case OMPC_num_tasks:
   5901   case OMPC_hint:
   5902   case OMPC_unknown:
   5903     llvm_unreachable("Clause is not allowed.");
   5904   }
   5905   return Res;
   5906 }
   5907 
   5908 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
   5909                                           SourceLocation KindKwLoc,
   5910                                           SourceLocation StartLoc,
   5911                                           SourceLocation LParenLoc,
   5912                                           SourceLocation EndLoc) {
   5913   if (Kind == OMPC_DEFAULT_unknown) {
   5914     std::string Values;
   5915     static_assert(OMPC_DEFAULT_unknown > 0,
   5916                   "OMPC_DEFAULT_unknown not greater than 0");
   5917     std::string Sep(", ");
   5918     for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
   5919       Values += "'";
   5920       Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
   5921       Values += "'";
   5922       switch (i) {
   5923       case OMPC_DEFAULT_unknown - 2:
   5924         Values += " or ";
   5925         break;
   5926       case OMPC_DEFAULT_unknown - 1:
   5927         break;
   5928       default:
   5929         Values += Sep;
   5930         break;
   5931       }
   5932     }
   5933     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
   5934         << Values << getOpenMPClauseName(OMPC_default);
   5935     return nullptr;
   5936   }
   5937   switch (Kind) {
   5938   case OMPC_DEFAULT_none:
   5939     DSAStack->setDefaultDSANone(KindKwLoc);
   5940     break;
   5941   case OMPC_DEFAULT_shared:
   5942     DSAStack->setDefaultDSAShared(KindKwLoc);
   5943     break;
   5944   case OMPC_DEFAULT_unknown:
   5945     llvm_unreachable("Clause kind is not allowed.");
   5946     break;
   5947   }
   5948   return new (Context)
   5949       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
   5950 }
   5951 
   5952 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
   5953                                            SourceLocation KindKwLoc,
   5954                                            SourceLocation StartLoc,
   5955                                            SourceLocation LParenLoc,
   5956                                            SourceLocation EndLoc) {
   5957   if (Kind == OMPC_PROC_BIND_unknown) {
   5958     std::string Values;
   5959     std::string Sep(", ");
   5960     for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
   5961       Values += "'";
   5962       Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
   5963       Values += "'";
   5964       switch (i) {
   5965       case OMPC_PROC_BIND_unknown - 2:
   5966         Values += " or ";
   5967         break;
   5968       case OMPC_PROC_BIND_unknown - 1:
   5969         break;
   5970       default:
   5971         Values += Sep;
   5972         break;
   5973       }
   5974     }
   5975     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
   5976         << Values << getOpenMPClauseName(OMPC_proc_bind);
   5977     return nullptr;
   5978   }
   5979   return new (Context)
   5980       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
   5981 }
   5982 
   5983 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
   5984     OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
   5985     SourceLocation StartLoc, SourceLocation LParenLoc,
   5986     SourceLocation ArgumentLoc, SourceLocation DelimLoc,
   5987     SourceLocation EndLoc) {
   5988   OMPClause *Res = nullptr;
   5989   switch (Kind) {
   5990   case OMPC_schedule:
   5991     Res = ActOnOpenMPScheduleClause(
   5992         static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
   5993         LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
   5994     break;
   5995   case OMPC_if:
   5996     Res =
   5997         ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
   5998                             StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
   5999     break;
   6000   case OMPC_final:
   6001   case OMPC_num_threads:
   6002   case OMPC_safelen:
   6003   case OMPC_simdlen:
   6004   case OMPC_collapse:
   6005   case OMPC_default:
   6006   case OMPC_proc_bind:
   6007   case OMPC_private:
   6008   case OMPC_firstprivate:
   6009   case OMPC_lastprivate:
   6010   case OMPC_shared:
   6011   case OMPC_reduction:
   6012   case OMPC_linear:
   6013   case OMPC_aligned:
   6014   case OMPC_copyin:
   6015   case OMPC_copyprivate:
   6016   case OMPC_ordered:
   6017   case OMPC_nowait:
   6018   case OMPC_untied:
   6019   case OMPC_mergeable:
   6020   case OMPC_threadprivate:
   6021   case OMPC_flush:
   6022   case OMPC_read:
   6023   case OMPC_write:
   6024   case OMPC_update:
   6025   case OMPC_capture:
   6026   case OMPC_seq_cst:
   6027   case OMPC_depend:
   6028   case OMPC_device:
   6029   case OMPC_threads:
   6030   case OMPC_simd:
   6031   case OMPC_map:
   6032   case OMPC_num_teams:
   6033   case OMPC_thread_limit:
   6034   case OMPC_priority:
   6035   case OMPC_grainsize:
   6036   case OMPC_nogroup:
   6037   case OMPC_num_tasks:
   6038   case OMPC_hint:
   6039   case OMPC_unknown:
   6040     llvm_unreachable("Clause is not allowed.");
   6041   }
   6042   return Res;
   6043 }
   6044 
   6045 OMPClause *Sema::ActOnOpenMPScheduleClause(
   6046     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
   6047     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
   6048     SourceLocation EndLoc) {
   6049   if (Kind == OMPC_SCHEDULE_unknown) {
   6050     std::string Values;
   6051     std::string Sep(", ");
   6052     for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
   6053       Values += "'";
   6054       Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
   6055       Values += "'";
   6056       switch (i) {
   6057       case OMPC_SCHEDULE_unknown - 2:
   6058         Values += " or ";
   6059         break;
   6060       case OMPC_SCHEDULE_unknown - 1:
   6061         break;
   6062       default:
   6063         Values += Sep;
   6064         break;
   6065       }
   6066     }
   6067     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
   6068         << Values << getOpenMPClauseName(OMPC_schedule);
   6069     return nullptr;
   6070   }
   6071   Expr *ValExpr = ChunkSize;
   6072   Expr *HelperValExpr = nullptr;
   6073   if (ChunkSize) {
   6074     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
   6075         !ChunkSize->isInstantiationDependent() &&
   6076         !ChunkSize->containsUnexpandedParameterPack()) {
   6077       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
   6078       ExprResult Val =
   6079           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
   6080       if (Val.isInvalid())
   6081         return nullptr;
   6082 
   6083       ValExpr = Val.get();
   6084 
   6085       // OpenMP [2.7.1, Restrictions]
   6086       //  chunk_size must be a loop invariant integer expression with a positive
   6087       //  value.
   6088       llvm::APSInt Result;
   6089       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
   6090         if (Result.isSigned() && !Result.isStrictlyPositive()) {
   6091           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
   6092               << "schedule" << 1 << ChunkSize->getSourceRange();
   6093           return nullptr;
   6094         }
   6095       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
   6096         auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
   6097                                     ChunkSize->getType(), ".chunk.");
   6098         auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
   6099                                            ChunkSize->getExprLoc(),
   6100                                            /*RefersToCapture=*/true);
   6101         HelperValExpr = ImpVarRef;
   6102       }
   6103     }
   6104   }
   6105 
   6106   return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
   6107                                          EndLoc, Kind, ValExpr, HelperValExpr);
   6108 }
   6109 
   6110 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
   6111                                    SourceLocation StartLoc,
   6112                                    SourceLocation EndLoc) {
   6113   OMPClause *Res = nullptr;
   6114   switch (Kind) {
   6115   case OMPC_ordered:
   6116     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
   6117     break;
   6118   case OMPC_nowait:
   6119     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
   6120     break;
   6121   case OMPC_untied:
   6122     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
   6123     break;
   6124   case OMPC_mergeable:
   6125     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
   6126     break;
   6127   case OMPC_read:
   6128     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
   6129     break;
   6130   case OMPC_write:
   6131     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
   6132     break;
   6133   case OMPC_update:
   6134     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
   6135     break;
   6136   case OMPC_capture:
   6137     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
   6138     break;
   6139   case OMPC_seq_cst:
   6140     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
   6141     break;
   6142   case OMPC_threads:
   6143     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
   6144     break;
   6145   case OMPC_simd:
   6146     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
   6147     break;
   6148   case OMPC_nogroup:
   6149     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
   6150     break;
   6151   case OMPC_if:
   6152   case OMPC_final:
   6153   case OMPC_num_threads:
   6154   case OMPC_safelen:
   6155   case OMPC_simdlen:
   6156   case OMPC_collapse:
   6157   case OMPC_schedule:
   6158   case OMPC_private:
   6159   case OMPC_firstprivate:
   6160   case OMPC_lastprivate:
   6161   case OMPC_shared:
   6162   case OMPC_reduction:
   6163   case OMPC_linear:
   6164   case OMPC_aligned:
   6165   case OMPC_copyin:
   6166   case OMPC_copyprivate:
   6167   case OMPC_default:
   6168   case OMPC_proc_bind:
   6169   case OMPC_threadprivate:
   6170   case OMPC_flush:
   6171   case OMPC_depend:
   6172   case OMPC_device:
   6173   case OMPC_map:
   6174   case OMPC_num_teams:
   6175   case OMPC_thread_limit:
   6176   case OMPC_priority:
   6177   case OMPC_grainsize:
   6178   case OMPC_num_tasks:
   6179   case OMPC_hint:
   6180   case OMPC_unknown:
   6181     llvm_unreachable("Clause is not allowed.");
   6182   }
   6183   return Res;
   6184 }
   6185 
   6186 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
   6187                                          SourceLocation EndLoc) {
   6188   DSAStack->setNowaitRegion();
   6189   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
   6190 }
   6191 
   6192 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
   6193                                          SourceLocation EndLoc) {
   6194   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
   6195 }
   6196 
   6197 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
   6198                                             SourceLocation EndLoc) {
   6199   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
   6200 }
   6201 
   6202 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
   6203                                        SourceLocation EndLoc) {
   6204   return new (Context) OMPReadClause(StartLoc, EndLoc);
   6205 }
   6206 
   6207 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
   6208                                         SourceLocation EndLoc) {
   6209   return new (Context) OMPWriteClause(StartLoc, EndLoc);
   6210 }
   6211 
   6212 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
   6213                                          SourceLocation EndLoc) {
   6214   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
   6215 }
   6216 
   6217 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
   6218                                           SourceLocation EndLoc) {
   6219   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
   6220 }
   6221 
   6222 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
   6223                                          SourceLocation EndLoc) {
   6224   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
   6225 }
   6226 
   6227 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
   6228                                           SourceLocation EndLoc) {
   6229   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
   6230 }
   6231 
   6232 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
   6233                                        SourceLocation EndLoc) {
   6234   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
   6235 }
   6236 
   6237 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
   6238                                           SourceLocation EndLoc) {
   6239   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
   6240 }
   6241 
   6242 OMPClause *Sema::ActOnOpenMPVarListClause(
   6243     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
   6244     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
   6245     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
   6246     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
   6247     OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
   6248     OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
   6249   OMPClause *Res = nullptr;
   6250   switch (Kind) {
   6251   case OMPC_private:
   6252     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
   6253     break;
   6254   case OMPC_firstprivate:
   6255     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
   6256     break;
   6257   case OMPC_lastprivate:
   6258     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
   6259     break;
   6260   case OMPC_shared:
   6261     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
   6262     break;
   6263   case OMPC_reduction:
   6264     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
   6265                                      EndLoc, ReductionIdScopeSpec, ReductionId);
   6266     break;
   6267   case OMPC_linear:
   6268     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
   6269                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
   6270     break;
   6271   case OMPC_aligned:
   6272     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
   6273                                    ColonLoc, EndLoc);
   6274     break;
   6275   case OMPC_copyin:
   6276     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
   6277     break;
   6278   case OMPC_copyprivate:
   6279     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
   6280     break;
   6281   case OMPC_flush:
   6282     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
   6283     break;
   6284   case OMPC_depend:
   6285     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
   6286                                   StartLoc, LParenLoc, EndLoc);
   6287     break;
   6288   case OMPC_map:
   6289     Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
   6290                                VarList, StartLoc, LParenLoc, EndLoc);
   6291     break;
   6292   case OMPC_if:
   6293   case OMPC_final:
   6294   case OMPC_num_threads:
   6295   case OMPC_safelen:
   6296   case OMPC_simdlen:
   6297   case OMPC_collapse:
   6298   case OMPC_default:
   6299   case OMPC_proc_bind:
   6300   case OMPC_schedule:
   6301   case OMPC_ordered:
   6302   case OMPC_nowait:
   6303   case OMPC_untied:
   6304   case OMPC_mergeable:
   6305   case OMPC_threadprivate:
   6306   case OMPC_read:
   6307   case OMPC_write:
   6308   case OMPC_update:
   6309   case OMPC_capture:
   6310   case OMPC_seq_cst:
   6311   case OMPC_device:
   6312   case OMPC_threads:
   6313   case OMPC_simd:
   6314   case OMPC_num_teams:
   6315   case OMPC_thread_limit:
   6316   case OMPC_priority:
   6317   case OMPC_grainsize:
   6318   case OMPC_nogroup:
   6319   case OMPC_num_tasks:
   6320   case OMPC_hint:
   6321   case OMPC_unknown:
   6322     llvm_unreachable("Clause is not allowed.");
   6323   }
   6324   return Res;
   6325 }
   6326 
   6327 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
   6328                                           SourceLocation StartLoc,
   6329                                           SourceLocation LParenLoc,
   6330                                           SourceLocation EndLoc) {
   6331   SmallVector<Expr *, 8> Vars;
   6332   SmallVector<Expr *, 8> PrivateCopies;
   6333   for (auto &RefExpr : VarList) {
   6334     assert(RefExpr && "NULL expr in OpenMP private clause.");
   6335     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   6336       // It will be analyzed later.
   6337       Vars.push_back(RefExpr);
   6338       PrivateCopies.push_back(nullptr);
   6339       continue;
   6340     }
   6341 
   6342     SourceLocation ELoc = RefExpr->getExprLoc();
   6343     // OpenMP [2.1, C/C++]
   6344     //  A list item is a variable name.
   6345     // OpenMP  [2.9.3.3, Restrictions, p.1]
   6346     //  A variable that is part of another variable (as an array or
   6347     //  structure element) cannot appear in a private clause.
   6348     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
   6349     if (!DE || !isa<VarDecl>(DE->getDecl())) {
   6350       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
   6351       continue;
   6352     }
   6353     Decl *D = DE->getDecl();
   6354     VarDecl *VD = cast<VarDecl>(D);
   6355 
   6356     QualType Type = VD->getType();
   6357     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
   6358       // It will be analyzed later.
   6359       Vars.push_back(DE);
   6360       PrivateCopies.push_back(nullptr);
   6361       continue;
   6362     }
   6363 
   6364     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
   6365     //  A variable that appears in a private clause must not have an incomplete
   6366     //  type or a reference type.
   6367     if (RequireCompleteType(ELoc, Type,
   6368                             diag::err_omp_private_incomplete_type)) {
   6369       continue;
   6370     }
   6371     Type = Type.getNonReferenceType();
   6372 
   6373     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
   6374     // in a Construct]
   6375     //  Variables with the predetermined data-sharing attributes may not be
   6376     //  listed in data-sharing attributes clauses, except for the cases
   6377     //  listed below. For these exceptions only, listing a predetermined
   6378     //  variable in a data-sharing attribute clause is allowed and overrides
   6379     //  the variable's predetermined data-sharing attributes.
   6380     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
   6381     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
   6382       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
   6383                                           << getOpenMPClauseName(OMPC_private);
   6384       ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6385       continue;
   6386     }
   6387 
   6388     // Variably modified types are not supported for tasks.
   6389     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
   6390         DSAStack->getCurrentDirective() == OMPD_task) {
   6391       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
   6392           << getOpenMPClauseName(OMPC_private) << Type
   6393           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
   6394       bool IsDecl =
   6395           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   6396       Diag(VD->getLocation(),
   6397            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   6398           << VD;
   6399       continue;
   6400     }
   6401 
   6402     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
   6403     //  A variable of class type (or array thereof) that appears in a private
   6404     //  clause requires an accessible, unambiguous default constructor for the
   6405     //  class type.
   6406     // Generate helper private variable and initialize it with the default
   6407     // value. The address of the original variable is replaced by the address of
   6408     // the new private variable in CodeGen. This new variable is not added to
   6409     // IdResolver, so the code in the OpenMP region uses original variable for
   6410     // proper diagnostics.
   6411     Type = Type.getUnqualifiedType();
   6412     auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
   6413                                   VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   6414     ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
   6415     if (VDPrivate->isInvalidDecl())
   6416       continue;
   6417     auto VDPrivateRefExpr = buildDeclRefExpr(
   6418         *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
   6419 
   6420     DSAStack->addDSA(VD, DE, OMPC_private);
   6421     Vars.push_back(DE);
   6422     PrivateCopies.push_back(VDPrivateRefExpr);
   6423   }
   6424 
   6425   if (Vars.empty())
   6426     return nullptr;
   6427 
   6428   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
   6429                                   PrivateCopies);
   6430 }
   6431 
   6432 namespace {
   6433 class DiagsUninitializedSeveretyRAII {
   6434 private:
   6435   DiagnosticsEngine &Diags;
   6436   SourceLocation SavedLoc;
   6437   bool IsIgnored;
   6438 
   6439 public:
   6440   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
   6441                                  bool IsIgnored)
   6442       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
   6443     if (!IsIgnored) {
   6444       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
   6445                         /*Map*/ diag::Severity::Ignored, Loc);
   6446     }
   6447   }
   6448   ~DiagsUninitializedSeveretyRAII() {
   6449     if (!IsIgnored)
   6450       Diags.popMappings(SavedLoc);
   6451   }
   6452 };
   6453 }
   6454 
   6455 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
   6456                                                SourceLocation StartLoc,
   6457                                                SourceLocation LParenLoc,
   6458                                                SourceLocation EndLoc) {
   6459   SmallVector<Expr *, 8> Vars;
   6460   SmallVector<Expr *, 8> PrivateCopies;
   6461   SmallVector<Expr *, 8> Inits;
   6462   bool IsImplicitClause =
   6463       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
   6464   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
   6465 
   6466   for (auto &RefExpr : VarList) {
   6467     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
   6468     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   6469       // It will be analyzed later.
   6470       Vars.push_back(RefExpr);
   6471       PrivateCopies.push_back(nullptr);
   6472       Inits.push_back(nullptr);
   6473       continue;
   6474     }
   6475 
   6476     SourceLocation ELoc =
   6477         IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
   6478     // OpenMP [2.1, C/C++]
   6479     //  A list item is a variable name.
   6480     // OpenMP  [2.9.3.3, Restrictions, p.1]
   6481     //  A variable that is part of another variable (as an array or
   6482     //  structure element) cannot appear in a private clause.
   6483     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
   6484     if (!DE || !isa<VarDecl>(DE->getDecl())) {
   6485       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
   6486       continue;
   6487     }
   6488     Decl *D = DE->getDecl();
   6489     VarDecl *VD = cast<VarDecl>(D);
   6490 
   6491     QualType Type = VD->getType();
   6492     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
   6493       // It will be analyzed later.
   6494       Vars.push_back(DE);
   6495       PrivateCopies.push_back(nullptr);
   6496       Inits.push_back(nullptr);
   6497       continue;
   6498     }
   6499 
   6500     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
   6501     //  A variable that appears in a private clause must not have an incomplete
   6502     //  type or a reference type.
   6503     if (RequireCompleteType(ELoc, Type,
   6504                             diag::err_omp_firstprivate_incomplete_type)) {
   6505       continue;
   6506     }
   6507     Type = Type.getNonReferenceType();
   6508 
   6509     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
   6510     //  A variable of class type (or array thereof) that appears in a private
   6511     //  clause requires an accessible, unambiguous copy constructor for the
   6512     //  class type.
   6513     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
   6514 
   6515     // If an implicit firstprivate variable found it was checked already.
   6516     if (!IsImplicitClause) {
   6517       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
   6518       bool IsConstant = ElemType.isConstant(Context);
   6519       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
   6520       //  A list item that specifies a given variable may not appear in more
   6521       // than one clause on the same directive, except that a variable may be
   6522       //  specified in both firstprivate and lastprivate clauses.
   6523       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
   6524           DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
   6525         Diag(ELoc, diag::err_omp_wrong_dsa)
   6526             << getOpenMPClauseName(DVar.CKind)
   6527             << getOpenMPClauseName(OMPC_firstprivate);
   6528         ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6529         continue;
   6530       }
   6531 
   6532       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
   6533       // in a Construct]
   6534       //  Variables with the predetermined data-sharing attributes may not be
   6535       //  listed in data-sharing attributes clauses, except for the cases
   6536       //  listed below. For these exceptions only, listing a predetermined
   6537       //  variable in a data-sharing attribute clause is allowed and overrides
   6538       //  the variable's predetermined data-sharing attributes.
   6539       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
   6540       // in a Construct, C/C++, p.2]
   6541       //  Variables with const-qualified type having no mutable member may be
   6542       //  listed in a firstprivate clause, even if they are static data members.
   6543       if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
   6544           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
   6545         Diag(ELoc, diag::err_omp_wrong_dsa)
   6546             << getOpenMPClauseName(DVar.CKind)
   6547             << getOpenMPClauseName(OMPC_firstprivate);
   6548         ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6549         continue;
   6550       }
   6551 
   6552       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
   6553       // OpenMP [2.9.3.4, Restrictions, p.2]
   6554       //  A list item that is private within a parallel region must not appear
   6555       //  in a firstprivate clause on a worksharing construct if any of the
   6556       //  worksharing regions arising from the worksharing construct ever bind
   6557       //  to any of the parallel regions arising from the parallel construct.
   6558       if (isOpenMPWorksharingDirective(CurrDir) &&
   6559           !isOpenMPParallelDirective(CurrDir)) {
   6560         DVar = DSAStack->getImplicitDSA(VD, true);
   6561         if (DVar.CKind != OMPC_shared &&
   6562             (isOpenMPParallelDirective(DVar.DKind) ||
   6563              DVar.DKind == OMPD_unknown)) {
   6564           Diag(ELoc, diag::err_omp_required_access)
   6565               << getOpenMPClauseName(OMPC_firstprivate)
   6566               << getOpenMPClauseName(OMPC_shared);
   6567           ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6568           continue;
   6569         }
   6570       }
   6571       // OpenMP [2.9.3.4, Restrictions, p.3]
   6572       //  A list item that appears in a reduction clause of a parallel construct
   6573       //  must not appear in a firstprivate clause on a worksharing or task
   6574       //  construct if any of the worksharing or task regions arising from the
   6575       //  worksharing or task construct ever bind to any of the parallel regions
   6576       //  arising from the parallel construct.
   6577       // OpenMP [2.9.3.4, Restrictions, p.4]
   6578       //  A list item that appears in a reduction clause in worksharing
   6579       //  construct must not appear in a firstprivate clause in a task construct
   6580       //  encountered during execution of any of the worksharing regions arising
   6581       //  from the worksharing construct.
   6582       if (CurrDir == OMPD_task) {
   6583         DVar =
   6584             DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
   6585                                       [](OpenMPDirectiveKind K) -> bool {
   6586                                         return isOpenMPParallelDirective(K) ||
   6587                                                isOpenMPWorksharingDirective(K);
   6588                                       },
   6589                                       false);
   6590         if (DVar.CKind == OMPC_reduction &&
   6591             (isOpenMPParallelDirective(DVar.DKind) ||
   6592              isOpenMPWorksharingDirective(DVar.DKind))) {
   6593           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
   6594               << getOpenMPDirectiveName(DVar.DKind);
   6595           ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6596           continue;
   6597         }
   6598       }
   6599 
   6600       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
   6601       // A list item that is private within a teams region must not appear in a
   6602       // firstprivate clause on a distribute construct if any of the distribute
   6603       // regions arising from the distribute construct ever bind to any of the
   6604       // teams regions arising from the teams construct.
   6605       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
   6606       // A list item that appears in a reduction clause of a teams construct
   6607       // must not appear in a firstprivate clause on a distribute construct if
   6608       // any of the distribute regions arising from the distribute construct
   6609       // ever bind to any of the teams regions arising from the teams construct.
   6610       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
   6611       // A list item may appear in a firstprivate or lastprivate clause but not
   6612       // both.
   6613       if (CurrDir == OMPD_distribute) {
   6614         DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
   6615                                          [](OpenMPDirectiveKind K) -> bool {
   6616                                            return isOpenMPTeamsDirective(K);
   6617                                          },
   6618                                          false);
   6619         if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
   6620           Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
   6621           ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6622           continue;
   6623         }
   6624         DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
   6625                                          [](OpenMPDirectiveKind K) -> bool {
   6626                                            return isOpenMPTeamsDirective(K);
   6627                                          },
   6628                                          false);
   6629         if (DVar.CKind == OMPC_reduction &&
   6630             isOpenMPTeamsDirective(DVar.DKind)) {
   6631           Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
   6632           ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6633           continue;
   6634         }
   6635         DVar = DSAStack->getTopDSA(VD, false);
   6636         if (DVar.CKind == OMPC_lastprivate) {
   6637           Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
   6638           ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6639           continue;
   6640         }
   6641       }
   6642     }
   6643 
   6644     // Variably modified types are not supported for tasks.
   6645     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
   6646         DSAStack->getCurrentDirective() == OMPD_task) {
   6647       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
   6648           << getOpenMPClauseName(OMPC_firstprivate) << Type
   6649           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
   6650       bool IsDecl =
   6651           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   6652       Diag(VD->getLocation(),
   6653            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   6654           << VD;
   6655       continue;
   6656     }
   6657 
   6658     Type = Type.getUnqualifiedType();
   6659     auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
   6660                                   VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   6661     // Generate helper private variable and initialize it with the value of the
   6662     // original variable. The address of the original variable is replaced by
   6663     // the address of the new private variable in the CodeGen. This new variable
   6664     // is not added to IdResolver, so the code in the OpenMP region uses
   6665     // original variable for proper diagnostics and variable capturing.
   6666     Expr *VDInitRefExpr = nullptr;
   6667     // For arrays generate initializer for single element and replace it by the
   6668     // original array element in CodeGen.
   6669     if (Type->isArrayType()) {
   6670       auto VDInit =
   6671           buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
   6672       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
   6673       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
   6674       ElemType = ElemType.getUnqualifiedType();
   6675       auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
   6676                                       ".firstprivate.temp");
   6677       InitializedEntity Entity =
   6678           InitializedEntity::InitializeVariable(VDInitTemp);
   6679       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
   6680 
   6681       InitializationSequence InitSeq(*this, Entity, Kind, Init);
   6682       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
   6683       if (Result.isInvalid())
   6684         VDPrivate->setInvalidDecl();
   6685       else
   6686         VDPrivate->setInit(Result.getAs<Expr>());
   6687       // Remove temp variable declaration.
   6688       Context.Deallocate(VDInitTemp);
   6689     } else {
   6690       auto *VDInit =
   6691           buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
   6692       VDInitRefExpr =
   6693           buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
   6694       AddInitializerToDecl(VDPrivate,
   6695                            DefaultLvalueConversion(VDInitRefExpr).get(),
   6696                            /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
   6697     }
   6698     if (VDPrivate->isInvalidDecl()) {
   6699       if (IsImplicitClause) {
   6700         Diag(DE->getExprLoc(),
   6701              diag::note_omp_task_predetermined_firstprivate_here);
   6702       }
   6703       continue;
   6704     }
   6705     CurContext->addDecl(VDPrivate);
   6706     auto VDPrivateRefExpr = buildDeclRefExpr(
   6707         *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
   6708     DSAStack->addDSA(VD, DE, OMPC_firstprivate);
   6709     Vars.push_back(DE);
   6710     PrivateCopies.push_back(VDPrivateRefExpr);
   6711     Inits.push_back(VDInitRefExpr);
   6712   }
   6713 
   6714   if (Vars.empty())
   6715     return nullptr;
   6716 
   6717   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
   6718                                        Vars, PrivateCopies, Inits);
   6719 }
   6720 
   6721 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
   6722                                               SourceLocation StartLoc,
   6723                                               SourceLocation LParenLoc,
   6724                                               SourceLocation EndLoc) {
   6725   SmallVector<Expr *, 8> Vars;
   6726   SmallVector<Expr *, 8> SrcExprs;
   6727   SmallVector<Expr *, 8> DstExprs;
   6728   SmallVector<Expr *, 8> AssignmentOps;
   6729   for (auto &RefExpr : VarList) {
   6730     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
   6731     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   6732       // It will be analyzed later.
   6733       Vars.push_back(RefExpr);
   6734       SrcExprs.push_back(nullptr);
   6735       DstExprs.push_back(nullptr);
   6736       AssignmentOps.push_back(nullptr);
   6737       continue;
   6738     }
   6739 
   6740     SourceLocation ELoc = RefExpr->getExprLoc();
   6741     // OpenMP [2.1, C/C++]
   6742     //  A list item is a variable name.
   6743     // OpenMP  [2.14.3.5, Restrictions, p.1]
   6744     //  A variable that is part of another variable (as an array or structure
   6745     //  element) cannot appear in a lastprivate clause.
   6746     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
   6747     if (!DE || !isa<VarDecl>(DE->getDecl())) {
   6748       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
   6749       continue;
   6750     }
   6751     Decl *D = DE->getDecl();
   6752     VarDecl *VD = cast<VarDecl>(D);
   6753 
   6754     QualType Type = VD->getType();
   6755     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
   6756       // It will be analyzed later.
   6757       Vars.push_back(DE);
   6758       SrcExprs.push_back(nullptr);
   6759       DstExprs.push_back(nullptr);
   6760       AssignmentOps.push_back(nullptr);
   6761       continue;
   6762     }
   6763 
   6764     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
   6765     //  A variable that appears in a lastprivate clause must not have an
   6766     //  incomplete type or a reference type.
   6767     if (RequireCompleteType(ELoc, Type,
   6768                             diag::err_omp_lastprivate_incomplete_type)) {
   6769       continue;
   6770     }
   6771     Type = Type.getNonReferenceType();
   6772 
   6773     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
   6774     // in a Construct]
   6775     //  Variables with the predetermined data-sharing attributes may not be
   6776     //  listed in data-sharing attributes clauses, except for the cases
   6777     //  listed below.
   6778     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
   6779     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
   6780         DVar.CKind != OMPC_firstprivate &&
   6781         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
   6782       Diag(ELoc, diag::err_omp_wrong_dsa)
   6783           << getOpenMPClauseName(DVar.CKind)
   6784           << getOpenMPClauseName(OMPC_lastprivate);
   6785       ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6786       continue;
   6787     }
   6788 
   6789     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
   6790     // OpenMP [2.14.3.5, Restrictions, p.2]
   6791     // A list item that is private within a parallel region, or that appears in
   6792     // the reduction clause of a parallel construct, must not appear in a
   6793     // lastprivate clause on a worksharing construct if any of the corresponding
   6794     // worksharing regions ever binds to any of the corresponding parallel
   6795     // regions.
   6796     DSAStackTy::DSAVarData TopDVar = DVar;
   6797     if (isOpenMPWorksharingDirective(CurrDir) &&
   6798         !isOpenMPParallelDirective(CurrDir)) {
   6799       DVar = DSAStack->getImplicitDSA(VD, true);
   6800       if (DVar.CKind != OMPC_shared) {
   6801         Diag(ELoc, diag::err_omp_required_access)
   6802             << getOpenMPClauseName(OMPC_lastprivate)
   6803             << getOpenMPClauseName(OMPC_shared);
   6804         ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6805         continue;
   6806       }
   6807     }
   6808     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
   6809     //  A variable of class type (or array thereof) that appears in a
   6810     //  lastprivate clause requires an accessible, unambiguous default
   6811     //  constructor for the class type, unless the list item is also specified
   6812     //  in a firstprivate clause.
   6813     //  A variable of class type (or array thereof) that appears in a
   6814     //  lastprivate clause requires an accessible, unambiguous copy assignment
   6815     //  operator for the class type.
   6816     Type = Context.getBaseElementType(Type).getNonReferenceType();
   6817     auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
   6818                                Type.getUnqualifiedType(), ".lastprivate.src",
   6819                                VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   6820     auto *PseudoSrcExpr = buildDeclRefExpr(
   6821         *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
   6822     auto *DstVD =
   6823         buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
   6824                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   6825     auto *PseudoDstExpr =
   6826         buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
   6827     // For arrays generate assignment operation for single element and replace
   6828     // it by the original array element in CodeGen.
   6829     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
   6830                                    PseudoDstExpr, PseudoSrcExpr);
   6831     if (AssignmentOp.isInvalid())
   6832       continue;
   6833     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
   6834                                        /*DiscardedValue=*/true);
   6835     if (AssignmentOp.isInvalid())
   6836       continue;
   6837 
   6838     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
   6839     // A list item may appear in a firstprivate or lastprivate clause but not
   6840     // both.
   6841     if (CurrDir == OMPD_distribute) {
   6842       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
   6843       if (DVar.CKind == OMPC_firstprivate) {
   6844         Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
   6845         ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6846         continue;
   6847       }
   6848     }
   6849 
   6850     if (TopDVar.CKind != OMPC_firstprivate)
   6851       DSAStack->addDSA(VD, DE, OMPC_lastprivate);
   6852     Vars.push_back(DE);
   6853     SrcExprs.push_back(PseudoSrcExpr);
   6854     DstExprs.push_back(PseudoDstExpr);
   6855     AssignmentOps.push_back(AssignmentOp.get());
   6856   }
   6857 
   6858   if (Vars.empty())
   6859     return nullptr;
   6860 
   6861   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
   6862                                       Vars, SrcExprs, DstExprs, AssignmentOps);
   6863 }
   6864 
   6865 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
   6866                                          SourceLocation StartLoc,
   6867                                          SourceLocation LParenLoc,
   6868                                          SourceLocation EndLoc) {
   6869   SmallVector<Expr *, 8> Vars;
   6870   for (auto &RefExpr : VarList) {
   6871     assert(RefExpr && "NULL expr in OpenMP shared clause.");
   6872     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   6873       // It will be analyzed later.
   6874       Vars.push_back(RefExpr);
   6875       continue;
   6876     }
   6877 
   6878     SourceLocation ELoc = RefExpr->getExprLoc();
   6879     // OpenMP [2.1, C/C++]
   6880     //  A list item is a variable name.
   6881     // OpenMP  [2.14.3.2, Restrictions, p.1]
   6882     //  A variable that is part of another variable (as an array or structure
   6883     //  element) cannot appear in a shared unless it is a static data member
   6884     //  of a C++ class.
   6885     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
   6886     if (!DE || !isa<VarDecl>(DE->getDecl())) {
   6887       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
   6888       continue;
   6889     }
   6890     Decl *D = DE->getDecl();
   6891     VarDecl *VD = cast<VarDecl>(D);
   6892 
   6893     QualType Type = VD->getType();
   6894     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
   6895       // It will be analyzed later.
   6896       Vars.push_back(DE);
   6897       continue;
   6898     }
   6899 
   6900     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
   6901     // in a Construct]
   6902     //  Variables with the predetermined data-sharing attributes may not be
   6903     //  listed in data-sharing attributes clauses, except for the cases
   6904     //  listed below. For these exceptions only, listing a predetermined
   6905     //  variable in a data-sharing attribute clause is allowed and overrides
   6906     //  the variable's predetermined data-sharing attributes.
   6907     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
   6908     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
   6909         DVar.RefExpr) {
   6910       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
   6911                                           << getOpenMPClauseName(OMPC_shared);
   6912       ReportOriginalDSA(*this, DSAStack, VD, DVar);
   6913       continue;
   6914     }
   6915 
   6916     DSAStack->addDSA(VD, DE, OMPC_shared);
   6917     Vars.push_back(DE);
   6918   }
   6919 
   6920   if (Vars.empty())
   6921     return nullptr;
   6922 
   6923   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
   6924 }
   6925 
   6926 namespace {
   6927 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
   6928   DSAStackTy *Stack;
   6929 
   6930 public:
   6931   bool VisitDeclRefExpr(DeclRefExpr *E) {
   6932     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
   6933       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
   6934       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
   6935         return false;
   6936       if (DVar.CKind != OMPC_unknown)
   6937         return true;
   6938       DSAStackTy::DSAVarData DVarPrivate =
   6939           Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
   6940       if (DVarPrivate.CKind != OMPC_unknown)
   6941         return true;
   6942       return false;
   6943     }
   6944     return false;
   6945   }
   6946   bool VisitStmt(Stmt *S) {
   6947     for (auto Child : S->children()) {
   6948       if (Child && Visit(Child))
   6949         return true;
   6950     }
   6951     return false;
   6952   }
   6953   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
   6954 };
   6955 } // namespace
   6956 
   6957 OMPClause *Sema::ActOnOpenMPReductionClause(
   6958     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
   6959     SourceLocation ColonLoc, SourceLocation EndLoc,
   6960     CXXScopeSpec &ReductionIdScopeSpec,
   6961     const DeclarationNameInfo &ReductionId) {
   6962   // TODO: Allow scope specification search when 'declare reduction' is
   6963   // supported.
   6964   assert(ReductionIdScopeSpec.isEmpty() &&
   6965          "No support for scoped reduction identifiers yet.");
   6966 
   6967   auto DN = ReductionId.getName();
   6968   auto OOK = DN.getCXXOverloadedOperator();
   6969   BinaryOperatorKind BOK = BO_Comma;
   6970 
   6971   // OpenMP [2.14.3.6, reduction clause]
   6972   // C
   6973   // reduction-identifier is either an identifier or one of the following
   6974   // operators: +, -, *,  &, |, ^, && and ||
   6975   // C++
   6976   // reduction-identifier is either an id-expression or one of the following
   6977   // operators: +, -, *, &, |, ^, && and ||
   6978   // FIXME: Only 'min' and 'max' identifiers are supported for now.
   6979   switch (OOK) {
   6980   case OO_Plus:
   6981   case OO_Minus:
   6982     BOK = BO_Add;
   6983     break;
   6984   case OO_Star:
   6985     BOK = BO_Mul;
   6986     break;
   6987   case OO_Amp:
   6988     BOK = BO_And;
   6989     break;
   6990   case OO_Pipe:
   6991     BOK = BO_Or;
   6992     break;
   6993   case OO_Caret:
   6994     BOK = BO_Xor;
   6995     break;
   6996   case OO_AmpAmp:
   6997     BOK = BO_LAnd;
   6998     break;
   6999   case OO_PipePipe:
   7000     BOK = BO_LOr;
   7001     break;
   7002   case OO_New:
   7003   case OO_Delete:
   7004   case OO_Array_New:
   7005   case OO_Array_Delete:
   7006   case OO_Slash:
   7007   case OO_Percent:
   7008   case OO_Tilde:
   7009   case OO_Exclaim:
   7010   case OO_Equal:
   7011   case OO_Less:
   7012   case OO_Greater:
   7013   case OO_LessEqual:
   7014   case OO_GreaterEqual:
   7015   case OO_PlusEqual:
   7016   case OO_MinusEqual:
   7017   case OO_StarEqual:
   7018   case OO_SlashEqual:
   7019   case OO_PercentEqual:
   7020   case OO_CaretEqual:
   7021   case OO_AmpEqual:
   7022   case OO_PipeEqual:
   7023   case OO_LessLess:
   7024   case OO_GreaterGreater:
   7025   case OO_LessLessEqual:
   7026   case OO_GreaterGreaterEqual:
   7027   case OO_EqualEqual:
   7028   case OO_ExclaimEqual:
   7029   case OO_PlusPlus:
   7030   case OO_MinusMinus:
   7031   case OO_Comma:
   7032   case OO_ArrowStar:
   7033   case OO_Arrow:
   7034   case OO_Call:
   7035   case OO_Subscript:
   7036   case OO_Conditional:
   7037   case OO_Coawait:
   7038   case NUM_OVERLOADED_OPERATORS:
   7039     llvm_unreachable("Unexpected reduction identifier");
   7040   case OO_None:
   7041     if (auto II = DN.getAsIdentifierInfo()) {
   7042       if (II->isStr("max"))
   7043         BOK = BO_GT;
   7044       else if (II->isStr("min"))
   7045         BOK = BO_LT;
   7046     }
   7047     break;
   7048   }
   7049   SourceRange ReductionIdRange;
   7050   if (ReductionIdScopeSpec.isValid()) {
   7051     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
   7052   }
   7053   ReductionIdRange.setEnd(ReductionId.getEndLoc());
   7054   if (BOK == BO_Comma) {
   7055     // Not allowed reduction identifier is found.
   7056     Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
   7057         << ReductionIdRange;
   7058     return nullptr;
   7059   }
   7060 
   7061   SmallVector<Expr *, 8> Vars;
   7062   SmallVector<Expr *, 8> Privates;
   7063   SmallVector<Expr *, 8> LHSs;
   7064   SmallVector<Expr *, 8> RHSs;
   7065   SmallVector<Expr *, 8> ReductionOps;
   7066   for (auto RefExpr : VarList) {
   7067     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
   7068     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   7069       // It will be analyzed later.
   7070       Vars.push_back(RefExpr);
   7071       Privates.push_back(nullptr);
   7072       LHSs.push_back(nullptr);
   7073       RHSs.push_back(nullptr);
   7074       ReductionOps.push_back(nullptr);
   7075       continue;
   7076     }
   7077 
   7078     if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
   7079         RefExpr->isInstantiationDependent() ||
   7080         RefExpr->containsUnexpandedParameterPack()) {
   7081       // It will be analyzed later.
   7082       Vars.push_back(RefExpr);
   7083       Privates.push_back(nullptr);
   7084       LHSs.push_back(nullptr);
   7085       RHSs.push_back(nullptr);
   7086       ReductionOps.push_back(nullptr);
   7087       continue;
   7088     }
   7089 
   7090     auto ELoc = RefExpr->getExprLoc();
   7091     auto ERange = RefExpr->getSourceRange();
   7092     // OpenMP [2.1, C/C++]
   7093     //  A list item is a variable or array section, subject to the restrictions
   7094     //  specified in Section 2.4 on page 42 and in each of the sections
   7095     // describing clauses and directives for which a list appears.
   7096     // OpenMP  [2.14.3.3, Restrictions, p.1]
   7097     //  A variable that is part of another variable (as an array or
   7098     //  structure element) cannot appear in a private clause.
   7099     auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
   7100     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
   7101     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
   7102     if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
   7103       Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
   7104       continue;
   7105     }
   7106     QualType Type;
   7107     VarDecl *VD = nullptr;
   7108     if (DE) {
   7109       auto D = DE->getDecl();
   7110       VD = cast<VarDecl>(D);
   7111       Type = VD->getType();
   7112     } else if (ASE) {
   7113       Type = ASE->getType();
   7114       auto *Base = ASE->getBase()->IgnoreParenImpCasts();
   7115       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
   7116         Base = TempASE->getBase()->IgnoreParenImpCasts();
   7117       DE = dyn_cast<DeclRefExpr>(Base);
   7118       if (DE)
   7119         VD = dyn_cast<VarDecl>(DE->getDecl());
   7120       if (!VD) {
   7121         Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
   7122             << 0 << Base->getSourceRange();
   7123         continue;
   7124       }
   7125     } else if (OASE) {
   7126       auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
   7127       if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
   7128         Type = ATy->getElementType();
   7129       else
   7130         Type = BaseType->getPointeeType();
   7131       auto *Base = OASE->getBase()->IgnoreParenImpCasts();
   7132       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
   7133         Base = TempOASE->getBase()->IgnoreParenImpCasts();
   7134       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
   7135         Base = TempASE->getBase()->IgnoreParenImpCasts();
   7136       DE = dyn_cast<DeclRefExpr>(Base);
   7137       if (DE)
   7138         VD = dyn_cast<VarDecl>(DE->getDecl());
   7139       if (!VD) {
   7140         Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
   7141             << 1 << Base->getSourceRange();
   7142         continue;
   7143       }
   7144     }
   7145 
   7146     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
   7147     //  A variable that appears in a private clause must not have an incomplete
   7148     //  type or a reference type.
   7149     if (RequireCompleteType(ELoc, Type,
   7150                             diag::err_omp_reduction_incomplete_type))
   7151       continue;
   7152     // OpenMP [2.14.3.6, reduction clause, Restrictions]
   7153     // Arrays may not appear in a reduction clause.
   7154     if (Type.getNonReferenceType()->isArrayType()) {
   7155       Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
   7156       if (!ASE && !OASE) {
   7157         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
   7158                       VarDecl::DeclarationOnly;
   7159         Diag(VD->getLocation(),
   7160              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7161             << VD;
   7162       }
   7163       continue;
   7164     }
   7165     // OpenMP [2.14.3.6, reduction clause, Restrictions]
   7166     // A list item that appears in a reduction clause must not be
   7167     // const-qualified.
   7168     if (Type.getNonReferenceType().isConstant(Context)) {
   7169       Diag(ELoc, diag::err_omp_const_reduction_list_item)
   7170           << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
   7171       if (!ASE && !OASE) {
   7172         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
   7173                       VarDecl::DeclarationOnly;
   7174         Diag(VD->getLocation(),
   7175              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7176             << VD;
   7177       }
   7178       continue;
   7179     }
   7180     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
   7181     //  If a list-item is a reference type then it must bind to the same object
   7182     //  for all threads of the team.
   7183     if (!ASE && !OASE) {
   7184       VarDecl *VDDef = VD->getDefinition();
   7185       if (Type->isReferenceType() && VDDef) {
   7186         DSARefChecker Check(DSAStack);
   7187         if (Check.Visit(VDDef->getInit())) {
   7188           Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
   7189           Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
   7190           continue;
   7191         }
   7192       }
   7193     }
   7194     // OpenMP [2.14.3.6, reduction clause, Restrictions]
   7195     // The type of a list item that appears in a reduction clause must be valid
   7196     // for the reduction-identifier. For a max or min reduction in C, the type
   7197     // of the list item must be an allowed arithmetic data type: char, int,
   7198     // float, double, or _Bool, possibly modified with long, short, signed, or
   7199     // unsigned. For a max or min reduction in C++, the type of the list item
   7200     // must be an allowed arithmetic data type: char, wchar_t, int, float,
   7201     // double, or bool, possibly modified with long, short, signed, or unsigned.
   7202     if ((BOK == BO_GT || BOK == BO_LT) &&
   7203         !(Type->isScalarType() ||
   7204           (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
   7205       Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
   7206           << getLangOpts().CPlusPlus;
   7207       if (!ASE && !OASE) {
   7208         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
   7209                       VarDecl::DeclarationOnly;
   7210         Diag(VD->getLocation(),
   7211              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7212             << VD;
   7213       }
   7214       continue;
   7215     }
   7216     if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
   7217         !getLangOpts().CPlusPlus && Type->isFloatingType()) {
   7218       Diag(ELoc, diag::err_omp_clause_floating_type_arg);
   7219       if (!ASE && !OASE) {
   7220         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
   7221                       VarDecl::DeclarationOnly;
   7222         Diag(VD->getLocation(),
   7223              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7224             << VD;
   7225       }
   7226       continue;
   7227     }
   7228     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
   7229     // in a Construct]
   7230     //  Variables with the predetermined data-sharing attributes may not be
   7231     //  listed in data-sharing attributes clauses, except for the cases
   7232     //  listed below. For these exceptions only, listing a predetermined
   7233     //  variable in a data-sharing attribute clause is allowed and overrides
   7234     //  the variable's predetermined data-sharing attributes.
   7235     // OpenMP [2.14.3.6, Restrictions, p.3]
   7236     //  Any number of reduction clauses can be specified on the directive,
   7237     //  but a list item can appear only once in the reduction clauses for that
   7238     //  directive.
   7239     DSAStackTy::DSAVarData DVar;
   7240     DVar = DSAStack->getTopDSA(VD, false);
   7241     if (DVar.CKind == OMPC_reduction) {
   7242       Diag(ELoc, diag::err_omp_once_referenced)
   7243           << getOpenMPClauseName(OMPC_reduction);
   7244       if (DVar.RefExpr) {
   7245         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
   7246       }
   7247     } else if (DVar.CKind != OMPC_unknown) {
   7248       Diag(ELoc, diag::err_omp_wrong_dsa)
   7249           << getOpenMPClauseName(DVar.CKind)
   7250           << getOpenMPClauseName(OMPC_reduction);
   7251       ReportOriginalDSA(*this, DSAStack, VD, DVar);
   7252       continue;
   7253     }
   7254 
   7255     // OpenMP [2.14.3.6, Restrictions, p.1]
   7256     //  A list item that appears in a reduction clause of a worksharing
   7257     //  construct must be shared in the parallel regions to which any of the
   7258     //  worksharing regions arising from the worksharing construct bind.
   7259     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
   7260     if (isOpenMPWorksharingDirective(CurrDir) &&
   7261         !isOpenMPParallelDirective(CurrDir)) {
   7262       DVar = DSAStack->getImplicitDSA(VD, true);
   7263       if (DVar.CKind != OMPC_shared) {
   7264         Diag(ELoc, diag::err_omp_required_access)
   7265             << getOpenMPClauseName(OMPC_reduction)
   7266             << getOpenMPClauseName(OMPC_shared);
   7267         ReportOriginalDSA(*this, DSAStack, VD, DVar);
   7268         continue;
   7269       }
   7270     }
   7271 
   7272     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
   7273     auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
   7274                                VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   7275     auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
   7276                                VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   7277     auto PrivateTy = Type;
   7278     if (OASE) {
   7279       // For array sections only:
   7280       // Create pseudo array type for private copy. The size for this array will
   7281       // be generated during codegen.
   7282       // For array subscripts or single variables Private Ty is the same as Type
   7283       // (type of the variable or single array element).
   7284       PrivateTy = Context.getVariableArrayType(
   7285           Type, new (Context) OpaqueValueExpr(SourceLocation(),
   7286                                               Context.getSizeType(), VK_RValue),
   7287           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
   7288     }
   7289     // Private copy.
   7290     auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
   7291                                    VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   7292     // Add initializer for private variable.
   7293     Expr *Init = nullptr;
   7294     switch (BOK) {
   7295     case BO_Add:
   7296     case BO_Xor:
   7297     case BO_Or:
   7298     case BO_LOr:
   7299       // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
   7300       if (Type->isScalarType() || Type->isAnyComplexType()) {
   7301         Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
   7302       }
   7303       break;
   7304     case BO_Mul:
   7305     case BO_LAnd:
   7306       if (Type->isScalarType() || Type->isAnyComplexType()) {
   7307         // '*' and '&&' reduction ops - initializer is '1'.
   7308         Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
   7309       }
   7310       break;
   7311     case BO_And: {
   7312       // '&' reduction op - initializer is '~0'.
   7313       QualType OrigType = Type;
   7314       if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
   7315         Type = ComplexTy->getElementType();
   7316       }
   7317       if (Type->isRealFloatingType()) {
   7318         llvm::APFloat InitValue =
   7319             llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
   7320                                            /*isIEEE=*/true);
   7321         Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
   7322                                        Type, ELoc);
   7323       } else if (Type->isScalarType()) {
   7324         auto Size = Context.getTypeSize(Type);
   7325         QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
   7326         llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
   7327         Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
   7328       }
   7329       if (Init && OrigType->isAnyComplexType()) {
   7330         // Init = 0xFFFF + 0xFFFFi;
   7331         auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
   7332         Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
   7333       }
   7334       Type = OrigType;
   7335       break;
   7336     }
   7337     case BO_LT:
   7338     case BO_GT: {
   7339       // 'min' reduction op - initializer is 'Largest representable number in
   7340       // the reduction list item type'.
   7341       // 'max' reduction op - initializer is 'Least representable number in
   7342       // the reduction list item type'.
   7343       if (Type->isIntegerType() || Type->isPointerType()) {
   7344         bool IsSigned = Type->hasSignedIntegerRepresentation();
   7345         auto Size = Context.getTypeSize(Type);
   7346         QualType IntTy =
   7347             Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
   7348         llvm::APInt InitValue =
   7349             (BOK != BO_LT)
   7350                 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
   7351                            : llvm::APInt::getMinValue(Size)
   7352                 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
   7353                            : llvm::APInt::getMaxValue(Size);
   7354         Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
   7355         if (Type->isPointerType()) {
   7356           // Cast to pointer type.
   7357           auto CastExpr = BuildCStyleCastExpr(
   7358               SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
   7359               SourceLocation(), Init);
   7360           if (CastExpr.isInvalid())
   7361             continue;
   7362           Init = CastExpr.get();
   7363         }
   7364       } else if (Type->isRealFloatingType()) {
   7365         llvm::APFloat InitValue = llvm::APFloat::getLargest(
   7366             Context.getFloatTypeSemantics(Type), BOK != BO_LT);
   7367         Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
   7368                                        Type, ELoc);
   7369       }
   7370       break;
   7371     }
   7372     case BO_PtrMemD:
   7373     case BO_PtrMemI:
   7374     case BO_MulAssign:
   7375     case BO_Div:
   7376     case BO_Rem:
   7377     case BO_Sub:
   7378     case BO_Shl:
   7379     case BO_Shr:
   7380     case BO_LE:
   7381     case BO_GE:
   7382     case BO_EQ:
   7383     case BO_NE:
   7384     case BO_AndAssign:
   7385     case BO_XorAssign:
   7386     case BO_OrAssign:
   7387     case BO_Assign:
   7388     case BO_AddAssign:
   7389     case BO_SubAssign:
   7390     case BO_DivAssign:
   7391     case BO_RemAssign:
   7392     case BO_ShlAssign:
   7393     case BO_ShrAssign:
   7394     case BO_Comma:
   7395       llvm_unreachable("Unexpected reduction operation");
   7396     }
   7397     if (Init) {
   7398       AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
   7399                            /*TypeMayContainAuto=*/false);
   7400     } else
   7401       ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
   7402     if (!RHSVD->hasInit()) {
   7403       Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
   7404                                                             << ReductionIdRange;
   7405       if (VD) {
   7406         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
   7407                       VarDecl::DeclarationOnly;
   7408         Diag(VD->getLocation(),
   7409              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7410             << VD;
   7411       }
   7412       continue;
   7413     }
   7414     // Store initializer for single element in private copy. Will be used during
   7415     // codegen.
   7416     PrivateVD->setInit(RHSVD->getInit());
   7417     PrivateVD->setInitStyle(RHSVD->getInitStyle());
   7418     auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
   7419     auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
   7420     auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
   7421     ExprResult ReductionOp =
   7422         BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
   7423                    LHSDRE, RHSDRE);
   7424     if (ReductionOp.isUsable()) {
   7425       if (BOK != BO_LT && BOK != BO_GT) {
   7426         ReductionOp =
   7427             BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
   7428                        BO_Assign, LHSDRE, ReductionOp.get());
   7429       } else {
   7430         auto *ConditionalOp = new (Context) ConditionalOperator(
   7431             ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
   7432             RHSDRE, Type, VK_LValue, OK_Ordinary);
   7433         ReductionOp =
   7434             BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
   7435                        BO_Assign, LHSDRE, ConditionalOp);
   7436       }
   7437       ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
   7438     }
   7439     if (ReductionOp.isInvalid())
   7440       continue;
   7441 
   7442     DSAStack->addDSA(VD, DE, OMPC_reduction);
   7443     Vars.push_back(RefExpr);
   7444     Privates.push_back(PrivateDRE);
   7445     LHSs.push_back(LHSDRE);
   7446     RHSs.push_back(RHSDRE);
   7447     ReductionOps.push_back(ReductionOp.get());
   7448   }
   7449 
   7450   if (Vars.empty())
   7451     return nullptr;
   7452 
   7453   return OMPReductionClause::Create(
   7454       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
   7455       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
   7456       LHSs, RHSs, ReductionOps);
   7457 }
   7458 
   7459 OMPClause *Sema::ActOnOpenMPLinearClause(
   7460     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
   7461     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
   7462     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
   7463   SmallVector<Expr *, 8> Vars;
   7464   SmallVector<Expr *, 8> Privates;
   7465   SmallVector<Expr *, 8> Inits;
   7466   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
   7467       LinKind == OMPC_LINEAR_unknown) {
   7468     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
   7469     LinKind = OMPC_LINEAR_val;
   7470   }
   7471   for (auto &RefExpr : VarList) {
   7472     assert(RefExpr && "NULL expr in OpenMP linear clause.");
   7473     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   7474       // It will be analyzed later.
   7475       Vars.push_back(RefExpr);
   7476       Privates.push_back(nullptr);
   7477       Inits.push_back(nullptr);
   7478       continue;
   7479     }
   7480 
   7481     // OpenMP [2.14.3.7, linear clause]
   7482     // A list item that appears in a linear clause is subject to the private
   7483     // clause semantics described in Section 2.14.3.3 on page 159 except as
   7484     // noted. In addition, the value of the new list item on each iteration
   7485     // of the associated loop(s) corresponds to the value of the original
   7486     // list item before entering the construct plus the logical number of
   7487     // the iteration times linear-step.
   7488 
   7489     SourceLocation ELoc = RefExpr->getExprLoc();
   7490     // OpenMP [2.1, C/C++]
   7491     //  A list item is a variable name.
   7492     // OpenMP  [2.14.3.3, Restrictions, p.1]
   7493     //  A variable that is part of another variable (as an array or
   7494     //  structure element) cannot appear in a private clause.
   7495     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
   7496     if (!DE || !isa<VarDecl>(DE->getDecl())) {
   7497       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
   7498       continue;
   7499     }
   7500 
   7501     VarDecl *VD = cast<VarDecl>(DE->getDecl());
   7502 
   7503     // OpenMP [2.14.3.7, linear clause]
   7504     //  A list-item cannot appear in more than one linear clause.
   7505     //  A list-item that appears in a linear clause cannot appear in any
   7506     //  other data-sharing attribute clause.
   7507     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
   7508     if (DVar.RefExpr) {
   7509       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
   7510                                           << getOpenMPClauseName(OMPC_linear);
   7511       ReportOriginalDSA(*this, DSAStack, VD, DVar);
   7512       continue;
   7513     }
   7514 
   7515     QualType QType = VD->getType();
   7516     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
   7517       // It will be analyzed later.
   7518       Vars.push_back(DE);
   7519       Privates.push_back(nullptr);
   7520       Inits.push_back(nullptr);
   7521       continue;
   7522     }
   7523 
   7524     // A variable must not have an incomplete type or a reference type.
   7525     if (RequireCompleteType(ELoc, QType,
   7526                             diag::err_omp_linear_incomplete_type)) {
   7527       continue;
   7528     }
   7529     if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
   7530         !QType->isReferenceType()) {
   7531       Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
   7532           << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
   7533       continue;
   7534     }
   7535     QType = QType.getNonReferenceType();
   7536 
   7537     // A list item must not be const-qualified.
   7538     if (QType.isConstant(Context)) {
   7539       Diag(ELoc, diag::err_omp_const_variable)
   7540           << getOpenMPClauseName(OMPC_linear);
   7541       bool IsDecl =
   7542           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   7543       Diag(VD->getLocation(),
   7544            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7545           << VD;
   7546       continue;
   7547     }
   7548 
   7549     // A list item must be of integral or pointer type.
   7550     QType = QType.getUnqualifiedType().getCanonicalType();
   7551     const Type *Ty = QType.getTypePtrOrNull();
   7552     if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
   7553                 !Ty->isPointerType())) {
   7554       Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
   7555       bool IsDecl =
   7556           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   7557       Diag(VD->getLocation(),
   7558            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7559           << VD;
   7560       continue;
   7561     }
   7562 
   7563     // Build private copy of original var.
   7564     auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
   7565                                  VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   7566     auto *PrivateRef = buildDeclRefExpr(
   7567         *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
   7568     // Build var to save initial value.
   7569     VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
   7570     Expr *InitExpr;
   7571     if (LinKind == OMPC_LINEAR_uval)
   7572       InitExpr = VD->getInit();
   7573     else
   7574       InitExpr = DE;
   7575     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
   7576                          /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
   7577     auto InitRef = buildDeclRefExpr(
   7578         *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
   7579     DSAStack->addDSA(VD, DE, OMPC_linear);
   7580     Vars.push_back(DE);
   7581     Privates.push_back(PrivateRef);
   7582     Inits.push_back(InitRef);
   7583   }
   7584 
   7585   if (Vars.empty())
   7586     return nullptr;
   7587 
   7588   Expr *StepExpr = Step;
   7589   Expr *CalcStepExpr = nullptr;
   7590   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
   7591       !Step->isInstantiationDependent() &&
   7592       !Step->containsUnexpandedParameterPack()) {
   7593     SourceLocation StepLoc = Step->getLocStart();
   7594     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
   7595     if (Val.isInvalid())
   7596       return nullptr;
   7597     StepExpr = Val.get();
   7598 
   7599     // Build var to save the step value.
   7600     VarDecl *SaveVar =
   7601         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
   7602     ExprResult SaveRef =
   7603         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
   7604     ExprResult CalcStep =
   7605         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
   7606     CalcStep = ActOnFinishFullExpr(CalcStep.get());
   7607 
   7608     // Warn about zero linear step (it would be probably better specified as
   7609     // making corresponding variables 'const').
   7610     llvm::APSInt Result;
   7611     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
   7612     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
   7613       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
   7614                                                      << (Vars.size() > 1);
   7615     if (!IsConstant && CalcStep.isUsable()) {
   7616       // Calculate the step beforehand instead of doing this on each iteration.
   7617       // (This is not used if the number of iterations may be kfold-ed).
   7618       CalcStepExpr = CalcStep.get();
   7619     }
   7620   }
   7621 
   7622   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
   7623                                  ColonLoc, EndLoc, Vars, Privates, Inits,
   7624                                  StepExpr, CalcStepExpr);
   7625 }
   7626 
   7627 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
   7628                                      Expr *NumIterations, Sema &SemaRef,
   7629                                      Scope *S) {
   7630   // Walk the vars and build update/final expressions for the CodeGen.
   7631   SmallVector<Expr *, 8> Updates;
   7632   SmallVector<Expr *, 8> Finals;
   7633   Expr *Step = Clause.getStep();
   7634   Expr *CalcStep = Clause.getCalcStep();
   7635   // OpenMP [2.14.3.7, linear clause]
   7636   // If linear-step is not specified it is assumed to be 1.
   7637   if (Step == nullptr)
   7638     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
   7639   else if (CalcStep)
   7640     Step = cast<BinaryOperator>(CalcStep)->getLHS();
   7641   bool HasErrors = false;
   7642   auto CurInit = Clause.inits().begin();
   7643   auto CurPrivate = Clause.privates().begin();
   7644   auto LinKind = Clause.getModifier();
   7645   for (auto &RefExpr : Clause.varlists()) {
   7646     Expr *InitExpr = *CurInit;
   7647 
   7648     // Build privatized reference to the current linear var.
   7649     auto DE = cast<DeclRefExpr>(RefExpr);
   7650     Expr *CapturedRef;
   7651     if (LinKind == OMPC_LINEAR_uval)
   7652       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
   7653     else
   7654       CapturedRef =
   7655           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
   7656                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
   7657                            /*RefersToCapture=*/true);
   7658 
   7659     // Build update: Var = InitExpr + IV * Step
   7660     ExprResult Update =
   7661         BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
   7662                            InitExpr, IV, Step, /* Subtract */ false);
   7663     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
   7664                                          /*DiscardedValue=*/true);
   7665 
   7666     // Build final: Var = InitExpr + NumIterations * Step
   7667     ExprResult Final =
   7668         BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
   7669                            InitExpr, NumIterations, Step, /* Subtract */ false);
   7670     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
   7671                                         /*DiscardedValue=*/true);
   7672     if (!Update.isUsable() || !Final.isUsable()) {
   7673       Updates.push_back(nullptr);
   7674       Finals.push_back(nullptr);
   7675       HasErrors = true;
   7676     } else {
   7677       Updates.push_back(Update.get());
   7678       Finals.push_back(Final.get());
   7679     }
   7680     ++CurInit, ++CurPrivate;
   7681   }
   7682   Clause.setUpdates(Updates);
   7683   Clause.setFinals(Finals);
   7684   return HasErrors;
   7685 }
   7686 
   7687 OMPClause *Sema::ActOnOpenMPAlignedClause(
   7688     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
   7689     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
   7690 
   7691   SmallVector<Expr *, 8> Vars;
   7692   for (auto &RefExpr : VarList) {
   7693     assert(RefExpr && "NULL expr in OpenMP aligned clause.");
   7694     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   7695       // It will be analyzed later.
   7696       Vars.push_back(RefExpr);
   7697       continue;
   7698     }
   7699 
   7700     SourceLocation ELoc = RefExpr->getExprLoc();
   7701     // OpenMP [2.1, C/C++]
   7702     //  A list item is a variable name.
   7703     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
   7704     if (!DE || !isa<VarDecl>(DE->getDecl())) {
   7705       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
   7706       continue;
   7707     }
   7708 
   7709     VarDecl *VD = cast<VarDecl>(DE->getDecl());
   7710 
   7711     // OpenMP  [2.8.1, simd construct, Restrictions]
   7712     // The type of list items appearing in the aligned clause must be
   7713     // array, pointer, reference to array, or reference to pointer.
   7714     QualType QType = VD->getType();
   7715     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
   7716     const Type *Ty = QType.getTypePtrOrNull();
   7717     if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
   7718                 !Ty->isPointerType())) {
   7719       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
   7720           << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
   7721       bool IsDecl =
   7722           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   7723       Diag(VD->getLocation(),
   7724            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7725           << VD;
   7726       continue;
   7727     }
   7728 
   7729     // OpenMP  [2.8.1, simd construct, Restrictions]
   7730     // A list-item cannot appear in more than one aligned clause.
   7731     if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
   7732       Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
   7733       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
   7734           << getOpenMPClauseName(OMPC_aligned);
   7735       continue;
   7736     }
   7737 
   7738     Vars.push_back(DE);
   7739   }
   7740 
   7741   // OpenMP [2.8.1, simd construct, Description]
   7742   // The parameter of the aligned clause, alignment, must be a constant
   7743   // positive integer expression.
   7744   // If no optional parameter is specified, implementation-defined default
   7745   // alignments for SIMD instructions on the target platforms are assumed.
   7746   if (Alignment != nullptr) {
   7747     ExprResult AlignResult =
   7748         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
   7749     if (AlignResult.isInvalid())
   7750       return nullptr;
   7751     Alignment = AlignResult.get();
   7752   }
   7753   if (Vars.empty())
   7754     return nullptr;
   7755 
   7756   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
   7757                                   EndLoc, Vars, Alignment);
   7758 }
   7759 
   7760 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
   7761                                          SourceLocation StartLoc,
   7762                                          SourceLocation LParenLoc,
   7763                                          SourceLocation EndLoc) {
   7764   SmallVector<Expr *, 8> Vars;
   7765   SmallVector<Expr *, 8> SrcExprs;
   7766   SmallVector<Expr *, 8> DstExprs;
   7767   SmallVector<Expr *, 8> AssignmentOps;
   7768   for (auto &RefExpr : VarList) {
   7769     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
   7770     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   7771       // It will be analyzed later.
   7772       Vars.push_back(RefExpr);
   7773       SrcExprs.push_back(nullptr);
   7774       DstExprs.push_back(nullptr);
   7775       AssignmentOps.push_back(nullptr);
   7776       continue;
   7777     }
   7778 
   7779     SourceLocation ELoc = RefExpr->getExprLoc();
   7780     // OpenMP [2.1, C/C++]
   7781     //  A list item is a variable name.
   7782     // OpenMP  [2.14.4.1, Restrictions, p.1]
   7783     //  A list item that appears in a copyin clause must be threadprivate.
   7784     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
   7785     if (!DE || !isa<VarDecl>(DE->getDecl())) {
   7786       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
   7787       continue;
   7788     }
   7789 
   7790     Decl *D = DE->getDecl();
   7791     VarDecl *VD = cast<VarDecl>(D);
   7792 
   7793     QualType Type = VD->getType();
   7794     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
   7795       // It will be analyzed later.
   7796       Vars.push_back(DE);
   7797       SrcExprs.push_back(nullptr);
   7798       DstExprs.push_back(nullptr);
   7799       AssignmentOps.push_back(nullptr);
   7800       continue;
   7801     }
   7802 
   7803     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
   7804     //  A list item that appears in a copyin clause must be threadprivate.
   7805     if (!DSAStack->isThreadPrivate(VD)) {
   7806       Diag(ELoc, diag::err_omp_required_access)
   7807           << getOpenMPClauseName(OMPC_copyin)
   7808           << getOpenMPDirectiveName(OMPD_threadprivate);
   7809       continue;
   7810     }
   7811 
   7812     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
   7813     //  A variable of class type (or array thereof) that appears in a
   7814     //  copyin clause requires an accessible, unambiguous copy assignment
   7815     //  operator for the class type.
   7816     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
   7817     auto *SrcVD =
   7818         buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
   7819                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   7820     auto *PseudoSrcExpr = buildDeclRefExpr(
   7821         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
   7822     auto *DstVD =
   7823         buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
   7824                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   7825     auto *PseudoDstExpr =
   7826         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
   7827     // For arrays generate assignment operation for single element and replace
   7828     // it by the original array element in CodeGen.
   7829     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
   7830                                    PseudoDstExpr, PseudoSrcExpr);
   7831     if (AssignmentOp.isInvalid())
   7832       continue;
   7833     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
   7834                                        /*DiscardedValue=*/true);
   7835     if (AssignmentOp.isInvalid())
   7836       continue;
   7837 
   7838     DSAStack->addDSA(VD, DE, OMPC_copyin);
   7839     Vars.push_back(DE);
   7840     SrcExprs.push_back(PseudoSrcExpr);
   7841     DstExprs.push_back(PseudoDstExpr);
   7842     AssignmentOps.push_back(AssignmentOp.get());
   7843   }
   7844 
   7845   if (Vars.empty())
   7846     return nullptr;
   7847 
   7848   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
   7849                                  SrcExprs, DstExprs, AssignmentOps);
   7850 }
   7851 
   7852 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
   7853                                               SourceLocation StartLoc,
   7854                                               SourceLocation LParenLoc,
   7855                                               SourceLocation EndLoc) {
   7856   SmallVector<Expr *, 8> Vars;
   7857   SmallVector<Expr *, 8> SrcExprs;
   7858   SmallVector<Expr *, 8> DstExprs;
   7859   SmallVector<Expr *, 8> AssignmentOps;
   7860   for (auto &RefExpr : VarList) {
   7861     assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
   7862     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   7863       // It will be analyzed later.
   7864       Vars.push_back(RefExpr);
   7865       SrcExprs.push_back(nullptr);
   7866       DstExprs.push_back(nullptr);
   7867       AssignmentOps.push_back(nullptr);
   7868       continue;
   7869     }
   7870 
   7871     SourceLocation ELoc = RefExpr->getExprLoc();
   7872     // OpenMP [2.1, C/C++]
   7873     //  A list item is a variable name.
   7874     // OpenMP  [2.14.4.1, Restrictions, p.1]
   7875     //  A list item that appears in a copyin clause must be threadprivate.
   7876     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
   7877     if (!DE || !isa<VarDecl>(DE->getDecl())) {
   7878       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
   7879       continue;
   7880     }
   7881 
   7882     Decl *D = DE->getDecl();
   7883     VarDecl *VD = cast<VarDecl>(D);
   7884 
   7885     QualType Type = VD->getType();
   7886     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
   7887       // It will be analyzed later.
   7888       Vars.push_back(DE);
   7889       SrcExprs.push_back(nullptr);
   7890       DstExprs.push_back(nullptr);
   7891       AssignmentOps.push_back(nullptr);
   7892       continue;
   7893     }
   7894 
   7895     // OpenMP [2.14.4.2, Restrictions, p.2]
   7896     //  A list item that appears in a copyprivate clause may not appear in a
   7897     //  private or firstprivate clause on the single construct.
   7898     if (!DSAStack->isThreadPrivate(VD)) {
   7899       auto DVar = DSAStack->getTopDSA(VD, false);
   7900       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
   7901           DVar.RefExpr) {
   7902         Diag(ELoc, diag::err_omp_wrong_dsa)
   7903             << getOpenMPClauseName(DVar.CKind)
   7904             << getOpenMPClauseName(OMPC_copyprivate);
   7905         ReportOriginalDSA(*this, DSAStack, VD, DVar);
   7906         continue;
   7907       }
   7908 
   7909       // OpenMP [2.11.4.2, Restrictions, p.1]
   7910       //  All list items that appear in a copyprivate clause must be either
   7911       //  threadprivate or private in the enclosing context.
   7912       if (DVar.CKind == OMPC_unknown) {
   7913         DVar = DSAStack->getImplicitDSA(VD, false);
   7914         if (DVar.CKind == OMPC_shared) {
   7915           Diag(ELoc, diag::err_omp_required_access)
   7916               << getOpenMPClauseName(OMPC_copyprivate)
   7917               << "threadprivate or private in the enclosing context";
   7918           ReportOriginalDSA(*this, DSAStack, VD, DVar);
   7919           continue;
   7920         }
   7921       }
   7922     }
   7923 
   7924     // Variably modified types are not supported.
   7925     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
   7926       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
   7927           << getOpenMPClauseName(OMPC_copyprivate) << Type
   7928           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
   7929       bool IsDecl =
   7930           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
   7931       Diag(VD->getLocation(),
   7932            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
   7933           << VD;
   7934       continue;
   7935     }
   7936 
   7937     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
   7938     //  A variable of class type (or array thereof) that appears in a
   7939     //  copyin clause requires an accessible, unambiguous copy assignment
   7940     //  operator for the class type.
   7941     Type = Context.getBaseElementType(Type.getNonReferenceType())
   7942                .getUnqualifiedType();
   7943     auto *SrcVD =
   7944         buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
   7945                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   7946     auto *PseudoSrcExpr =
   7947         buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
   7948     auto *DstVD =
   7949         buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
   7950                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
   7951     auto *PseudoDstExpr =
   7952         buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
   7953     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
   7954                                    PseudoDstExpr, PseudoSrcExpr);
   7955     if (AssignmentOp.isInvalid())
   7956       continue;
   7957     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
   7958                                        /*DiscardedValue=*/true);
   7959     if (AssignmentOp.isInvalid())
   7960       continue;
   7961 
   7962     // No need to mark vars as copyprivate, they are already threadprivate or
   7963     // implicitly private.
   7964     Vars.push_back(DE);
   7965     SrcExprs.push_back(PseudoSrcExpr);
   7966     DstExprs.push_back(PseudoDstExpr);
   7967     AssignmentOps.push_back(AssignmentOp.get());
   7968   }
   7969 
   7970   if (Vars.empty())
   7971     return nullptr;
   7972 
   7973   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
   7974                                       Vars, SrcExprs, DstExprs, AssignmentOps);
   7975 }
   7976 
   7977 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
   7978                                         SourceLocation StartLoc,
   7979                                         SourceLocation LParenLoc,
   7980                                         SourceLocation EndLoc) {
   7981   if (VarList.empty())
   7982     return nullptr;
   7983 
   7984   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
   7985 }
   7986 
   7987 OMPClause *
   7988 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
   7989                               SourceLocation DepLoc, SourceLocation ColonLoc,
   7990                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
   7991                               SourceLocation LParenLoc, SourceLocation EndLoc) {
   7992   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
   7993       DepKind != OMPC_DEPEND_source) {
   7994     std::string Values = "'";
   7995     Values += getOpenMPSimpleClauseTypeName(OMPC_depend, OMPC_DEPEND_source);
   7996     Values += "'";
   7997     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
   7998         << Values << getOpenMPClauseName(OMPC_depend);
   7999     return nullptr;
   8000   }
   8001   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
   8002       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source)) {
   8003     std::string Values;
   8004     std::string Sep(", ");
   8005     for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
   8006       if (i == OMPC_DEPEND_source)
   8007         continue;
   8008       Values += "'";
   8009       Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
   8010       Values += "'";
   8011       switch (i) {
   8012       case OMPC_DEPEND_unknown - 3:
   8013         Values += " or ";
   8014         break;
   8015       case OMPC_DEPEND_unknown - 2:
   8016         break;
   8017       default:
   8018         Values += Sep;
   8019         break;
   8020       }
   8021     }
   8022     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
   8023         << Values << getOpenMPClauseName(OMPC_depend);
   8024     return nullptr;
   8025   }
   8026   SmallVector<Expr *, 8> Vars;
   8027   for (auto &RefExpr : VarList) {
   8028     assert(RefExpr && "NULL expr in OpenMP shared clause.");
   8029     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
   8030       // It will be analyzed later.
   8031       Vars.push_back(RefExpr);
   8032       continue;
   8033     }
   8034 
   8035     SourceLocation ELoc = RefExpr->getExprLoc();
   8036     // OpenMP  [2.11.1.1, Restrictions, p.3]
   8037     //  A variable that is part of another variable (such as a field of a
   8038     //  structure) but is not an array element or an array section cannot appear
   8039     //  in a depend clause.
   8040     auto *SimpleExpr = RefExpr->IgnoreParenCasts();
   8041     auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
   8042     auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
   8043     auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
   8044     if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
   8045         (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
   8046         (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
   8047          !ASE->getBase()->getType()->isArrayType())) {
   8048       Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
   8049           << RefExpr->getSourceRange();
   8050       continue;
   8051     }
   8052 
   8053     Vars.push_back(RefExpr->IgnoreParenImpCasts());
   8054   }
   8055 
   8056   if (DepKind != OMPC_DEPEND_source && Vars.empty())
   8057     return nullptr;
   8058 
   8059   return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
   8060                                  DepLoc, ColonLoc, Vars);
   8061 }
   8062 
   8063 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
   8064                                          SourceLocation LParenLoc,
   8065                                          SourceLocation EndLoc) {
   8066   Expr *ValExpr = Device;
   8067 
   8068   // OpenMP [2.9.1, Restrictions]
   8069   // The device expression must evaluate to a non-negative integer value.
   8070   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
   8071                                  /*StrictlyPositive=*/false))
   8072     return nullptr;
   8073 
   8074   return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
   8075 }
   8076 
   8077 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
   8078                                    DSAStackTy *Stack, CXXRecordDecl *RD) {
   8079   if (!RD || RD->isInvalidDecl())
   8080     return true;
   8081 
   8082   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
   8083     if (auto *CTD = CTSD->getSpecializedTemplate())
   8084       RD = CTD->getTemplatedDecl();
   8085   auto QTy = SemaRef.Context.getRecordType(RD);
   8086   if (RD->isDynamicClass()) {
   8087     SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
   8088     SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
   8089     return false;
   8090   }
   8091   auto *DC = RD;
   8092   bool IsCorrect = true;
   8093   for (auto *I : DC->decls()) {
   8094     if (I) {
   8095       if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
   8096         if (MD->isStatic()) {
   8097           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
   8098           SemaRef.Diag(MD->getLocation(),
   8099                        diag::note_omp_static_member_in_target);
   8100           IsCorrect = false;
   8101         }
   8102       } else if (auto *VD = dyn_cast<VarDecl>(I)) {
   8103         if (VD->isStaticDataMember()) {
   8104           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
   8105           SemaRef.Diag(VD->getLocation(),
   8106                        diag::note_omp_static_member_in_target);
   8107           IsCorrect = false;
   8108         }
   8109       }
   8110     }
   8111   }
   8112 
   8113   for (auto &I : RD->bases()) {
   8114     if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
   8115                                 I.getType()->getAsCXXRecordDecl()))
   8116       IsCorrect = false;
   8117   }
   8118   return IsCorrect;
   8119 }
   8120 
   8121 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
   8122                               DSAStackTy *Stack, QualType QTy) {
   8123   NamedDecl *ND;
   8124   if (QTy->isIncompleteType(&ND)) {
   8125     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
   8126     return false;
   8127   } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
   8128     if (!RD->isInvalidDecl() &&
   8129         !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
   8130       return false;
   8131   }
   8132   return true;
   8133 }
   8134 
   8135 OMPClause *Sema::ActOnOpenMPMapClause(
   8136     OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
   8137     SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
   8138     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
   8139   SmallVector<Expr *, 4> Vars;
   8140 
   8141   for (auto &RE : VarList) {
   8142     assert(RE && "Null expr in omp map");
   8143     if (isa<DependentScopeDeclRefExpr>(RE)) {
   8144       // It will be analyzed later.
   8145       Vars.push_back(RE);
   8146       continue;
   8147     }
   8148     SourceLocation ELoc = RE->getExprLoc();
   8149 
   8150     // OpenMP [2.14.5, Restrictions]
   8151     //  A variable that is part of another variable (such as field of a
   8152     //  structure) but is not an array element or an array section cannot appear
   8153     //  in a map clause.
   8154     auto *VE = RE->IgnoreParenLValueCasts();
   8155 
   8156     if (VE->isValueDependent() || VE->isTypeDependent() ||
   8157         VE->isInstantiationDependent() ||
   8158         VE->containsUnexpandedParameterPack()) {
   8159       // It will be analyzed later.
   8160       Vars.push_back(RE);
   8161       continue;
   8162     }
   8163 
   8164     auto *SimpleExpr = RE->IgnoreParenCasts();
   8165     auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
   8166     auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
   8167     auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
   8168 
   8169     if (!RE->IgnoreParenImpCasts()->isLValue() ||
   8170         (!OASE && !ASE && !DE) ||
   8171         (DE && !isa<VarDecl>(DE->getDecl())) ||
   8172         (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
   8173          !ASE->getBase()->getType()->isArrayType())) {
   8174       Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
   8175         << RE->getSourceRange();
   8176       continue;
   8177     }
   8178 
   8179     Decl *D = nullptr;
   8180     if (DE) {
   8181       D = DE->getDecl();
   8182     } else if (ASE) {
   8183       auto *B = ASE->getBase()->IgnoreParenCasts();
   8184       D = dyn_cast<DeclRefExpr>(B)->getDecl();
   8185     } else if (OASE) {
   8186       auto *B = OASE->getBase();
   8187       D = dyn_cast<DeclRefExpr>(B)->getDecl();
   8188     }
   8189     assert(D && "Null decl on map clause.");
   8190     auto *VD = cast<VarDecl>(D);
   8191 
   8192     // OpenMP [2.14.5, Restrictions, p.8]
   8193     // threadprivate variables cannot appear in a map clause.
   8194     if (DSAStack->isThreadPrivate(VD)) {
   8195       auto DVar = DSAStack->getTopDSA(VD, false);
   8196       Diag(ELoc, diag::err_omp_threadprivate_in_map);
   8197       ReportOriginalDSA(*this, DSAStack, VD, DVar);
   8198       continue;
   8199     }
   8200 
   8201     // OpenMP [2.14.5, Restrictions, p.2]
   8202     //  At most one list item can be an array item derived from a given variable
   8203     //  in map clauses of the same construct.
   8204     // OpenMP [2.14.5, Restrictions, p.3]
   8205     //  List items of map clauses in the same construct must not share original
   8206     //  storage.
   8207     // OpenMP [2.14.5, Restrictions, C/C++, p.2]
   8208     //  A variable for which the type is pointer, reference to array, or
   8209     //  reference to pointer and an array section derived from that variable
   8210     //  must not appear as list items of map clauses of the same construct.
   8211     DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
   8212     if (MI.RefExpr) {
   8213       Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
   8214       Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
   8215           << MI.RefExpr->getSourceRange();
   8216       continue;
   8217     }
   8218 
   8219     // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
   8220     //  A variable for which the type is pointer, reference to array, or
   8221     //  reference to pointer must not appear as a list item if the enclosing
   8222     //  device data environment already contains an array section derived from
   8223     //  that variable.
   8224     //  An array section derived from a variable for which the type is pointer,
   8225     //  reference to array, or reference to pointer must not appear as a list
   8226     //  item if the enclosing device data environment already contains that
   8227     //  variable.
   8228     QualType Type = VD->getType();
   8229     MI = DSAStack->getMapInfoForVar(VD);
   8230     if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
   8231                        isa<DeclRefExpr>(VE)) &&
   8232         (Type->isPointerType() || Type->isReferenceType())) {
   8233       Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
   8234       Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
   8235           << MI.RefExpr->getSourceRange();
   8236       continue;
   8237     }
   8238 
   8239     // OpenMP [2.14.5, Restrictions, C/C++, p.7]
   8240     //  A list item must have a mappable type.
   8241     if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
   8242                            DSAStack, Type))
   8243       continue;
   8244 
   8245     Vars.push_back(RE);
   8246     MI.RefExpr = RE;
   8247     DSAStack->addMapInfoForVar(VD, MI);
   8248   }
   8249   if (Vars.empty())
   8250     return nullptr;
   8251 
   8252   return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
   8253                               MapTypeModifier, MapType, MapLoc);
   8254 }
   8255 
   8256 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
   8257                                            SourceLocation StartLoc,
   8258                                            SourceLocation LParenLoc,
   8259                                            SourceLocation EndLoc) {
   8260   Expr *ValExpr = NumTeams;
   8261 
   8262   // OpenMP [teams Constrcut, Restrictions]
   8263   // The num_teams expression must evaluate to a positive integer value.
   8264   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
   8265                                  /*StrictlyPositive=*/true))
   8266     return nullptr;
   8267 
   8268   return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
   8269 }
   8270 
   8271 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
   8272                                               SourceLocation StartLoc,
   8273                                               SourceLocation LParenLoc,
   8274                                               SourceLocation EndLoc) {
   8275   Expr *ValExpr = ThreadLimit;
   8276 
   8277   // OpenMP [teams Constrcut, Restrictions]
   8278   // The thread_limit expression must evaluate to a positive integer value.
   8279   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
   8280                                  /*StrictlyPositive=*/true))
   8281     return nullptr;
   8282 
   8283   return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
   8284                                             EndLoc);
   8285 }
   8286 
   8287 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
   8288                                            SourceLocation StartLoc,
   8289                                            SourceLocation LParenLoc,
   8290                                            SourceLocation EndLoc) {
   8291   Expr *ValExpr = Priority;
   8292 
   8293   // OpenMP [2.9.1, task Constrcut]
   8294   // The priority-value is a non-negative numerical scalar expression.
   8295   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
   8296                                  /*StrictlyPositive=*/false))
   8297     return nullptr;
   8298 
   8299   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
   8300 }
   8301 
   8302 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
   8303                                             SourceLocation StartLoc,
   8304                                             SourceLocation LParenLoc,
   8305                                             SourceLocation EndLoc) {
   8306   Expr *ValExpr = Grainsize;
   8307 
   8308   // OpenMP [2.9.2, taskloop Constrcut]
   8309   // The parameter of the grainsize clause must be a positive integer
   8310   // expression.
   8311   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
   8312                                  /*StrictlyPositive=*/true))
   8313     return nullptr;
   8314 
   8315   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
   8316 }
   8317 
   8318 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
   8319                                            SourceLocation StartLoc,
   8320                                            SourceLocation LParenLoc,
   8321                                            SourceLocation EndLoc) {
   8322   Expr *ValExpr = NumTasks;
   8323 
   8324   // OpenMP [2.9.2, taskloop Constrcut]
   8325   // The parameter of the num_tasks clause must be a positive integer
   8326   // expression.
   8327   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
   8328                                  /*StrictlyPositive=*/true))
   8329     return nullptr;
   8330 
   8331   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
   8332 }
   8333 
   8334 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
   8335                                        SourceLocation LParenLoc,
   8336                                        SourceLocation EndLoc) {
   8337   // OpenMP [2.13.2, critical construct, Description]
   8338   // ... where hint-expression is an integer constant expression that evaluates
   8339   // to a valid lock hint.
   8340   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
   8341   if (HintExpr.isInvalid())
   8342     return nullptr;
   8343   return new (Context)
   8344       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
   8345 }
   8346 
   8347