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