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