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