Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1 | //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===// |
Alexey Bataev | c640058 | 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 | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 11 | /// clauses. |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 15 | #include "clang/AST/ASTContext.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | 4fa7eab | 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" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 22 | #include "clang/Basic/OpenMPKinds.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Initialization.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 25 | #include "clang/Sema/Lookup.h" |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Scope.h" |
| 27 | #include "clang/Sema/ScopeInfo.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 28 | #include "clang/Sema/SemaInternal.h" |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Alexey Bataev | 0c01835 | 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 { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [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 | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /// \brief Stack for tracking declarations used in OpenMP directives and |
| 44 | /// clauses and their data-sharing attributes. |
| 45 | class DSAStackTy { |
| 46 | public: |
| 47 | struct DSAVarData { |
| 48 | OpenMPDirectiveKind DKind; |
| 49 | OpenMPClauseKind CKind; |
| 50 | DeclRefExpr *RefExpr; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 51 | DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr) {} |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 52 | }; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 53 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 54 | private: |
| 55 | struct DSAInfo { |
| 56 | OpenMPClauseKind Attributes; |
| 57 | DeclRefExpr *RefExpr; |
| 58 | }; |
| 59 | typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy; |
| 60 | |
| 61 | struct SharingMapTy { |
| 62 | DeclSAMapTy SharingMap; |
| 63 | DefaultDataSharingAttributes DefaultAttr; |
| 64 | OpenMPDirectiveKind Directive; |
| 65 | DeclarationNameInfo DirectiveName; |
| 66 | Scope *CurScope; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 67 | SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name, |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 68 | Scope *CurScope) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 69 | : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind), |
| 70 | DirectiveName(std::move(Name)), CurScope(CurScope) {} |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 71 | SharingMapTy() |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 72 | : SharingMap(), DefaultAttr(DSA_unspecified), Directive(OMPD_unknown), |
| 73 | DirectiveName(), CurScope(nullptr) {} |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | typedef SmallVector<SharingMapTy, 64> StackTy; |
| 77 | |
| 78 | /// \brief Stack of used declaration and their data-sharing attributes. |
| 79 | StackTy Stack; |
| 80 | Sema &Actions; |
| 81 | |
| 82 | typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator; |
| 83 | |
| 84 | DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 85 | |
| 86 | /// \brief Checks if the variable is a local for OpenMP region. |
| 87 | bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 88 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 89 | public: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 90 | explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) {} |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 91 | |
| 92 | void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName, |
| 93 | Scope *CurScope) { |
| 94 | Stack.push_back(SharingMapTy(DKind, DirName, CurScope)); |
| 95 | } |
| 96 | |
| 97 | void pop() { |
| 98 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!"); |
| 99 | Stack.pop_back(); |
| 100 | } |
| 101 | |
| 102 | /// \brief Adds explicit data sharing attribute to the specified declaration. |
| 103 | void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A); |
| 104 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 105 | /// \brief Returns data sharing attributes from top of the stack for the |
| 106 | /// specified declaration. |
| 107 | DSAVarData getTopDSA(VarDecl *D); |
| 108 | /// \brief Returns data-sharing attributes for the specified declaration. |
| 109 | DSAVarData getImplicitDSA(VarDecl *D); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 110 | /// \brief Checks if the specified variables has \a CKind data-sharing |
| 111 | /// attribute in \a DKind directive. |
| 112 | DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind, |
| 113 | OpenMPDirectiveKind DKind = OMPD_unknown); |
| 114 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 115 | /// \brief Returns currently analyzed directive. |
| 116 | OpenMPDirectiveKind getCurrentDirective() const { |
| 117 | return Stack.back().Directive; |
| 118 | } |
| 119 | |
| 120 | /// \brief Set default data sharing attribute to none. |
| 121 | void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; } |
| 122 | /// \brief Set default data sharing attribute to shared. |
| 123 | void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; } |
| 124 | |
| 125 | DefaultDataSharingAttributes getDefaultDSA() const { |
| 126 | return Stack.back().DefaultAttr; |
| 127 | } |
| 128 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 129 | /// \brief Checks if the spewcified variable is threadprivate. |
| 130 | bool isThreadPrivate(VarDecl *D) { |
| 131 | DSAVarData DVar = getTopDSA(D); |
| 132 | return (DVar.CKind == OMPC_threadprivate || DVar.CKind == OMPC_copyin); |
| 133 | } |
| 134 | |
| 135 | Scope *getCurScope() const { return Stack.back().CurScope; } |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 136 | Scope *getCurScope() { return Stack.back().CurScope; } |
| 137 | }; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 138 | } // namespace |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 139 | |
| 140 | DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter, |
| 141 | VarDecl *D) { |
| 142 | DSAVarData DVar; |
| 143 | if (Iter == Stack.rend() - 1) { |
| 144 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 145 | // in a region but not in construct] |
| 146 | // File-scope or namespace-scope variables referenced in called routines |
| 147 | // in the region are shared unless they appear in a threadprivate |
| 148 | // directive. |
| 149 | // TODO |
| 150 | if (!D->isFunctionOrMethodVarDecl()) |
| 151 | DVar.CKind = OMPC_shared; |
| 152 | |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 153 | // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced |
| 154 | // in a region but not in construct] |
| 155 | // Variables with static storage duration that are declared in called |
| 156 | // routines in the region are shared. |
| 157 | if (D->hasGlobalStorage()) |
| 158 | DVar.CKind = OMPC_shared; |
| 159 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 160 | return DVar; |
| 161 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 162 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 163 | DVar.DKind = Iter->Directive; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 164 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 165 | // in a Construct, C/C++, predetermined, p.1] |
| 166 | // Variables with automatic storage duration that are declared in a scope |
| 167 | // inside the construct are private. |
| 168 | if (DVar.DKind != OMPD_parallel) { |
| 169 | if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() && |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 170 | (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 171 | DVar.CKind = OMPC_private; |
| 172 | return DVar; |
| 173 | } |
| 174 | } |
| 175 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 176 | // Explicitly specified attributes and local variables with predetermined |
| 177 | // attributes. |
| 178 | if (Iter->SharingMap.count(D)) { |
| 179 | DVar.RefExpr = Iter->SharingMap[D].RefExpr; |
| 180 | DVar.CKind = Iter->SharingMap[D].Attributes; |
| 181 | return DVar; |
| 182 | } |
| 183 | |
| 184 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 185 | // in a Construct, C/C++, implicitly determined, p.1] |
| 186 | // In a parallel or task construct, the data-sharing attributes of these |
| 187 | // variables are determined by the default clause, if present. |
| 188 | switch (Iter->DefaultAttr) { |
| 189 | case DSA_shared: |
| 190 | DVar.CKind = OMPC_shared; |
| 191 | return DVar; |
| 192 | case DSA_none: |
| 193 | return DVar; |
| 194 | case DSA_unspecified: |
| 195 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 196 | // in a Construct, implicitly determined, p.2] |
| 197 | // In a parallel construct, if no default clause is present, these |
| 198 | // variables are shared. |
| 199 | if (DVar.DKind == OMPD_parallel) { |
| 200 | DVar.CKind = OMPC_shared; |
| 201 | return DVar; |
| 202 | } |
| 203 | |
| 204 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 205 | // in a Construct, implicitly determined, p.4] |
| 206 | // In a task construct, if no default clause is present, a variable that in |
| 207 | // the enclosing context is determined to be shared by all implicit tasks |
| 208 | // bound to the current team is shared. |
| 209 | // TODO |
| 210 | if (DVar.DKind == OMPD_task) { |
| 211 | DSAVarData DVarTemp; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 212 | for (StackTy::reverse_iterator I = std::next(Iter), |
| 213 | EE = std::prev(Stack.rend()); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 214 | I != EE; ++I) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 215 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables |
| 216 | // Referenced |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 217 | // in a Construct, implicitly determined, p.6] |
| 218 | // In a task construct, if no default clause is present, a variable |
| 219 | // whose data-sharing attribute is not determined by the rules above is |
| 220 | // firstprivate. |
| 221 | DVarTemp = getDSA(I, D); |
| 222 | if (DVarTemp.CKind != OMPC_shared) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 223 | DVar.RefExpr = nullptr; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 224 | DVar.DKind = OMPD_task; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 225 | DVar.CKind = OMPC_firstprivate; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 226 | return DVar; |
| 227 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 228 | if (I->Directive == OMPD_parallel) |
| 229 | break; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 230 | } |
| 231 | DVar.DKind = OMPD_task; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 232 | DVar.CKind = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 233 | (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 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.3] |
| 239 | // For constructs other than task, if no default clause is present, these |
| 240 | // variables inherit their data-sharing attributes from the enclosing |
| 241 | // context. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 242 | return getDSA(std::next(Iter), D); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) { |
| 246 | if (A == OMPC_threadprivate) { |
| 247 | Stack[0].SharingMap[D].Attributes = A; |
| 248 | Stack[0].SharingMap[D].RefExpr = E; |
| 249 | } else { |
| 250 | assert(Stack.size() > 1 && "Data-sharing attributes stack is empty"); |
| 251 | Stack.back().SharingMap[D].Attributes = A; |
| 252 | Stack.back().SharingMap[D].RefExpr = E; |
| 253 | } |
| 254 | } |
| 255 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 256 | bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 257 | if (Stack.size() > 2) { |
| 258 | reverse_iterator I = Iter, E = Stack.rend() - 1; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 259 | Scope *TopScope = nullptr; |
| 260 | while (I != E && I->Directive != OMPD_parallel) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 261 | ++I; |
| 262 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 263 | if (I == E) |
| 264 | return false; |
| 265 | TopScope = I->CurScope ? I->CurScope->getParent() : nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 266 | Scope *CurScope = getCurScope(); |
| 267 | while (CurScope != TopScope && !CurScope->isDeclScope(D)) { |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 268 | CurScope = CurScope->getParent(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 269 | } |
| 270 | return CurScope != TopScope; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 271 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 272 | return false; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) { |
| 276 | DSAVarData DVar; |
| 277 | |
| 278 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 279 | // in a Construct, C/C++, predetermined, p.1] |
| 280 | // Variables appearing in threadprivate directives are threadprivate. |
| 281 | if (D->getTLSKind() != VarDecl::TLS_None) { |
| 282 | DVar.CKind = OMPC_threadprivate; |
| 283 | return DVar; |
| 284 | } |
| 285 | if (Stack[0].SharingMap.count(D)) { |
| 286 | DVar.RefExpr = Stack[0].SharingMap[D].RefExpr; |
| 287 | DVar.CKind = OMPC_threadprivate; |
| 288 | return DVar; |
| 289 | } |
| 290 | |
| 291 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 292 | // in a Construct, C/C++, predetermined, p.1] |
| 293 | // Variables with automatic storage duration that are declared in a scope |
| 294 | // inside the construct are private. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 295 | OpenMPDirectiveKind Kind = getCurrentDirective(); |
| 296 | if (Kind != OMPD_parallel) { |
| 297 | if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() && |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 298 | (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 299 | DVar.CKind = OMPC_private; |
| 300 | return DVar; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 301 | } |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 305 | // in a Construct, C/C++, predetermined, p.4] |
| 306 | // Static data memebers are shared. |
| 307 | if (D->isStaticDataMember()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 308 | // Variables with const-qualified type having no mutable member may be |
| 309 | // listed |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 310 | // in a firstprivate clause, even if they are static data members. |
| 311 | DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate); |
| 312 | if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) |
| 313 | return DVar; |
| 314 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 315 | DVar.CKind = OMPC_shared; |
| 316 | return DVar; |
| 317 | } |
| 318 | |
| 319 | QualType Type = D->getType().getNonReferenceType().getCanonicalType(); |
| 320 | bool IsConstant = Type.isConstant(Actions.getASTContext()); |
| 321 | while (Type->isArrayType()) { |
| 322 | QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType(); |
| 323 | Type = ElemType.getNonReferenceType().getCanonicalType(); |
| 324 | } |
| 325 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 326 | // in a Construct, C/C++, predetermined, p.6] |
| 327 | // Variables with const qualified type having no mutable member are |
| 328 | // shared. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 329 | CXXRecordDecl *RD = |
| 330 | Actions.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 331 | if (IsConstant && |
| 332 | !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) { |
| 333 | // Variables with const-qualified type having no mutable member may be |
| 334 | // listed in a firstprivate clause, even if they are static data members. |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 335 | DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate); |
| 336 | if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr) |
| 337 | return DVar; |
| 338 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 339 | DVar.CKind = OMPC_shared; |
| 340 | return DVar; |
| 341 | } |
| 342 | |
| 343 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 344 | // in a Construct, C/C++, predetermined, p.7] |
| 345 | // Variables with static storage duration that are declared in a scope |
| 346 | // inside the construct are shared. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 347 | if (D->isStaticLocal()) { |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 348 | DVar.CKind = OMPC_shared; |
| 349 | return DVar; |
| 350 | } |
| 351 | |
| 352 | // Explicitly specified attributes and local variables with predetermined |
| 353 | // attributes. |
| 354 | if (Stack.back().SharingMap.count(D)) { |
| 355 | DVar.RefExpr = Stack.back().SharingMap[D].RefExpr; |
| 356 | DVar.CKind = Stack.back().SharingMap[D].Attributes; |
| 357 | } |
| 358 | |
| 359 | return DVar; |
| 360 | } |
| 361 | |
| 362 | DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 363 | return getDSA(std::next(Stack.rbegin()), D); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 366 | DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind, |
| 367 | OpenMPDirectiveKind DKind) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 368 | for (StackTy::reverse_iterator I = std::next(Stack.rbegin()), |
| 369 | E = std::prev(Stack.rend()); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 370 | I != E; ++I) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 371 | if (DKind != OMPD_unknown && DKind != I->Directive) |
| 372 | continue; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 373 | DSAVarData DVar = getDSA(I, D); |
| 374 | if (DVar.CKind == CKind) |
| 375 | return DVar; |
| 376 | } |
| 377 | return DSAVarData(); |
| 378 | } |
| 379 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 380 | void Sema::InitDataSharingAttributesStack() { |
| 381 | VarDataSharingAttributesStack = new DSAStackTy(*this); |
| 382 | } |
| 383 | |
| 384 | #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack) |
| 385 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 386 | void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; } |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 387 | |
| 388 | void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind, |
| 389 | const DeclarationNameInfo &DirName, |
| 390 | Scope *CurScope) { |
| 391 | DSAStack->push(DKind, DirName, CurScope); |
| 392 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
| 393 | } |
| 394 | |
| 395 | void Sema::EndOpenMPDSABlock(Stmt *CurDirective) { |
| 396 | DSAStack->pop(); |
| 397 | DiscardCleanupsInEvaluationContext(); |
| 398 | PopExpressionEvaluationContext(); |
| 399 | } |
| 400 | |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 401 | namespace { |
| 402 | |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 403 | class VarDeclFilterCCC : public CorrectionCandidateCallback { |
| 404 | private: |
| 405 | Sema &Actions; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 406 | |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 407 | public: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 408 | VarDeclFilterCCC(Sema &S) : Actions(S) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 409 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 410 | NamedDecl *ND = Candidate.getCorrectionDecl(); |
| 411 | if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) { |
| 412 | return VD->hasGlobalStorage() && |
| 413 | Actions.isDeclInScope(ND, Actions.getCurLexicalContext(), |
| 414 | Actions.getCurScope()); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 415 | } |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 416 | return false; |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 417 | } |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 418 | }; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 419 | } // namespace |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 420 | |
| 421 | ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope, |
| 422 | CXXScopeSpec &ScopeSpec, |
| 423 | const DeclarationNameInfo &Id) { |
| 424 | LookupResult Lookup(*this, Id, LookupOrdinaryName); |
| 425 | LookupParsedName(Lookup, CurScope, &ScopeSpec, true); |
| 426 | |
| 427 | if (Lookup.isAmbiguous()) |
| 428 | return ExprError(); |
| 429 | |
| 430 | VarDecl *VD; |
| 431 | if (!Lookup.isSingleResult()) { |
| 432 | VarDeclFilterCCC Validator(*this); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 433 | if (TypoCorrection Corrected = |
| 434 | CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator, |
| 435 | CTK_ErrorRecovery)) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 436 | diagnoseTypo(Corrected, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 437 | PDiag(Lookup.empty() |
| 438 | ? diag::err_undeclared_var_use_suggest |
| 439 | : diag::err_omp_expected_var_arg_suggest) |
| 440 | << Id.getName()); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 441 | VD = Corrected.getCorrectionDeclAs<VarDecl>(); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 442 | } else { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 443 | Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use |
| 444 | : diag::err_omp_expected_var_arg) |
| 445 | << Id.getName(); |
| 446 | return ExprError(); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 447 | } |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 448 | } else { |
| 449 | if (!(VD = Lookup.getAsSingle<VarDecl>())) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 450 | Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName(); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 451 | Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at); |
| 452 | return ExprError(); |
| 453 | } |
| 454 | } |
| 455 | Lookup.suppressDiagnostics(); |
| 456 | |
| 457 | // OpenMP [2.9.2, Syntax, C/C++] |
| 458 | // Variables must be file-scope, namespace-scope, or static block-scope. |
| 459 | if (!VD->hasGlobalStorage()) { |
| 460 | Diag(Id.getLoc(), diag::err_omp_global_var_arg) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 461 | << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal(); |
| 462 | bool IsDecl = |
| 463 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 464 | Diag(VD->getLocation(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 465 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 466 | << VD; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 467 | return ExprError(); |
| 468 | } |
| 469 | |
Alexey Bataev | d0dbb7e | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 470 | VarDecl *CanonicalVD = VD->getCanonicalDecl(); |
| 471 | NamedDecl *ND = cast<NamedDecl>(CanonicalVD); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 472 | // OpenMP [2.9.2, Restrictions, C/C++, p.2] |
| 473 | // A threadprivate directive for file-scope variables must appear outside |
| 474 | // any definition or declaration. |
Alexey Bataev | d0dbb7e | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 475 | if (CanonicalVD->getDeclContext()->isTranslationUnit() && |
| 476 | !getCurLexicalContext()->isTranslationUnit()) { |
| 477 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 478 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 479 | bool IsDecl = |
| 480 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 481 | Diag(VD->getLocation(), |
| 482 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 483 | << VD; |
Alexey Bataev | d0dbb7e | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 484 | return ExprError(); |
| 485 | } |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 486 | // OpenMP [2.9.2, Restrictions, C/C++, p.3] |
| 487 | // A threadprivate directive for static class member variables must appear |
| 488 | // in the class definition, in the same scope in which the member |
| 489 | // variables are declared. |
Alexey Bataev | d0dbb7e | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 490 | if (CanonicalVD->isStaticDataMember() && |
| 491 | !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) { |
| 492 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 493 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 494 | bool IsDecl = |
| 495 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 496 | Diag(VD->getLocation(), |
| 497 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 498 | << VD; |
Alexey Bataev | d0dbb7e | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 499 | return ExprError(); |
| 500 | } |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 501 | // OpenMP [2.9.2, Restrictions, C/C++, p.4] |
| 502 | // A threadprivate directive for namespace-scope variables must appear |
| 503 | // outside any definition or declaration other than the namespace |
| 504 | // definition itself. |
Alexey Bataev | d0dbb7e | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 505 | if (CanonicalVD->getDeclContext()->isNamespace() && |
| 506 | (!getCurLexicalContext()->isFileContext() || |
| 507 | !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) { |
| 508 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 509 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 510 | bool IsDecl = |
| 511 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 512 | Diag(VD->getLocation(), |
| 513 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 514 | << VD; |
Alexey Bataev | d0dbb7e | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 515 | return ExprError(); |
| 516 | } |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 517 | // OpenMP [2.9.2, Restrictions, C/C++, p.6] |
| 518 | // A threadprivate directive for static block-scope variables must appear |
| 519 | // in the scope of the variable and not in a nested scope. |
Alexey Bataev | d0dbb7e | 2013-09-26 03:24:06 +0000 | [diff] [blame] | 520 | if (CanonicalVD->isStaticLocal() && CurScope && |
| 521 | !isDeclInScope(ND, getCurLexicalContext(), CurScope)) { |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 522 | Diag(Id.getLoc(), diag::err_omp_var_scope) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 523 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
| 524 | bool IsDecl = |
| 525 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 526 | Diag(VD->getLocation(), |
| 527 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 528 | << VD; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 529 | return ExprError(); |
| 530 | } |
| 531 | |
| 532 | // OpenMP [2.9.2, Restrictions, C/C++, p.2-6] |
| 533 | // A threadprivate directive must lexically precede all references to any |
| 534 | // of the variables in its list. |
| 535 | if (VD->isUsed()) { |
| 536 | Diag(Id.getLoc(), diag::err_omp_var_used) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 537 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD; |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 538 | return ExprError(); |
| 539 | } |
| 540 | |
| 541 | QualType ExprType = VD->getType().getNonReferenceType(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 542 | ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc()); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 543 | return DE; |
| 544 | } |
| 545 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 546 | Sema::DeclGroupPtrTy |
| 547 | Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc, |
| 548 | ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 549 | if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 550 | CurContext->addDecl(D); |
| 551 | return DeclGroupPtrTy::make(DeclGroupRef(D)); |
| 552 | } |
| 553 | return DeclGroupPtrTy(); |
| 554 | } |
| 555 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 556 | OMPThreadPrivateDecl * |
| 557 | Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) { |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 558 | SmallVector<Expr *, 8> Vars; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 559 | for (auto &RefExpr : VarList) { |
| 560 | DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 561 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 562 | SourceLocation ILoc = DE->getExprLoc(); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 563 | |
| 564 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 565 | // A threadprivate variable must not have an incomplete type. |
| 566 | if (RequireCompleteType(ILoc, VD->getType(), |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 567 | diag::err_omp_threadprivate_incomplete_type)) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 568 | continue; |
| 569 | } |
| 570 | |
| 571 | // OpenMP [2.9.2, Restrictions, C/C++, p.10] |
| 572 | // A threadprivate variable must not have a reference type. |
| 573 | if (VD->getType()->isReferenceType()) { |
| 574 | Diag(ILoc, diag::err_omp_ref_type_arg) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 575 | << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType(); |
| 576 | bool IsDecl = |
| 577 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 578 | Diag(VD->getLocation(), |
| 579 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 580 | << VD; |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 581 | continue; |
| 582 | } |
| 583 | |
Richard Smith | 38afbc7 | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 584 | // Check if this is a TLS variable. |
| 585 | if (VD->getTLSKind()) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 586 | Diag(ILoc, diag::err_omp_var_thread_local) << VD; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 587 | bool IsDecl = |
| 588 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 589 | Diag(VD->getLocation(), |
| 590 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 591 | << VD; |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 592 | continue; |
| 593 | } |
| 594 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 595 | Vars.push_back(RefExpr); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 596 | DSAStack->addDSA(VD, DE, OMPC_threadprivate); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 597 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 598 | OMPThreadPrivateDecl *D = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 599 | if (!Vars.empty()) { |
| 600 | D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc, |
| 601 | Vars); |
| 602 | D->setAccess(AS_public); |
| 603 | } |
| 604 | return D; |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 605 | } |
Alexey Bataev | 6af701f | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 606 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 607 | namespace { |
| 608 | class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> { |
| 609 | DSAStackTy *Stack; |
| 610 | Sema &Actions; |
| 611 | bool ErrorFound; |
| 612 | CapturedStmt *CS; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 613 | llvm::SmallVector<Expr *, 8> ImplicitFirstprivate; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 614 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 615 | public: |
| 616 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 617 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 618 | // Skip internally declared variables. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 619 | if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) |
| 620 | return; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 621 | |
| 622 | SourceLocation ELoc = E->getExprLoc(); |
| 623 | |
| 624 | OpenMPDirectiveKind DKind = Stack->getCurrentDirective(); |
| 625 | DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD); |
| 626 | if (DVar.CKind != OMPC_unknown) { |
| 627 | if (DKind == OMPD_task && DVar.CKind != OMPC_shared && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 628 | !Stack->isThreadPrivate(VD) && !DVar.RefExpr) |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 629 | ImplicitFirstprivate.push_back(DVar.RefExpr); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 630 | return; |
| 631 | } |
| 632 | // The default(none) clause requires that each variable that is referenced |
| 633 | // in the construct, and does not have a predetermined data-sharing |
| 634 | // attribute, must have its data-sharing attribute explicitly determined |
| 635 | // by being listed in a data-sharing attribute clause. |
| 636 | if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none && |
| 637 | (DKind == OMPD_parallel || DKind == OMPD_task)) { |
| 638 | ErrorFound = true; |
| 639 | Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD; |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | // OpenMP [2.9.3.6, Restrictions, p.2] |
| 644 | // A list item that appears in a reduction clause of the innermost |
| 645 | // enclosing worksharing or parallel construct may not be accessed in an |
| 646 | // explicit task. |
| 647 | // TODO: |
| 648 | |
| 649 | // Define implicit data-sharing attributes for task. |
| 650 | DVar = Stack->getImplicitDSA(VD); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 651 | if (DKind == OMPD_task && DVar.CKind != OMPC_shared) |
| 652 | ImplicitFirstprivate.push_back(DVar.RefExpr); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | void VisitOMPExecutableDirective(OMPExecutableDirective *S) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 656 | for (auto C : S->clauses()) |
| 657 | if (C) |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 658 | for (StmtRange R = C->children(); R; ++R) |
| 659 | if (Stmt *Child = *R) |
| 660 | Visit(Child); |
| 661 | } |
| 662 | void VisitStmt(Stmt *S) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 663 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E; |
| 664 | ++I) |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 665 | if (Stmt *Child = *I) |
| 666 | if (!isa<OMPExecutableDirective>(Child)) |
| 667 | Visit(Child); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 668 | } |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 669 | |
| 670 | bool isErrorFound() { return ErrorFound; } |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 671 | ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; } |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 672 | |
| 673 | DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 674 | : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) {} |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 675 | }; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 676 | } // namespace |
| 677 | |
| 678 | void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc, |
| 679 | Scope *CurScope) { |
| 680 | switch (DKind) { |
| 681 | case OMPD_parallel: { |
| 682 | QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1); |
| 683 | QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty); |
| 684 | Sema::CapturedParamNameType Params[3] = { |
| 685 | std::make_pair(".global_tid.", KmpInt32PtrTy), |
| 686 | std::make_pair(".bound_tid.", KmpInt32PtrTy), |
| 687 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 688 | }; |
| 689 | ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params); |
| 690 | break; |
| 691 | } |
| 692 | case OMPD_simd: { |
| 693 | Sema::CapturedParamNameType Params[1] = { |
| 694 | std::make_pair(StringRef(), QualType()) // __context with shared vars |
| 695 | }; |
| 696 | ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params); |
| 697 | break; |
| 698 | } |
| 699 | case OMPD_threadprivate: |
| 700 | case OMPD_task: |
| 701 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 702 | case OMPD_unknown: |
| 703 | llvm_unreachable("Unknown OpenMP directive"); |
| 704 | } |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 707 | StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind, |
| 708 | ArrayRef<OMPClause *> Clauses, |
| 709 | Stmt *AStmt, |
| 710 | SourceLocation StartLoc, |
| 711 | SourceLocation EndLoc) { |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 712 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 713 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 714 | StmtResult Res = StmtError(); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 715 | |
| 716 | // Check default data sharing attributes for referenced variables. |
| 717 | DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt)); |
| 718 | DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt()); |
| 719 | if (DSAChecker.isErrorFound()) |
| 720 | return StmtError(); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 721 | // Generate list of implicitly defined firstprivate variables. |
| 722 | llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit; |
| 723 | ClausesWithImplicit.append(Clauses.begin(), Clauses.end()); |
| 724 | |
| 725 | bool ErrorFound = false; |
| 726 | if (!DSAChecker.getImplicitFirstprivate().empty()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 727 | if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause( |
| 728 | DSAChecker.getImplicitFirstprivate(), SourceLocation(), |
| 729 | SourceLocation(), SourceLocation())) { |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 730 | ClausesWithImplicit.push_back(Implicit); |
| 731 | ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() != |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 732 | DSAChecker.getImplicitFirstprivate().size(); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 733 | } else |
| 734 | ErrorFound = true; |
| 735 | } |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 736 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 737 | switch (Kind) { |
| 738 | case OMPD_parallel: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 739 | Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc, |
| 740 | EndLoc); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 741 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 742 | case OMPD_simd: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 743 | Res = |
| 744 | ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 745 | break; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 746 | case OMPD_threadprivate: |
| 747 | case OMPD_task: |
| 748 | llvm_unreachable("OpenMP Directive is not allowed"); |
| 749 | case OMPD_unknown: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 750 | llvm_unreachable("Unknown OpenMP directive"); |
| 751 | } |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 752 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 753 | if (ErrorFound) |
| 754 | return StmtError(); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 755 | return Res; |
| 756 | } |
| 757 | |
| 758 | StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses, |
| 759 | Stmt *AStmt, |
| 760 | SourceLocation StartLoc, |
| 761 | SourceLocation EndLoc) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 762 | assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected"); |
| 763 | CapturedStmt *CS = cast<CapturedStmt>(AStmt); |
| 764 | // 1.2.2 OpenMP Language Terminology |
| 765 | // Structured block - An executable statement with a single entry at the |
| 766 | // top and a single exit at the bottom. |
| 767 | // The point of exit cannot be a branch out of the structured block. |
| 768 | // longjmp() and throw() must not violate the entry/exit criteria. |
| 769 | CS->getCapturedDecl()->setNothrow(); |
| 770 | |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 771 | getCurFunction()->setHasBranchProtectedScope(); |
| 772 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 773 | return Owned( |
| 774 | OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt)); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 777 | StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 778 | Stmt *AStmt, SourceLocation StartLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 779 | SourceLocation EndLoc) { |
| 780 | Stmt *CStmt = AStmt; |
| 781 | while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt)) |
| 782 | CStmt = CS->getCapturedStmt(); |
| 783 | while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt)) |
| 784 | CStmt = AS->getSubStmt(); |
| 785 | ForStmt *For = dyn_cast<ForStmt>(CStmt); |
| 786 | if (!For) { |
| 787 | Diag(CStmt->getLocStart(), diag::err_omp_not_for) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 788 | << getOpenMPDirectiveName(OMPD_simd); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 789 | return StmtError(); |
| 790 | } |
| 791 | |
| 792 | // FIXME: Checking loop canonical form, collapsing etc. |
| 793 | |
| 794 | getCurFunction()->setHasBranchProtectedScope(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 795 | return Owned( |
| 796 | OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt)); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 799 | OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 800 | SourceLocation StartLoc, |
| 801 | SourceLocation LParenLoc, |
| 802 | SourceLocation EndLoc) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 803 | OMPClause *Res = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 804 | switch (Kind) { |
| 805 | case OMPC_if: |
| 806 | Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 807 | break; |
| 808 | case OMPC_num_threads: |
| 809 | Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 810 | break; |
| 811 | case OMPC_safelen: |
| 812 | Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 813 | break; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 814 | case OMPC_collapse: |
| 815 | Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc); |
| 816 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 817 | case OMPC_default: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 818 | case OMPC_proc_bind: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 819 | case OMPC_private: |
| 820 | case OMPC_firstprivate: |
| 821 | case OMPC_shared: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 822 | case OMPC_linear: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 823 | case OMPC_copyin: |
| 824 | case OMPC_threadprivate: |
| 825 | case OMPC_unknown: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 826 | llvm_unreachable("Clause is not allowed."); |
| 827 | } |
| 828 | return Res; |
| 829 | } |
| 830 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 831 | OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 832 | SourceLocation LParenLoc, |
| 833 | SourceLocation EndLoc) { |
| 834 | Expr *ValExpr = Condition; |
| 835 | if (!Condition->isValueDependent() && !Condition->isTypeDependent() && |
| 836 | !Condition->isInstantiationDependent() && |
| 837 | !Condition->containsUnexpandedParameterPack()) { |
| 838 | ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 839 | Condition->getExprLoc(), Condition); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 840 | if (Val.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 841 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 842 | |
| 843 | ValExpr = Val.take(); |
| 844 | } |
| 845 | |
| 846 | return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
| 847 | } |
| 848 | |
| 849 | ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc, |
| 850 | Expr *Op) { |
| 851 | if (!Op) |
| 852 | return ExprError(); |
| 853 | |
| 854 | class IntConvertDiagnoser : public ICEConvertDiagnoser { |
| 855 | public: |
| 856 | IntConvertDiagnoser() |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 857 | : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 858 | SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 859 | QualType T) override { |
| 860 | return S.Diag(Loc, diag::err_omp_not_integral) << T; |
| 861 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 862 | SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc, |
| 863 | QualType T) override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 864 | return S.Diag(Loc, diag::err_omp_incomplete_type) << T; |
| 865 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 866 | SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc, |
| 867 | QualType T, |
| 868 | QualType ConvTy) override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 869 | return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy; |
| 870 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 871 | SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv, |
| 872 | QualType ConvTy) override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 873 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 874 | << ConvTy->isEnumeralType() << ConvTy; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 875 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 876 | SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 877 | QualType T) override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 878 | return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T; |
| 879 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 880 | SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv, |
| 881 | QualType ConvTy) override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 882 | return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 883 | << ConvTy->isEnumeralType() << ConvTy; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 884 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 885 | SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType, |
| 886 | QualType) override { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 887 | llvm_unreachable("conversion functions are permitted"); |
| 888 | } |
| 889 | } ConvertDiagnoser; |
| 890 | return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser); |
| 891 | } |
| 892 | |
| 893 | OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads, |
| 894 | SourceLocation StartLoc, |
| 895 | SourceLocation LParenLoc, |
| 896 | SourceLocation EndLoc) { |
| 897 | Expr *ValExpr = NumThreads; |
| 898 | if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() && |
| 899 | !NumThreads->isInstantiationDependent() && |
| 900 | !NumThreads->containsUnexpandedParameterPack()) { |
| 901 | SourceLocation NumThreadsLoc = NumThreads->getLocStart(); |
| 902 | ExprResult Val = |
| 903 | PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads); |
| 904 | if (Val.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 905 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 906 | |
| 907 | ValExpr = Val.take(); |
| 908 | |
| 909 | // OpenMP [2.5, Restrictions] |
| 910 | // The num_threads expression must evaluate to a positive integer value. |
| 911 | llvm::APSInt Result; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 912 | if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() && |
| 913 | !Result.isStrictlyPositive()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 914 | Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause) |
| 915 | << "num_threads" << NumThreads->getSourceRange(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 916 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 920 | return new (Context) |
| 921 | OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E, |
| 925 | OpenMPClauseKind CKind) { |
| 926 | if (!E) |
| 927 | return ExprError(); |
| 928 | if (E->isValueDependent() || E->isTypeDependent() || |
| 929 | E->isInstantiationDependent() || E->containsUnexpandedParameterPack()) |
| 930 | return Owned(E); |
| 931 | llvm::APSInt Result; |
| 932 | ExprResult ICE = VerifyIntegerConstantExpression(E, &Result); |
| 933 | if (ICE.isInvalid()) |
| 934 | return ExprError(); |
| 935 | if (!Result.isStrictlyPositive()) { |
| 936 | Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause) |
| 937 | << getOpenMPClauseName(CKind) << E->getSourceRange(); |
| 938 | return ExprError(); |
| 939 | } |
| 940 | return ICE; |
| 941 | } |
| 942 | |
| 943 | OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc, |
| 944 | SourceLocation LParenLoc, |
| 945 | SourceLocation EndLoc) { |
| 946 | // OpenMP [2.8.1, simd construct, Description] |
| 947 | // The parameter of the safelen clause must be a constant |
| 948 | // positive integer expression. |
| 949 | ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen); |
| 950 | if (Safelen.isInvalid()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 951 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 952 | return new (Context) |
| 953 | OMPSafelenClause(Safelen.take(), StartLoc, LParenLoc, EndLoc); |
| 954 | } |
| 955 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 956 | OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *Num, SourceLocation StartLoc, |
| 957 | SourceLocation LParenLoc, |
| 958 | SourceLocation EndLoc) { |
| 959 | // OpenMP [2.8.1, simd construct, Description] |
| 960 | // The parameter of the collapse clause must be a constant |
| 961 | // positive integer expression. |
| 962 | ExprResult NumForLoops = |
| 963 | VerifyPositiveIntegerConstantInClause(Num, OMPC_collapse); |
| 964 | if (NumForLoops.isInvalid()) |
| 965 | return nullptr; |
| 966 | return new (Context) |
| 967 | OMPCollapseClause(NumForLoops.take(), StartLoc, LParenLoc, EndLoc); |
| 968 | } |
| 969 | |
| 970 | OMPClause *Sema::ActOnOpenMPSimpleClause( |
| 971 | OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc, |
| 972 | SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) { |
| 973 | OMPClause *Res = nullptr; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 974 | switch (Kind) { |
| 975 | case OMPC_default: |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 976 | Res = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 977 | ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument), |
| 978 | ArgumentLoc, StartLoc, LParenLoc, EndLoc); |
| 979 | break; |
| 980 | case OMPC_proc_bind: |
| 981 | Res = ActOnOpenMPProcBindClause( |
| 982 | static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc, |
| 983 | LParenLoc, EndLoc); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 984 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 985 | case OMPC_if: |
| 986 | case OMPC_num_threads: |
| 987 | case OMPC_safelen: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 988 | case OMPC_collapse: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 989 | case OMPC_private: |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 990 | case OMPC_firstprivate: |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 991 | case OMPC_shared: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 992 | case OMPC_linear: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 993 | case OMPC_copyin: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 994 | case OMPC_threadprivate: |
| 995 | case OMPC_unknown: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 996 | llvm_unreachable("Clause is not allowed."); |
| 997 | } |
| 998 | return Res; |
| 999 | } |
| 1000 | |
| 1001 | OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind, |
| 1002 | SourceLocation KindKwLoc, |
| 1003 | SourceLocation StartLoc, |
| 1004 | SourceLocation LParenLoc, |
| 1005 | SourceLocation EndLoc) { |
| 1006 | if (Kind == OMPC_DEFAULT_unknown) { |
| 1007 | std::string Values; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1008 | static_assert(OMPC_DEFAULT_unknown > 0, |
| 1009 | "OMPC_DEFAULT_unknown not greater than 0"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1010 | std::string Sep(", "); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1011 | for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) { |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1012 | Values += "'"; |
| 1013 | Values += getOpenMPSimpleClauseTypeName(OMPC_default, i); |
| 1014 | Values += "'"; |
| 1015 | switch (i) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1016 | case OMPC_DEFAULT_unknown - 2: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1017 | Values += " or "; |
| 1018 | break; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1019 | case OMPC_DEFAULT_unknown - 1: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | default: |
| 1022 | Values += Sep; |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1027 | << Values << getOpenMPClauseName(OMPC_default); |
| 1028 | return nullptr; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1029 | } |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1030 | switch (Kind) { |
| 1031 | case OMPC_DEFAULT_none: |
| 1032 | DSAStack->setDefaultDSANone(); |
| 1033 | break; |
| 1034 | case OMPC_DEFAULT_shared: |
| 1035 | DSAStack->setDefaultDSAShared(); |
| 1036 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1037 | case OMPC_DEFAULT_unknown: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1038 | llvm_unreachable("Clause kind is not allowed."); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1039 | break; |
| 1040 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1041 | return new (Context) |
| 1042 | OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1045 | OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind, |
| 1046 | SourceLocation KindKwLoc, |
| 1047 | SourceLocation StartLoc, |
| 1048 | SourceLocation LParenLoc, |
| 1049 | SourceLocation EndLoc) { |
| 1050 | if (Kind == OMPC_PROC_BIND_unknown) { |
| 1051 | std::string Values; |
| 1052 | std::string Sep(", "); |
| 1053 | for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) { |
| 1054 | Values += "'"; |
| 1055 | Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i); |
| 1056 | Values += "'"; |
| 1057 | switch (i) { |
| 1058 | case OMPC_PROC_BIND_unknown - 2: |
| 1059 | Values += " or "; |
| 1060 | break; |
| 1061 | case OMPC_PROC_BIND_unknown - 1: |
| 1062 | break; |
| 1063 | default: |
| 1064 | Values += Sep; |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | Diag(KindKwLoc, diag::err_omp_unexpected_clause_value) |
| 1069 | << Values << getOpenMPClauseName(OMPC_proc_bind); |
| 1070 | return nullptr; |
| 1071 | } |
| 1072 | return new (Context) |
| 1073 | OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc); |
| 1074 | } |
| 1075 | |
| 1076 | OMPClause * |
| 1077 | Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, |
| 1078 | Expr *TailExpr, SourceLocation StartLoc, |
| 1079 | SourceLocation LParenLoc, |
| 1080 | SourceLocation ColonLoc, SourceLocation EndLoc) { |
| 1081 | OMPClause *Res = nullptr; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1082 | switch (Kind) { |
| 1083 | case OMPC_private: |
| 1084 | Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 1085 | break; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1086 | case OMPC_firstprivate: |
| 1087 | Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 1088 | break; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1089 | case OMPC_shared: |
| 1090 | Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 1091 | break; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1092 | case OMPC_linear: |
| 1093 | Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc, |
| 1094 | ColonLoc, EndLoc); |
| 1095 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1096 | case OMPC_copyin: |
| 1097 | Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc); |
| 1098 | break; |
| 1099 | case OMPC_if: |
| 1100 | case OMPC_num_threads: |
| 1101 | case OMPC_safelen: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1102 | case OMPC_collapse: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1103 | case OMPC_default: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1104 | case OMPC_proc_bind: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1105 | case OMPC_threadprivate: |
| 1106 | case OMPC_unknown: |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1107 | llvm_unreachable("Clause is not allowed."); |
| 1108 | } |
| 1109 | return Res; |
| 1110 | } |
| 1111 | |
| 1112 | OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList, |
| 1113 | SourceLocation StartLoc, |
| 1114 | SourceLocation LParenLoc, |
| 1115 | SourceLocation EndLoc) { |
| 1116 | SmallVector<Expr *, 8> Vars; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1117 | for (auto &RefExpr : VarList) { |
| 1118 | assert(RefExpr && "NULL expr in OpenMP private clause."); |
| 1119 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1120 | // It will be analyzed later. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1121 | Vars.push_back(RefExpr); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1122 | continue; |
| 1123 | } |
| 1124 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1125 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1126 | // OpenMP [2.1, C/C++] |
| 1127 | // A list item is a variable name. |
| 1128 | // OpenMP [2.9.3.3, Restrictions, p.1] |
| 1129 | // A variable that is part of another variable (as an array or |
| 1130 | // structure element) cannot appear in a private clause. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1131 | DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1132 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1133 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1134 | continue; |
| 1135 | } |
| 1136 | Decl *D = DE->getDecl(); |
| 1137 | VarDecl *VD = cast<VarDecl>(D); |
| 1138 | |
| 1139 | QualType Type = VD->getType(); |
| 1140 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 1141 | // It will be analyzed later. |
| 1142 | Vars.push_back(DE); |
| 1143 | continue; |
| 1144 | } |
| 1145 | |
| 1146 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 1147 | // A variable that appears in a private clause must not have an incomplete |
| 1148 | // type or a reference type. |
| 1149 | if (RequireCompleteType(ELoc, Type, |
| 1150 | diag::err_omp_private_incomplete_type)) { |
| 1151 | continue; |
| 1152 | } |
| 1153 | if (Type->isReferenceType()) { |
| 1154 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1155 | << getOpenMPClauseName(OMPC_private) << Type; |
| 1156 | bool IsDecl = |
| 1157 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1158 | Diag(VD->getLocation(), |
| 1159 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1160 | << VD; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1161 | continue; |
| 1162 | } |
| 1163 | |
| 1164 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.1] |
| 1165 | // A variable of class type (or array thereof) that appears in a private |
| 1166 | // clause requires an accesible, unambiguous default constructor for the |
| 1167 | // class type. |
| 1168 | while (Type.getNonReferenceType()->isArrayType()) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1169 | Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr()) |
| 1170 | ->getElementType(); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1171 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1172 | CXXRecordDecl *RD = getLangOpts().CPlusPlus |
| 1173 | ? Type.getNonReferenceType()->getAsCXXRecordDecl() |
| 1174 | : nullptr; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1175 | if (RD) { |
| 1176 | CXXConstructorDecl *CD = LookupDefaultConstructor(RD); |
| 1177 | PartialDiagnostic PD = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1178 | PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); |
| 1179 | if (!CD || CheckConstructorAccess( |
| 1180 | ELoc, CD, InitializedEntity::InitializeTemporary(Type), |
| 1181 | CD->getAccess(), PD) == AR_inaccessible || |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1182 | CD->isDeleted()) { |
| 1183 | Diag(ELoc, diag::err_omp_required_method) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1184 | << getOpenMPClauseName(OMPC_private) << 0; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1185 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 1186 | VarDecl::DeclarationOnly; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1187 | Diag(VD->getLocation(), |
| 1188 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1189 | << VD; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1190 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 1191 | continue; |
| 1192 | } |
| 1193 | MarkFunctionReferenced(ELoc, CD); |
| 1194 | DiagnoseUseOfDecl(CD, ELoc); |
| 1195 | |
| 1196 | CXXDestructorDecl *DD = RD->getDestructor(); |
| 1197 | if (DD) { |
| 1198 | if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || |
| 1199 | DD->isDeleted()) { |
| 1200 | Diag(ELoc, diag::err_omp_required_method) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1201 | << getOpenMPClauseName(OMPC_private) << 4; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1202 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 1203 | VarDecl::DeclarationOnly; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1204 | Diag(VD->getLocation(), |
| 1205 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1206 | << VD; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1207 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 1208 | continue; |
| 1209 | } |
| 1210 | MarkFunctionReferenced(ELoc, DD); |
| 1211 | DiagnoseUseOfDecl(DD, ELoc); |
| 1212 | } |
| 1213 | } |
| 1214 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1215 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 1216 | // in a Construct] |
| 1217 | // Variables with the predetermined data-sharing attributes may not be |
| 1218 | // listed in data-sharing attributes clauses, except for the cases |
| 1219 | // listed below. For these exceptions only, listing a predetermined |
| 1220 | // variable in a data-sharing attribute clause is allowed and overrides |
| 1221 | // the variable's predetermined data-sharing attributes. |
| 1222 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); |
| 1223 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1224 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 1225 | << getOpenMPClauseName(OMPC_private); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1226 | if (DVar.RefExpr) { |
| 1227 | Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1228 | << getOpenMPClauseName(DVar.CKind); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1229 | } else { |
| 1230 | Diag(VD->getLocation(), diag::note_omp_predetermined_dsa) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1231 | << getOpenMPClauseName(DVar.CKind); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1232 | } |
| 1233 | continue; |
| 1234 | } |
| 1235 | |
| 1236 | DSAStack->addDSA(VD, DE, OMPC_private); |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1237 | Vars.push_back(DE); |
| 1238 | } |
| 1239 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1240 | if (Vars.empty()) |
| 1241 | return nullptr; |
Alexey Bataev | 4fa7eab | 2013-07-19 03:13:43 +0000 | [diff] [blame] | 1242 | |
| 1243 | return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 1244 | } |
| 1245 | |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1246 | OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList, |
| 1247 | SourceLocation StartLoc, |
| 1248 | SourceLocation LParenLoc, |
| 1249 | SourceLocation EndLoc) { |
| 1250 | SmallVector<Expr *, 8> Vars; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1251 | for (auto &RefExpr : VarList) { |
| 1252 | assert(RefExpr && "NULL expr in OpenMP firstprivate clause."); |
| 1253 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1254 | // It will be analyzed later. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1255 | Vars.push_back(RefExpr); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1256 | continue; |
| 1257 | } |
| 1258 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1259 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1260 | // OpenMP [2.1, C/C++] |
| 1261 | // A list item is a variable name. |
| 1262 | // OpenMP [2.9.3.3, Restrictions, p.1] |
| 1263 | // A variable that is part of another variable (as an array or |
| 1264 | // structure element) cannot appear in a private clause. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1265 | DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1266 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1267 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1268 | continue; |
| 1269 | } |
| 1270 | Decl *D = DE->getDecl(); |
| 1271 | VarDecl *VD = cast<VarDecl>(D); |
| 1272 | |
| 1273 | QualType Type = VD->getType(); |
| 1274 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 1275 | // It will be analyzed later. |
| 1276 | Vars.push_back(DE); |
| 1277 | continue; |
| 1278 | } |
| 1279 | |
| 1280 | // OpenMP [2.9.3.3, Restrictions, C/C++, p.3] |
| 1281 | // A variable that appears in a private clause must not have an incomplete |
| 1282 | // type or a reference type. |
| 1283 | if (RequireCompleteType(ELoc, Type, |
| 1284 | diag::err_omp_firstprivate_incomplete_type)) { |
| 1285 | continue; |
| 1286 | } |
| 1287 | if (Type->isReferenceType()) { |
| 1288 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1289 | << getOpenMPClauseName(OMPC_firstprivate) << Type; |
| 1290 | bool IsDecl = |
| 1291 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1292 | Diag(VD->getLocation(), |
| 1293 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1294 | << VD; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1295 | continue; |
| 1296 | } |
| 1297 | |
| 1298 | // OpenMP [2.9.3.4, Restrictions, C/C++, p.1] |
| 1299 | // A variable of class type (or array thereof) that appears in a private |
| 1300 | // clause requires an accesible, unambiguous copy constructor for the |
| 1301 | // class type. |
| 1302 | Type = Context.getBaseElementType(Type); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1303 | CXXRecordDecl *RD = getLangOpts().CPlusPlus |
| 1304 | ? Type.getNonReferenceType()->getAsCXXRecordDecl() |
| 1305 | : nullptr; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1306 | if (RD) { |
| 1307 | CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0); |
| 1308 | PartialDiagnostic PD = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1309 | PartialDiagnostic(PartialDiagnostic::NullDiagnostic()); |
| 1310 | if (!CD || CheckConstructorAccess( |
| 1311 | ELoc, CD, InitializedEntity::InitializeTemporary(Type), |
| 1312 | CD->getAccess(), PD) == AR_inaccessible || |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1313 | CD->isDeleted()) { |
| 1314 | Diag(ELoc, diag::err_omp_required_method) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1315 | << getOpenMPClauseName(OMPC_firstprivate) << 1; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1316 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 1317 | VarDecl::DeclarationOnly; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1318 | Diag(VD->getLocation(), |
| 1319 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1320 | << VD; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1321 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 1322 | continue; |
| 1323 | } |
| 1324 | MarkFunctionReferenced(ELoc, CD); |
| 1325 | DiagnoseUseOfDecl(CD, ELoc); |
| 1326 | |
| 1327 | CXXDestructorDecl *DD = RD->getDestructor(); |
| 1328 | if (DD) { |
| 1329 | if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible || |
| 1330 | DD->isDeleted()) { |
| 1331 | Diag(ELoc, diag::err_omp_required_method) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1332 | << getOpenMPClauseName(OMPC_firstprivate) << 4; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1333 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 1334 | VarDecl::DeclarationOnly; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1335 | Diag(VD->getLocation(), |
| 1336 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1337 | << VD; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1338 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 1339 | continue; |
| 1340 | } |
| 1341 | MarkFunctionReferenced(ELoc, DD); |
| 1342 | DiagnoseUseOfDecl(DD, ELoc); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | // If StartLoc and EndLoc are invalid - this is an implicit firstprivate |
| 1347 | // variable and it was checked already. |
| 1348 | if (StartLoc.isValid() && EndLoc.isValid()) { |
| 1349 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); |
| 1350 | Type = Type.getNonReferenceType().getCanonicalType(); |
| 1351 | bool IsConstant = Type.isConstant(Context); |
| 1352 | Type = Context.getBaseElementType(Type); |
| 1353 | // OpenMP [2.4.13, Data-sharing Attribute Clauses] |
| 1354 | // A list item that specifies a given variable may not appear in more |
| 1355 | // than one clause on the same directive, except that a variable may be |
| 1356 | // specified in both firstprivate and lastprivate clauses. |
| 1357 | // TODO: add processing for lastprivate. |
| 1358 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate && |
| 1359 | DVar.RefExpr) { |
| 1360 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1361 | << getOpenMPClauseName(DVar.CKind) |
| 1362 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1363 | Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1364 | << getOpenMPClauseName(DVar.CKind); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1365 | continue; |
| 1366 | } |
| 1367 | |
| 1368 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 1369 | // in a Construct] |
| 1370 | // Variables with the predetermined data-sharing attributes may not be |
| 1371 | // listed in data-sharing attributes clauses, except for the cases |
| 1372 | // listed below. For these exceptions only, listing a predetermined |
| 1373 | // variable in a data-sharing attribute clause is allowed and overrides |
| 1374 | // the variable's predetermined data-sharing attributes. |
| 1375 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 1376 | // in a Construct, C/C++, p.2] |
| 1377 | // Variables with const-qualified type having no mutable member may be |
| 1378 | // listed in a firstprivate clause, even if they are static data members. |
| 1379 | if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr && |
| 1380 | DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) { |
| 1381 | Diag(ELoc, diag::err_omp_wrong_dsa) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1382 | << getOpenMPClauseName(DVar.CKind) |
| 1383 | << getOpenMPClauseName(OMPC_firstprivate); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1384 | Diag(VD->getLocation(), diag::note_omp_predetermined_dsa) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1385 | << getOpenMPClauseName(DVar.CKind); |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1386 | continue; |
| 1387 | } |
| 1388 | |
| 1389 | // OpenMP [2.9.3.4, Restrictions, p.2] |
| 1390 | // A list item that is private within a parallel region must not appear |
| 1391 | // in a firstprivate clause on a worksharing construct if any of the |
| 1392 | // worksharing regions arising from the worksharing construct ever bind |
| 1393 | // to any of the parallel regions arising from the parallel construct. |
| 1394 | // OpenMP [2.9.3.4, Restrictions, p.3] |
| 1395 | // A list item that appears in a reduction clause of a parallel construct |
| 1396 | // must not appear in a firstprivate clause on a worksharing or task |
| 1397 | // construct if any of the worksharing or task regions arising from the |
| 1398 | // worksharing or task construct ever bind to any of the parallel regions |
| 1399 | // arising from the parallel construct. |
| 1400 | // OpenMP [2.9.3.4, Restrictions, p.4] |
| 1401 | // A list item that appears in a reduction clause in worksharing |
| 1402 | // construct must not appear in a firstprivate clause in a task construct |
| 1403 | // encountered during execution of any of the worksharing regions arising |
| 1404 | // from the worksharing construct. |
| 1405 | // TODO: |
| 1406 | } |
| 1407 | |
| 1408 | DSAStack->addDSA(VD, DE, OMPC_firstprivate); |
| 1409 | Vars.push_back(DE); |
| 1410 | } |
| 1411 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1412 | if (Vars.empty()) |
| 1413 | return nullptr; |
Alexey Bataev | d195bc3 | 2013-10-01 05:32:34 +0000 | [diff] [blame] | 1414 | |
| 1415 | return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, |
| 1416 | Vars); |
| 1417 | } |
| 1418 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1419 | OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList, |
| 1420 | SourceLocation StartLoc, |
| 1421 | SourceLocation LParenLoc, |
| 1422 | SourceLocation EndLoc) { |
| 1423 | SmallVector<Expr *, 8> Vars; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1424 | for (auto &RefExpr : VarList) { |
| 1425 | assert(RefExpr && "NULL expr in OpenMP shared clause."); |
| 1426 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1427 | // It will be analyzed later. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1428 | Vars.push_back(RefExpr); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1429 | continue; |
| 1430 | } |
| 1431 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1432 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1433 | // OpenMP [2.1, C/C++] |
| 1434 | // A list item is a variable name. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1435 | // OpenMP [2.14.3.2, Restrictions, p.1] |
| 1436 | // A variable that is part of another variable (as an array or structure |
| 1437 | // element) cannot appear in a shared unless it is a static data member |
| 1438 | // of a C++ class. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1439 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1440 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1441 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1442 | continue; |
| 1443 | } |
| 1444 | Decl *D = DE->getDecl(); |
| 1445 | VarDecl *VD = cast<VarDecl>(D); |
| 1446 | |
| 1447 | QualType Type = VD->getType(); |
| 1448 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 1449 | // It will be analyzed later. |
| 1450 | Vars.push_back(DE); |
| 1451 | continue; |
| 1452 | } |
| 1453 | |
| 1454 | // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced |
| 1455 | // in a Construct] |
| 1456 | // Variables with the predetermined data-sharing attributes may not be |
| 1457 | // listed in data-sharing attributes clauses, except for the cases |
| 1458 | // listed below. For these exceptions only, listing a predetermined |
| 1459 | // variable in a data-sharing attribute clause is allowed and overrides |
| 1460 | // the variable's predetermined data-sharing attributes. |
| 1461 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1462 | if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && |
| 1463 | DVar.RefExpr) { |
| 1464 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 1465 | << getOpenMPClauseName(OMPC_shared); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1466 | Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1467 | << getOpenMPClauseName(DVar.CKind); |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1468 | continue; |
| 1469 | } |
| 1470 | |
| 1471 | DSAStack->addDSA(VD, DE, OMPC_shared); |
| 1472 | Vars.push_back(DE); |
| 1473 | } |
| 1474 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1475 | if (Vars.empty()) |
| 1476 | return nullptr; |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1477 | |
| 1478 | return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 1479 | } |
| 1480 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1481 | OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step, |
| 1482 | SourceLocation StartLoc, |
| 1483 | SourceLocation LParenLoc, |
| 1484 | SourceLocation ColonLoc, |
| 1485 | SourceLocation EndLoc) { |
| 1486 | SmallVector<Expr *, 8> Vars; |
| 1487 | for (auto &RefExpr : VarList) { |
| 1488 | assert(RefExpr && "NULL expr in OpenMP linear clause."); |
| 1489 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
| 1490 | // It will be analyzed later. |
| 1491 | Vars.push_back(RefExpr); |
| 1492 | continue; |
| 1493 | } |
| 1494 | |
| 1495 | // OpenMP [2.14.3.7, linear clause] |
| 1496 | // A list item that appears in a linear clause is subject to the private |
| 1497 | // clause semantics described in Section 2.14.3.3 on page 159 except as |
| 1498 | // noted. In addition, the value of the new list item on each iteration |
| 1499 | // of the associated loop(s) corresponds to the value of the original |
| 1500 | // list item before entering the construct plus the logical number of |
| 1501 | // the iteration times linear-step. |
| 1502 | |
| 1503 | SourceLocation ELoc = RefExpr->getExprLoc(); |
| 1504 | // OpenMP [2.1, C/C++] |
| 1505 | // A list item is a variable name. |
| 1506 | // OpenMP [2.14.3.3, Restrictions, p.1] |
| 1507 | // A variable that is part of another variable (as an array or |
| 1508 | // structure element) cannot appear in a private clause. |
| 1509 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
| 1510 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
| 1511 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
| 1512 | continue; |
| 1513 | } |
| 1514 | |
| 1515 | VarDecl *VD = cast<VarDecl>(DE->getDecl()); |
| 1516 | |
| 1517 | // OpenMP [2.14.3.7, linear clause] |
| 1518 | // A list-item cannot appear in more than one linear clause. |
| 1519 | // A list-item that appears in a linear clause cannot appear in any |
| 1520 | // other data-sharing attribute clause. |
| 1521 | DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD); |
| 1522 | if (DVar.RefExpr) { |
| 1523 | Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind) |
| 1524 | << getOpenMPClauseName(OMPC_linear); |
| 1525 | Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa) |
| 1526 | << getOpenMPClauseName(DVar.CKind); |
| 1527 | continue; |
| 1528 | } |
| 1529 | |
| 1530 | QualType QType = VD->getType(); |
| 1531 | if (QType->isDependentType() || QType->isInstantiationDependentType()) { |
| 1532 | // It will be analyzed later. |
| 1533 | Vars.push_back(DE); |
| 1534 | continue; |
| 1535 | } |
| 1536 | |
| 1537 | // A variable must not have an incomplete type or a reference type. |
| 1538 | if (RequireCompleteType(ELoc, QType, |
| 1539 | diag::err_omp_linear_incomplete_type)) { |
| 1540 | continue; |
| 1541 | } |
| 1542 | if (QType->isReferenceType()) { |
| 1543 | Diag(ELoc, diag::err_omp_clause_ref_type_arg) |
| 1544 | << getOpenMPClauseName(OMPC_linear) << QType; |
| 1545 | bool IsDecl = |
| 1546 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1547 | Diag(VD->getLocation(), |
| 1548 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1549 | << VD; |
| 1550 | continue; |
| 1551 | } |
| 1552 | |
| 1553 | // A list item must not be const-qualified. |
| 1554 | if (QType.isConstant(Context)) { |
| 1555 | Diag(ELoc, diag::err_omp_const_variable) |
| 1556 | << getOpenMPClauseName(OMPC_linear); |
| 1557 | bool IsDecl = |
| 1558 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1559 | Diag(VD->getLocation(), |
| 1560 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1561 | << VD; |
| 1562 | continue; |
| 1563 | } |
| 1564 | |
| 1565 | // A list item must be of integral or pointer type. |
| 1566 | QType = QType.getUnqualifiedType().getCanonicalType(); |
| 1567 | const Type *Ty = QType.getTypePtrOrNull(); |
| 1568 | if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) && |
| 1569 | !Ty->isPointerType())) { |
| 1570 | Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType; |
| 1571 | bool IsDecl = |
| 1572 | VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly; |
| 1573 | Diag(VD->getLocation(), |
| 1574 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1575 | << VD; |
| 1576 | continue; |
| 1577 | } |
| 1578 | |
| 1579 | DSAStack->addDSA(VD, DE, OMPC_linear); |
| 1580 | Vars.push_back(DE); |
| 1581 | } |
| 1582 | |
| 1583 | if (Vars.empty()) |
| 1584 | return nullptr; |
| 1585 | |
| 1586 | Expr *StepExpr = Step; |
| 1587 | if (Step && !Step->isValueDependent() && !Step->isTypeDependent() && |
| 1588 | !Step->isInstantiationDependent() && |
| 1589 | !Step->containsUnexpandedParameterPack()) { |
| 1590 | SourceLocation StepLoc = Step->getLocStart(); |
| 1591 | ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step); |
| 1592 | if (Val.isInvalid()) |
| 1593 | return nullptr; |
| 1594 | StepExpr = Val.take(); |
| 1595 | |
| 1596 | // Warn about zero linear step (it would be probably better specified as |
| 1597 | // making corresponding variables 'const'). |
| 1598 | llvm::APSInt Result; |
| 1599 | if (StepExpr->isIntegerConstantExpr(Result, Context) && |
| 1600 | !Result.isNegative() && !Result.isStrictlyPositive()) |
| 1601 | Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0] |
| 1602 | << (Vars.size() > 1); |
| 1603 | } |
| 1604 | |
| 1605 | return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc, |
| 1606 | Vars, StepExpr); |
| 1607 | } |
| 1608 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1609 | OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList, |
| 1610 | SourceLocation StartLoc, |
| 1611 | SourceLocation LParenLoc, |
| 1612 | SourceLocation EndLoc) { |
| 1613 | SmallVector<Expr *, 8> Vars; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1614 | for (auto &RefExpr : VarList) { |
| 1615 | assert(RefExpr && "NULL expr in OpenMP copyin clause."); |
| 1616 | if (isa<DependentScopeDeclRefExpr>(RefExpr)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1617 | // It will be analyzed later. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1618 | Vars.push_back(RefExpr); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1619 | continue; |
| 1620 | } |
| 1621 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1622 | SourceLocation ELoc = RefExpr->getExprLoc(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1623 | // OpenMP [2.1, C/C++] |
| 1624 | // A list item is a variable name. |
| 1625 | // OpenMP [2.14.4.1, Restrictions, p.1] |
| 1626 | // A list item that appears in a copyin clause must be threadprivate. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1627 | DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1628 | if (!DE || !isa<VarDecl>(DE->getDecl())) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1629 | Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1630 | continue; |
| 1631 | } |
| 1632 | |
| 1633 | Decl *D = DE->getDecl(); |
| 1634 | VarDecl *VD = cast<VarDecl>(D); |
| 1635 | |
| 1636 | QualType Type = VD->getType(); |
| 1637 | if (Type->isDependentType() || Type->isInstantiationDependentType()) { |
| 1638 | // It will be analyzed later. |
| 1639 | Vars.push_back(DE); |
| 1640 | continue; |
| 1641 | } |
| 1642 | |
| 1643 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.1] |
| 1644 | // A list item that appears in a copyin clause must be threadprivate. |
| 1645 | if (!DSAStack->isThreadPrivate(VD)) { |
| 1646 | Diag(ELoc, diag::err_omp_required_access) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1647 | << getOpenMPClauseName(OMPC_copyin) |
| 1648 | << getOpenMPDirectiveName(OMPD_threadprivate); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1649 | continue; |
| 1650 | } |
| 1651 | |
| 1652 | // OpenMP [2.14.4.1, Restrictions, C/C++, p.2] |
| 1653 | // A variable of class type (or array thereof) that appears in a |
| 1654 | // copyin clause requires an accesible, unambiguous copy assignment |
| 1655 | // operator for the class type. |
| 1656 | Type = Context.getBaseElementType(Type); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1657 | CXXRecordDecl *RD = |
| 1658 | getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1659 | if (RD) { |
| 1660 | CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0); |
| 1661 | DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1662 | if (!MD || CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible || |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1663 | MD->isDeleted()) { |
| 1664 | Diag(ELoc, diag::err_omp_required_method) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1665 | << getOpenMPClauseName(OMPC_copyin) << 2; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1666 | bool IsDecl = VD->isThisDeclarationADefinition(Context) == |
| 1667 | VarDecl::DeclarationOnly; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1668 | Diag(VD->getLocation(), |
| 1669 | IsDecl ? diag::note_previous_decl : diag::note_defined_here) |
| 1670 | << VD; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1671 | Diag(RD->getLocation(), diag::note_previous_decl) << RD; |
| 1672 | continue; |
| 1673 | } |
| 1674 | MarkFunctionReferenced(ELoc, MD); |
| 1675 | DiagnoseUseOfDecl(MD, ELoc); |
| 1676 | } |
| 1677 | |
| 1678 | DSAStack->addDSA(VD, DE, OMPC_copyin); |
| 1679 | Vars.push_back(DE); |
| 1680 | } |
| 1681 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1682 | if (Vars.empty()) |
| 1683 | return nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1684 | |
| 1685 | return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars); |
| 1686 | } |
| 1687 | |
Alexey Bataev | 0c01835 | 2013-09-06 18:03:48 +0000 | [diff] [blame] | 1688 | #undef DSAStack |