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