blob: de3f860bdce45cfbd94eb4f851530ae9ddbae3da [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
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 Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "clang/Basic/OpenMPKinds.h"
23#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000025#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000028#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000029using namespace clang;
30
Alexey Bataev758e55e2013-09-06 18:03:48 +000031//===----------------------------------------------------------------------===//
32// Stack of data-sharing attributes for variables
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// \brief Default data sharing attributes, which can be applied to directive.
37enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000038 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 Bataev758e55e2013-09-06 18:03:48 +000041};
Alexey Bataev7ff55242014-06-19 09:13:45 +000042
Alexey Bataevf29276e2014-06-18 04:14:57 +000043template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000044 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000045 bool operator()(T Kind) {
46 for (auto KindEl : Arr)
47 if (KindEl == Kind)
48 return true;
49 return false;
50 }
51
52private:
53 ArrayRef<T> Arr;
54};
Alexey Bataev23b69422014-06-18 07:08:49 +000055struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000056 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000057 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000058};
59
60typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
61typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000062
63/// \brief Stack for tracking declarations used in OpenMP directives and
64/// clauses and their data-sharing attributes.
65class DSAStackTy {
66public:
67 struct DSAVarData {
68 OpenMPDirectiveKind DKind;
69 OpenMPClauseKind CKind;
70 DeclRefExpr *RefExpr;
Alexander Musmancb7f9c42014-05-15 13:04:49 +000071 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000072 };
Alexey Bataeved09d242014-05-28 05:53:51 +000073
Alexey Bataev758e55e2013-09-06 18:03:48 +000074private:
75 struct DSAInfo {
76 OpenMPClauseKind Attributes;
77 DeclRefExpr *RefExpr;
78 };
79 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000080 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000081
82 struct SharingMapTy {
83 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000084 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000085 DefaultDataSharingAttributes DefaultAttr;
86 OpenMPDirectiveKind Directive;
87 DeclarationNameInfo DirectiveName;
88 Scope *CurScope;
Alexey Bataeved09d242014-05-28 05:53:51 +000089 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 Scope *CurScope)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
92 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope) {
93 }
Alexey Bataev758e55e2013-09-06 18:03:48 +000094 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +000095 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
96 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000097 };
98
99 typedef SmallVector<SharingMapTy, 64> StackTy;
100
101 /// \brief Stack of used declaration and their data-sharing attributes.
102 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000103 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104
105 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
106
107 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000108
109 /// \brief Checks if the variable is a local for OpenMP region.
110 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000111
Alexey Bataev758e55e2013-09-06 18:03:48 +0000112public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000113 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114
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 Musmanf0d76e72014-05-29 14:36:25 +0000125 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000126 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000127 /// for diagnostics.
128 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
129
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130 /// \brief Adds explicit data sharing attribute to the specified declaration.
131 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
132
Alexey Bataev758e55e2013-09-06 18:03:48 +0000133 /// \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 Bataevf29276e2014-06-18 04:14:57 +0000138 /// \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 Bataevd5af8e42013-10-01 05:32:34 +0000150
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151 /// \brief Returns currently analyzed directive.
152 OpenMPDirectiveKind getCurrentDirective() const {
153 return Stack.back().Directive;
154 }
155
156 /// \brief Set default data sharing attribute to none.
157 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
158 /// \brief Set default data sharing attribute to shared.
159 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
160
161 DefaultDataSharingAttributes getDefaultDSA() const {
162 return Stack.back().DefaultAttr;
163 }
164
Alexey Bataevf29276e2014-06-18 04:14:57 +0000165 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000166 bool isThreadPrivate(VarDecl *D) {
167 DSAVarData DVar = getTopDSA(D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000168 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000169 }
170
171 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000172 Scope *getCurScope() { return Stack.back().CurScope; }
173};
Alexey Bataeved09d242014-05-28 05:53:51 +0000174} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175
176DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
177 VarDecl *D) {
178 DSAVarData DVar;
179 if (Iter == Stack.rend() - 1) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000180 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
181 // in a region but not in construct]
182 // File-scope or namespace-scope variables referenced in called routines
183 // in the region are shared unless they appear in a threadprivate
184 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000185 if (!D->isFunctionOrMethodVarDecl())
186 DVar.CKind = OMPC_shared;
187
188 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
189 // in a region but not in construct]
190 // Variables with static storage duration that are declared in called
191 // routines in the region are shared.
192 if (D->hasGlobalStorage())
193 DVar.CKind = OMPC_shared;
194
Alexey Bataev758e55e2013-09-06 18:03:48 +0000195 return DVar;
196 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000197
Alexey Bataev758e55e2013-09-06 18:03:48 +0000198 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000199 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
200 // in a Construct, C/C++, predetermined, p.1]
201 // Variables with automatic storage duration that are declared in a scope
202 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000203 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
204 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
205 DVar.CKind = OMPC_private;
206 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000207 }
208
Alexey Bataev758e55e2013-09-06 18:03:48 +0000209 // Explicitly specified attributes and local variables with predetermined
210 // attributes.
211 if (Iter->SharingMap.count(D)) {
212 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
213 DVar.CKind = Iter->SharingMap[D].Attributes;
214 return DVar;
215 }
216
217 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
218 // in a Construct, C/C++, implicitly determined, p.1]
219 // In a parallel or task construct, the data-sharing attributes of these
220 // variables are determined by the default clause, if present.
221 switch (Iter->DefaultAttr) {
222 case DSA_shared:
223 DVar.CKind = OMPC_shared;
224 return DVar;
225 case DSA_none:
226 return DVar;
227 case DSA_unspecified:
228 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
229 // in a Construct, implicitly determined, p.2]
230 // In a parallel construct, if no default clause is present, these
231 // variables are shared.
Alexey Bataevcefffae2014-06-23 08:21:53 +0000232 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000233 DVar.CKind = OMPC_shared;
234 return DVar;
235 }
236
237 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
238 // in a Construct, implicitly determined, p.4]
239 // In a task construct, if no default clause is present, a variable that in
240 // the enclosing context is determined to be shared by all implicit tasks
241 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000242 if (DVar.DKind == OMPD_task) {
243 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000244 for (StackTy::reverse_iterator I = std::next(Iter),
245 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000247 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
248 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000249 // in a Construct, implicitly determined, p.6]
250 // In a task construct, if no default clause is present, a variable
251 // whose data-sharing attribute is not determined by the rules above is
252 // firstprivate.
253 DVarTemp = getDSA(I, D);
254 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000255 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000256 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000257 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258 return DVar;
259 }
Alexey Bataevcefffae2014-06-23 08:21:53 +0000260 if (isOpenMPParallelDirective(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000261 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000262 }
263 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000265 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000266 return DVar;
267 }
268 }
269 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
270 // in a Construct, implicitly determined, p.3]
271 // For constructs other than task, if no default clause is present, these
272 // variables inherit their data-sharing attributes from the enclosing
273 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000274 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000275}
276
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000277DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
278 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
279 auto It = Stack.back().AlignedMap.find(D);
280 if (It == Stack.back().AlignedMap.end()) {
281 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
282 Stack.back().AlignedMap[D] = NewDE;
283 return nullptr;
284 } else {
285 assert(It->second && "Unexpected nullptr expr in the aligned map");
286 return It->second;
287 }
288 return nullptr;
289}
290
Alexey Bataev758e55e2013-09-06 18:03:48 +0000291void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
292 if (A == OMPC_threadprivate) {
293 Stack[0].SharingMap[D].Attributes = A;
294 Stack[0].SharingMap[D].RefExpr = E;
295 } else {
296 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
297 Stack.back().SharingMap[D].Attributes = A;
298 Stack.back().SharingMap[D].RefExpr = E;
299 }
300}
301
Alexey Bataeved09d242014-05-28 05:53:51 +0000302bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000303 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000304 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000305 Scope *TopScope = nullptr;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000306 while (I != E && !isOpenMPParallelDirective(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000307 ++I;
308 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000309 if (I == E)
310 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000311 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000312 Scope *CurScope = getCurScope();
313 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000314 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000315 }
316 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000317 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000318 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000319}
320
321DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
322 DSAVarData DVar;
323
324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
325 // in a Construct, C/C++, predetermined, p.1]
326 // Variables appearing in threadprivate directives are threadprivate.
327 if (D->getTLSKind() != VarDecl::TLS_None) {
328 DVar.CKind = OMPC_threadprivate;
329 return DVar;
330 }
331 if (Stack[0].SharingMap.count(D)) {
332 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
333 DVar.CKind = OMPC_threadprivate;
334 return DVar;
335 }
336
337 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
338 // in a Construct, C/C++, predetermined, p.1]
339 // Variables with automatic storage duration that are declared in a scope
340 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000341 OpenMPDirectiveKind Kind = getCurrentDirective();
Alexey Bataevcefffae2014-06-23 08:21:53 +0000342 if (!isOpenMPParallelDirective(Kind)) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000343 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000344 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000345 DVar.CKind = OMPC_private;
346 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000347 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000348 }
349
350 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
351 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000352 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000353 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000354 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000355 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ff55242014-06-19 09:13:45 +0000356 DSAVarData DVarTemp =
357 hasDSA(D, MatchesAnyClause(OMPC_firstprivate), MatchesAlways());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000358 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
359 return DVar;
360
Alexey Bataev758e55e2013-09-06 18:03:48 +0000361 DVar.CKind = OMPC_shared;
362 return DVar;
363 }
364
365 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000366 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000367 while (Type->isArrayType()) {
368 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
369 Type = ElemType.getNonReferenceType().getCanonicalType();
370 }
371 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
372 // in a Construct, C/C++, predetermined, p.6]
373 // Variables with const qualified type having no mutable member are
374 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000375 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000376 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000378 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000379 // Variables with const-qualified type having no mutable member may be
380 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ff55242014-06-19 09:13:45 +0000381 DSAVarData DVarTemp =
382 hasDSA(D, MatchesAnyClause(OMPC_firstprivate), MatchesAlways());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000383 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
384 return DVar;
385
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386 DVar.CKind = OMPC_shared;
387 return DVar;
388 }
389
390 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
391 // in a Construct, C/C++, predetermined, p.7]
392 // Variables with static storage duration that are declared in a scope
393 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000394 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000395 DVar.CKind = OMPC_shared;
396 return DVar;
397 }
398
399 // Explicitly specified attributes and local variables with predetermined
400 // attributes.
401 if (Stack.back().SharingMap.count(D)) {
402 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
403 DVar.CKind = Stack.back().SharingMap[D].Attributes;
404 }
405
406 return DVar;
407}
408
409DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000410 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000411}
412
Alexey Bataevf29276e2014-06-18 04:14:57 +0000413template <class ClausesPredicate, class DirectivesPredicate>
414DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
415 DirectivesPredicate DPred) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000416 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
417 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000418 I != E; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000419 if (!DPred(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000420 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000421 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000422 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000423 return DVar;
424 }
425 return DSAVarData();
426}
427
Alexey Bataevf29276e2014-06-18 04:14:57 +0000428template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataevc5e02582014-06-16 07:08:35 +0000429DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(VarDecl *D,
Alexey Bataevf29276e2014-06-18 04:14:57 +0000430 ClausesPredicate CPred,
431 DirectivesPredicate DPred) {
Alexey Bataevc5e02582014-06-16 07:08:35 +0000432 for (auto I = Stack.rbegin(), EE = std::prev(Stack.rend()); I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000433 if (!DPred(I->Directive))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000434 continue;
435 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000436 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000437 return DVar;
438 return DSAVarData();
439 }
440 return DSAVarData();
441}
442
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443void Sema::InitDataSharingAttributesStack() {
444 VarDataSharingAttributesStack = new DSAStackTy(*this);
445}
446
447#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
448
Alexey Bataeved09d242014-05-28 05:53:51 +0000449void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450
451void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
452 const DeclarationNameInfo &DirName,
453 Scope *CurScope) {
454 DSAStack->push(DKind, DirName, CurScope);
455 PushExpressionEvaluationContext(PotentiallyEvaluated);
456}
457
458void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000459 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
460 // A variable of class type (or array thereof) that appears in a lastprivate
461 // clause requires an accessible, unambiguous default constructor for the
462 // class type, unless the list item is also specified in a firstprivate
463 // clause.
464 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
465 for (auto C : D->clauses()) {
466 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
467 for (auto VarRef : Clause->varlists()) {
468 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
469 continue;
470 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
471 auto DVar = DSAStack->getTopDSA(VD);
472 if (DVar.CKind == OMPC_lastprivate) {
473 SourceLocation ELoc = VarRef->getExprLoc();
474 auto Type = VarRef->getType();
475 if (Type->isArrayType())
476 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
477 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000478 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
479 // FIXME This code must be replaced by actual constructing of the
480 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000481 if (RD) {
482 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
483 PartialDiagnostic PD =
484 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
485 if (!CD ||
486 CheckConstructorAccess(
487 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
488 CD->getAccess(), PD) == AR_inaccessible ||
489 CD->isDeleted()) {
490 Diag(ELoc, diag::err_omp_required_method)
491 << getOpenMPClauseName(OMPC_lastprivate) << 0;
492 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
493 VarDecl::DeclarationOnly;
494 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
495 : diag::note_defined_here)
496 << VD;
497 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
498 continue;
499 }
500 MarkFunctionReferenced(ELoc, CD);
501 DiagnoseUseOfDecl(CD, ELoc);
502 }
503 }
504 }
505 }
506 }
507 }
508
Alexey Bataev758e55e2013-09-06 18:03:48 +0000509 DSAStack->pop();
510 DiscardCleanupsInEvaluationContext();
511 PopExpressionEvaluationContext();
512}
513
Alexey Bataeva769e072013-03-22 06:34:35 +0000514namespace {
515
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000516class VarDeclFilterCCC : public CorrectionCandidateCallback {
517private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000518 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000519
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000520public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000521 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000522 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000523 NamedDecl *ND = Candidate.getCorrectionDecl();
524 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
525 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000526 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
527 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000528 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000529 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000530 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000531};
Alexey Bataeved09d242014-05-28 05:53:51 +0000532} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000533
534ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
535 CXXScopeSpec &ScopeSpec,
536 const DeclarationNameInfo &Id) {
537 LookupResult Lookup(*this, Id, LookupOrdinaryName);
538 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
539
540 if (Lookup.isAmbiguous())
541 return ExprError();
542
543 VarDecl *VD;
544 if (!Lookup.isSingleResult()) {
545 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000546 if (TypoCorrection Corrected =
547 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
548 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000549 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000550 PDiag(Lookup.empty()
551 ? diag::err_undeclared_var_use_suggest
552 : diag::err_omp_expected_var_arg_suggest)
553 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000554 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000555 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000556 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
557 : diag::err_omp_expected_var_arg)
558 << Id.getName();
559 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000560 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000561 } else {
562 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000563 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000564 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
565 return ExprError();
566 }
567 }
568 Lookup.suppressDiagnostics();
569
570 // OpenMP [2.9.2, Syntax, C/C++]
571 // Variables must be file-scope, namespace-scope, or static block-scope.
572 if (!VD->hasGlobalStorage()) {
573 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000574 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
575 bool IsDecl =
576 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000577 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000578 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
579 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000580 return ExprError();
581 }
582
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000583 VarDecl *CanonicalVD = VD->getCanonicalDecl();
584 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000585 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
586 // A threadprivate directive for file-scope variables must appear outside
587 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000588 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
589 !getCurLexicalContext()->isTranslationUnit()) {
590 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000591 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
592 bool IsDecl =
593 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
594 Diag(VD->getLocation(),
595 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
596 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000597 return ExprError();
598 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000599 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
600 // A threadprivate directive for static class member variables must appear
601 // in the class definition, in the same scope in which the member
602 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000603 if (CanonicalVD->isStaticDataMember() &&
604 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
605 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000606 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
607 bool IsDecl =
608 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
609 Diag(VD->getLocation(),
610 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
611 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000612 return ExprError();
613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
615 // A threadprivate directive for namespace-scope variables must appear
616 // outside any definition or declaration other than the namespace
617 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000618 if (CanonicalVD->getDeclContext()->isNamespace() &&
619 (!getCurLexicalContext()->isFileContext() ||
620 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
621 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000622 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
623 bool IsDecl =
624 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
625 Diag(VD->getLocation(),
626 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
627 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000628 return ExprError();
629 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000630 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
631 // A threadprivate directive for static block-scope variables must appear
632 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000633 if (CanonicalVD->isStaticLocal() && CurScope &&
634 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000635 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000636 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
637 bool IsDecl =
638 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
639 Diag(VD->getLocation(),
640 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
641 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000642 return ExprError();
643 }
644
645 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
646 // A threadprivate directive must lexically precede all references to any
647 // of the variables in its list.
648 if (VD->isUsed()) {
649 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000650 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000651 return ExprError();
652 }
653
654 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000655 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000656 return DE;
657}
658
Alexey Bataeved09d242014-05-28 05:53:51 +0000659Sema::DeclGroupPtrTy
660Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
661 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000662 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000663 CurContext->addDecl(D);
664 return DeclGroupPtrTy::make(DeclGroupRef(D));
665 }
666 return DeclGroupPtrTy();
667}
668
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000669namespace {
670class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
671 Sema &SemaRef;
672
673public:
674 bool VisitDeclRefExpr(const DeclRefExpr *E) {
675 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
676 if (VD->hasLocalStorage()) {
677 SemaRef.Diag(E->getLocStart(),
678 diag::err_omp_local_var_in_threadprivate_init)
679 << E->getSourceRange();
680 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
681 << VD << VD->getSourceRange();
682 return true;
683 }
684 }
685 return false;
686 }
687 bool VisitStmt(const Stmt *S) {
688 for (auto Child : S->children()) {
689 if (Child && Visit(Child))
690 return true;
691 }
692 return false;
693 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000694 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000695};
696} // namespace
697
Alexey Bataeved09d242014-05-28 05:53:51 +0000698OMPThreadPrivateDecl *
699Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000700 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000701 for (auto &RefExpr : VarList) {
702 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000703 VarDecl *VD = cast<VarDecl>(DE->getDecl());
704 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000705
706 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
707 // A threadprivate variable must not have an incomplete type.
708 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000709 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000710 continue;
711 }
712
713 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
714 // A threadprivate variable must not have a reference type.
715 if (VD->getType()->isReferenceType()) {
716 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000717 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
718 bool IsDecl =
719 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
720 Diag(VD->getLocation(),
721 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
722 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000723 continue;
724 }
725
Richard Smithfd3834f2013-04-13 02:43:54 +0000726 // Check if this is a TLS variable.
727 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000728 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000729 bool IsDecl =
730 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
731 Diag(VD->getLocation(),
732 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
733 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000734 continue;
735 }
736
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000737 // Check if initial value of threadprivate variable reference variable with
738 // local storage (it is not supported by runtime).
739 if (auto Init = VD->getAnyInitializer()) {
740 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000741 if (Checker.Visit(Init))
742 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000743 }
744
Alexey Bataeved09d242014-05-28 05:53:51 +0000745 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000746 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000747 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000748 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000749 if (!Vars.empty()) {
750 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
751 Vars);
752 D->setAccess(AS_public);
753 }
754 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000755}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000756
Alexey Bataev7ff55242014-06-19 09:13:45 +0000757static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
758 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
759 bool IsLoopIterVar = false) {
760 if (DVar.RefExpr) {
761 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
762 << getOpenMPClauseName(DVar.CKind);
763 return;
764 }
765 enum {
766 PDSA_StaticMemberShared,
767 PDSA_StaticLocalVarShared,
768 PDSA_LoopIterVarPrivate,
769 PDSA_LoopIterVarLinear,
770 PDSA_LoopIterVarLastprivate,
771 PDSA_ConstVarShared,
772 PDSA_GlobalVarShared,
773 PDSA_LocalVarPrivate
774 } Reason;
775 bool ReportHint = false;
776 if (IsLoopIterVar) {
777 if (DVar.CKind == OMPC_private)
778 Reason = PDSA_LoopIterVarPrivate;
779 else if (DVar.CKind == OMPC_lastprivate)
780 Reason = PDSA_LoopIterVarLastprivate;
781 else
782 Reason = PDSA_LoopIterVarLinear;
783 } else if (VD->isStaticLocal())
784 Reason = PDSA_StaticLocalVarShared;
785 else if (VD->isStaticDataMember())
786 Reason = PDSA_StaticMemberShared;
787 else if (VD->isFileVarDecl())
788 Reason = PDSA_GlobalVarShared;
789 else if (VD->getType().isConstant(SemaRef.getASTContext()))
790 Reason = PDSA_ConstVarShared;
791 else {
792 ReportHint = true;
793 Reason = PDSA_LocalVarPrivate;
794 }
795
796 SemaRef.Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
797 << Reason << ReportHint
798 << getOpenMPDirectiveName(Stack->getCurrentDirective());
799}
800
Alexey Bataev758e55e2013-09-06 18:03:48 +0000801namespace {
802class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
803 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000804 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000805 bool ErrorFound;
806 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000807 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataeved09d242014-05-28 05:53:51 +0000808
Alexey Bataev758e55e2013-09-06 18:03:48 +0000809public:
810 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000811 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000812 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000813 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
814 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000815
816 SourceLocation ELoc = E->getExprLoc();
817
818 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
819 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
820 if (DVar.CKind != OMPC_unknown) {
821 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000822 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000823 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000824 return;
825 }
826 // The default(none) clause requires that each variable that is referenced
827 // in the construct, and does not have a predetermined data-sharing
828 // attribute, must have its data-sharing attribute explicitly determined
829 // by being listed in a data-sharing attribute clause.
830 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataevf29276e2014-06-18 04:14:57 +0000831 (isOpenMPParallelDirective(DKind) || DKind == OMPD_task)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000832 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000833 SemaRef.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000834 return;
835 }
836
837 // OpenMP [2.9.3.6, Restrictions, p.2]
838 // A list item that appears in a reduction clause of the innermost
839 // enclosing worksharing or parallel construct may not be accessed in an
840 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000841 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev23b69422014-06-18 07:08:49 +0000842 MatchesAlways());
Alexey Bataevc5e02582014-06-16 07:08:35 +0000843 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
844 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000845 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
846 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000847 return;
848 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000849
850 // Define implicit data-sharing attributes for task.
851 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000852 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
853 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000854 }
855 }
856 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000857 for (auto C : S->clauses())
858 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000859 for (StmtRange R = C->children(); R; ++R)
860 if (Stmt *Child = *R)
861 Visit(Child);
862 }
863 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000864 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
865 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000866 if (Stmt *Child = *I)
867 if (!isa<OMPExecutableDirective>(Child))
868 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000869 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000870
871 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000872 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000873
Alexey Bataev7ff55242014-06-19 09:13:45 +0000874 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
875 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000876};
Alexey Bataeved09d242014-05-28 05:53:51 +0000877} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000878
Alexey Bataev9959db52014-05-06 10:08:46 +0000879void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
880 Scope *CurScope) {
881 switch (DKind) {
882 case OMPD_parallel: {
883 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
884 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
885 Sema::CapturedParamNameType Params[3] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000886 std::make_pair(".global_tid.", KmpInt32PtrTy),
887 std::make_pair(".bound_tid.", KmpInt32PtrTy),
888 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000889 };
890 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
891 break;
892 }
893 case OMPD_simd: {
894 Sema::CapturedParamNameType Params[1] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000895 std::make_pair(StringRef(), QualType()) // __context with shared vars
896 };
897 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
898 break;
899 }
900 case OMPD_for: {
901 Sema::CapturedParamNameType Params[1] = {
902 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000903 };
904 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
905 break;
906 }
907 case OMPD_threadprivate:
908 case OMPD_task:
909 llvm_unreachable("OpenMP Directive is not allowed");
910 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000911 llvm_unreachable("Unknown OpenMP directive");
912 }
913}
914
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000915StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
916 ArrayRef<OMPClause *> Clauses,
917 Stmt *AStmt,
918 SourceLocation StartLoc,
919 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000920 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
921
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000922 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000923
924 // Check default data sharing attributes for referenced variables.
925 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
926 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
927 if (DSAChecker.isErrorFound())
928 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000929 // Generate list of implicitly defined firstprivate variables.
930 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
931 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
932
933 bool ErrorFound = false;
934 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000935 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
936 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
937 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000938 ClausesWithImplicit.push_back(Implicit);
939 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +0000940 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000941 } else
942 ErrorFound = true;
943 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000944
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000945 switch (Kind) {
946 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +0000947 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
948 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000949 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000950 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +0000951 Res =
952 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000953 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +0000954 case OMPD_for:
955 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
956 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000957 case OMPD_threadprivate:
958 case OMPD_task:
959 llvm_unreachable("OpenMP Directive is not allowed");
960 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000961 llvm_unreachable("Unknown OpenMP directive");
962 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000963
Alexey Bataeved09d242014-05-28 05:53:51 +0000964 if (ErrorFound)
965 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000966 return Res;
967}
968
969StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
970 Stmt *AStmt,
971 SourceLocation StartLoc,
972 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000973 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
974 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
975 // 1.2.2 OpenMP Language Terminology
976 // Structured block - An executable statement with a single entry at the
977 // top and a single exit at the bottom.
978 // The point of exit cannot be a branch out of the structured block.
979 // longjmp() and throw() must not violate the entry/exit criteria.
980 CS->getCapturedDecl()->setNothrow();
981
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000982 getCurFunction()->setHasBranchProtectedScope();
983
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000984 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
985 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000986}
987
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000988namespace {
989/// \brief Helper class for checking canonical form of the OpenMP loops and
990/// extracting iteration space of each loop in the loop nest, that will be used
991/// for IR generation.
992class OpenMPIterationSpaceChecker {
993 /// \brief Reference to Sema.
994 Sema &SemaRef;
995 /// \brief A location for diagnostics (when there is no some better location).
996 SourceLocation DefaultLoc;
997 /// \brief A location for diagnostics (when increment is not compatible).
998 SourceLocation ConditionLoc;
999 /// \brief A source location for referring to condition later.
1000 SourceRange ConditionSrcRange;
1001 /// \brief Loop variable.
1002 VarDecl *Var;
1003 /// \brief Lower bound (initializer for the var).
1004 Expr *LB;
1005 /// \brief Upper bound.
1006 Expr *UB;
1007 /// \brief Loop step (increment).
1008 Expr *Step;
1009 /// \brief This flag is true when condition is one of:
1010 /// Var < UB
1011 /// Var <= UB
1012 /// UB > Var
1013 /// UB >= Var
1014 bool TestIsLessOp;
1015 /// \brief This flag is true when condition is strict ( < or > ).
1016 bool TestIsStrictOp;
1017 /// \brief This flag is true when step is subtracted on each iteration.
1018 bool SubtractStep;
1019
1020public:
1021 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1022 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1023 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1024 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1025 SubtractStep(false) {}
1026 /// \brief Check init-expr for canonical loop form and save loop counter
1027 /// variable - #Var and its initialization value - #LB.
1028 bool CheckInit(Stmt *S);
1029 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1030 /// for less/greater and for strict/non-strict comparison.
1031 bool CheckCond(Expr *S);
1032 /// \brief Check incr-expr for canonical loop form and return true if it
1033 /// does not conform, otherwise save loop step (#Step).
1034 bool CheckInc(Expr *S);
1035 /// \brief Return the loop counter variable.
1036 VarDecl *GetLoopVar() const { return Var; }
1037 /// \brief Return true if any expression is dependent.
1038 bool Dependent() const;
1039
1040private:
1041 /// \brief Check the right-hand side of an assignment in the increment
1042 /// expression.
1043 bool CheckIncRHS(Expr *RHS);
1044 /// \brief Helper to set loop counter variable and its initializer.
1045 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1046 /// \brief Helper to set upper bound.
1047 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1048 const SourceLocation &SL);
1049 /// \brief Helper to set loop increment.
1050 bool SetStep(Expr *NewStep, bool Subtract);
1051};
1052
1053bool OpenMPIterationSpaceChecker::Dependent() const {
1054 if (!Var) {
1055 assert(!LB && !UB && !Step);
1056 return false;
1057 }
1058 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1059 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1060}
1061
1062bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1063 // State consistency checking to ensure correct usage.
1064 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1065 !TestIsLessOp && !TestIsStrictOp);
1066 if (!NewVar || !NewLB)
1067 return true;
1068 Var = NewVar;
1069 LB = NewLB;
1070 return false;
1071}
1072
1073bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1074 const SourceRange &SR,
1075 const SourceLocation &SL) {
1076 // State consistency checking to ensure correct usage.
1077 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1078 !TestIsLessOp && !TestIsStrictOp);
1079 if (!NewUB)
1080 return true;
1081 UB = NewUB;
1082 TestIsLessOp = LessOp;
1083 TestIsStrictOp = StrictOp;
1084 ConditionSrcRange = SR;
1085 ConditionLoc = SL;
1086 return false;
1087}
1088
1089bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1090 // State consistency checking to ensure correct usage.
1091 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1092 if (!NewStep)
1093 return true;
1094 if (!NewStep->isValueDependent()) {
1095 // Check that the step is integer expression.
1096 SourceLocation StepLoc = NewStep->getLocStart();
1097 ExprResult Val =
1098 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1099 if (Val.isInvalid())
1100 return true;
1101 NewStep = Val.get();
1102
1103 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1104 // If test-expr is of form var relational-op b and relational-op is < or
1105 // <= then incr-expr must cause var to increase on each iteration of the
1106 // loop. If test-expr is of form var relational-op b and relational-op is
1107 // > or >= then incr-expr must cause var to decrease on each iteration of
1108 // the loop.
1109 // If test-expr is of form b relational-op var and relational-op is < or
1110 // <= then incr-expr must cause var to decrease on each iteration of the
1111 // loop. If test-expr is of form b relational-op var and relational-op is
1112 // > or >= then incr-expr must cause var to increase on each iteration of
1113 // the loop.
1114 llvm::APSInt Result;
1115 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1116 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1117 bool IsConstNeg =
1118 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1119 bool IsConstZero = IsConstant && !Result.getBoolValue();
1120 if (UB && (IsConstZero ||
1121 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1122 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1123 SemaRef.Diag(NewStep->getExprLoc(),
1124 diag::err_omp_loop_incr_not_compatible)
1125 << Var << TestIsLessOp << NewStep->getSourceRange();
1126 SemaRef.Diag(ConditionLoc,
1127 diag::note_omp_loop_cond_requres_compatible_incr)
1128 << TestIsLessOp << ConditionSrcRange;
1129 return true;
1130 }
1131 }
1132
1133 Step = NewStep;
1134 SubtractStep = Subtract;
1135 return false;
1136}
1137
1138bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1139 // Check init-expr for canonical loop form and save loop counter
1140 // variable - #Var and its initialization value - #LB.
1141 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1142 // var = lb
1143 // integer-type var = lb
1144 // random-access-iterator-type var = lb
1145 // pointer-type var = lb
1146 //
1147 if (!S) {
1148 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1149 return true;
1150 }
1151 if (Expr *E = dyn_cast<Expr>(S))
1152 S = E->IgnoreParens();
1153 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1154 if (BO->getOpcode() == BO_Assign)
1155 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1156 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1157 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1158 if (DS->isSingleDecl()) {
1159 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1160 if (Var->hasInit()) {
1161 // Accept non-canonical init form here but emit ext. warning.
1162 if (Var->getInitStyle() != VarDecl::CInit)
1163 SemaRef.Diag(S->getLocStart(),
1164 diag::ext_omp_loop_not_canonical_init)
1165 << S->getSourceRange();
1166 return SetVarAndLB(Var, Var->getInit());
1167 }
1168 }
1169 }
1170 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1171 if (CE->getOperator() == OO_Equal)
1172 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1173 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1174
1175 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1176 << S->getSourceRange();
1177 return true;
1178}
1179
Alexey Bataev23b69422014-06-18 07:08:49 +00001180/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001181/// variable (which may be the loop variable) if possible.
1182static const VarDecl *GetInitVarDecl(const Expr *E) {
1183 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001184 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001185 E = E->IgnoreParenImpCasts();
1186 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1187 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1188 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1189 CE->getArg(0) != nullptr)
1190 E = CE->getArg(0)->IgnoreParenImpCasts();
1191 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1192 if (!DRE)
1193 return nullptr;
1194 return dyn_cast<VarDecl>(DRE->getDecl());
1195}
1196
1197bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1198 // Check test-expr for canonical form, save upper-bound UB, flags for
1199 // less/greater and for strict/non-strict comparison.
1200 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1201 // var relational-op b
1202 // b relational-op var
1203 //
1204 if (!S) {
1205 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1206 return true;
1207 }
1208 S = S->IgnoreParenImpCasts();
1209 SourceLocation CondLoc = S->getLocStart();
1210 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1211 if (BO->isRelationalOp()) {
1212 if (GetInitVarDecl(BO->getLHS()) == Var)
1213 return SetUB(BO->getRHS(),
1214 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1215 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1216 BO->getSourceRange(), BO->getOperatorLoc());
1217 if (GetInitVarDecl(BO->getRHS()) == Var)
1218 return SetUB(BO->getLHS(),
1219 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1220 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1221 BO->getSourceRange(), BO->getOperatorLoc());
1222 }
1223 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1224 if (CE->getNumArgs() == 2) {
1225 auto Op = CE->getOperator();
1226 switch (Op) {
1227 case OO_Greater:
1228 case OO_GreaterEqual:
1229 case OO_Less:
1230 case OO_LessEqual:
1231 if (GetInitVarDecl(CE->getArg(0)) == Var)
1232 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1233 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1234 CE->getOperatorLoc());
1235 if (GetInitVarDecl(CE->getArg(1)) == Var)
1236 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1237 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1238 CE->getOperatorLoc());
1239 break;
1240 default:
1241 break;
1242 }
1243 }
1244 }
1245 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1246 << S->getSourceRange() << Var;
1247 return true;
1248}
1249
1250bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1251 // RHS of canonical loop form increment can be:
1252 // var + incr
1253 // incr + var
1254 // var - incr
1255 //
1256 RHS = RHS->IgnoreParenImpCasts();
1257 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1258 if (BO->isAdditiveOp()) {
1259 bool IsAdd = BO->getOpcode() == BO_Add;
1260 if (GetInitVarDecl(BO->getLHS()) == Var)
1261 return SetStep(BO->getRHS(), !IsAdd);
1262 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1263 return SetStep(BO->getLHS(), false);
1264 }
1265 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1266 bool IsAdd = CE->getOperator() == OO_Plus;
1267 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1268 if (GetInitVarDecl(CE->getArg(0)) == Var)
1269 return SetStep(CE->getArg(1), !IsAdd);
1270 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1271 return SetStep(CE->getArg(0), false);
1272 }
1273 }
1274 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1275 << RHS->getSourceRange() << Var;
1276 return true;
1277}
1278
1279bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1280 // Check incr-expr for canonical loop form and return true if it
1281 // does not conform.
1282 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1283 // ++var
1284 // var++
1285 // --var
1286 // var--
1287 // var += incr
1288 // var -= incr
1289 // var = var + incr
1290 // var = incr + var
1291 // var = var - incr
1292 //
1293 if (!S) {
1294 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1295 return true;
1296 }
1297 S = S->IgnoreParens();
1298 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1299 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1300 return SetStep(
1301 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1302 (UO->isDecrementOp() ? -1 : 1)).get(),
1303 false);
1304 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1305 switch (BO->getOpcode()) {
1306 case BO_AddAssign:
1307 case BO_SubAssign:
1308 if (GetInitVarDecl(BO->getLHS()) == Var)
1309 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1310 break;
1311 case BO_Assign:
1312 if (GetInitVarDecl(BO->getLHS()) == Var)
1313 return CheckIncRHS(BO->getRHS());
1314 break;
1315 default:
1316 break;
1317 }
1318 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1319 switch (CE->getOperator()) {
1320 case OO_PlusPlus:
1321 case OO_MinusMinus:
1322 if (GetInitVarDecl(CE->getArg(0)) == Var)
1323 return SetStep(
1324 SemaRef.ActOnIntegerConstant(
1325 CE->getLocStart(),
1326 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1327 false);
1328 break;
1329 case OO_PlusEqual:
1330 case OO_MinusEqual:
1331 if (GetInitVarDecl(CE->getArg(0)) == Var)
1332 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1333 break;
1334 case OO_Equal:
1335 if (GetInitVarDecl(CE->getArg(0)) == Var)
1336 return CheckIncRHS(CE->getArg(1));
1337 break;
1338 default:
1339 break;
1340 }
1341 }
1342 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1343 << S->getSourceRange() << Var;
1344 return true;
1345}
Alexey Bataev23b69422014-06-18 07:08:49 +00001346} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001347
1348/// \brief Called on a for stmt to check and extract its iteration space
1349/// for further processing (such as collapsing).
1350static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
1351 Sema &SemaRef, DSAStackTy &DSA) {
1352 // OpenMP [2.6, Canonical Loop Form]
1353 // for (init-expr; test-expr; incr-expr) structured-block
1354 auto For = dyn_cast_or_null<ForStmt>(S);
1355 if (!For) {
1356 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
1357 << getOpenMPDirectiveName(DKind);
1358 return true;
1359 }
1360 assert(For->getBody());
1361
1362 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1363
1364 // Check init.
1365 Stmt *Init = For->getInit();
1366 if (ISC.CheckInit(Init)) {
1367 return true;
1368 }
1369
1370 bool HasErrors = false;
1371
1372 // Check loop variable's type.
1373 VarDecl *Var = ISC.GetLoopVar();
1374
1375 // OpenMP [2.6, Canonical Loop Form]
1376 // Var is one of the following:
1377 // A variable of signed or unsigned integer type.
1378 // For C++, a variable of a random access iterator type.
1379 // For C, a variable of a pointer type.
1380 QualType VarType = Var->getType();
1381 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1382 !VarType->isPointerType() &&
1383 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1384 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1385 << SemaRef.getLangOpts().CPlusPlus;
1386 HasErrors = true;
1387 }
1388
1389 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1390 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001391 // The loop iteration variable in the associated for-loop of a simd construct
1392 // with just one associated for-loop may be listed in a linear clause with a
1393 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001394 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1395 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001396 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001397 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1398 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) ||
1399 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1400 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001401 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001402 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1403 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001404 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001405 HasErrors = true;
1406 } else {
1407 // Make the loop iteration variable private by default.
1408 DSA.addDSA(Var, nullptr, OMPC_private);
1409 }
1410
Alexey Bataev7ff55242014-06-19 09:13:45 +00001411 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001412
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001413 // Check test-expr.
1414 HasErrors |= ISC.CheckCond(For->getCond());
1415
1416 // Check incr-expr.
1417 HasErrors |= ISC.CheckInc(For->getInc());
1418
1419 if (ISC.Dependent())
1420 return HasErrors;
1421
1422 // FIXME: Build loop's iteration space representation.
1423 return HasErrors;
1424}
1425
1426/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1427/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1428/// to get the first for loop.
1429static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1430 if (IgnoreCaptured)
1431 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1432 S = CapS->getCapturedStmt();
1433 // OpenMP [2.8.1, simd construct, Restrictions]
1434 // All loops associated with the construct must be perfectly nested; that is,
1435 // there must be no intervening code nor any OpenMP directive between any two
1436 // loops.
1437 while (true) {
1438 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1439 S = AS->getSubStmt();
1440 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1441 if (CS->size() != 1)
1442 break;
1443 S = CS->body_back();
1444 } else
1445 break;
1446 }
1447 return S;
1448}
1449
1450/// \brief Called on a for stmt to check itself and nested loops (if any).
1451static bool CheckOpenMPLoop(OpenMPDirectiveKind DKind, unsigned NestedLoopCount,
1452 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA) {
1453 // This is helper routine for loop directives (e.g., 'for', 'simd',
1454 // 'for simd', etc.).
1455 assert(NestedLoopCount == 1);
1456 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1457 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
1458 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA))
1459 return true;
1460 // Move on to the next nested for loop, or to the loop body.
1461 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1462 }
1463
1464 // FIXME: Build resulting iteration space for IR generation (collapsing
1465 // iteration spaces when loop count > 1 ('collapse' clause)).
1466 return false;
1467}
1468
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001469StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001470 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001471 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001472 // In presence of clause 'collapse', it will define the nested loops number.
1473 // For now, pass default value of 1.
1474 if (CheckOpenMPLoop(OMPD_simd, 1, AStmt, *this, *DSAStack))
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001475 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001476
1477 getCurFunction()->setHasBranchProtectedScope();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001478 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001479}
1480
Alexey Bataevf29276e2014-06-18 04:14:57 +00001481StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses,
1482 Stmt *AStmt, SourceLocation StartLoc,
1483 SourceLocation EndLoc) {
1484 // In presence of clause 'collapse', it will define the nested loops number.
1485 // For now, pass default value of 1.
1486 if (CheckOpenMPLoop(OMPD_for, 1, AStmt, *this, *DSAStack))
1487 return StmtError();
1488
1489 getCurFunction()->setHasBranchProtectedScope();
1490 return OMPForDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1491}
1492
Alexey Bataeved09d242014-05-28 05:53:51 +00001493OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001494 SourceLocation StartLoc,
1495 SourceLocation LParenLoc,
1496 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001497 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001498 switch (Kind) {
1499 case OMPC_if:
1500 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1501 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001502 case OMPC_num_threads:
1503 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1504 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001505 case OMPC_safelen:
1506 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1507 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001508 case OMPC_collapse:
1509 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1510 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001511 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001512 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001513 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001514 case OMPC_private:
1515 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001516 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001517 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001518 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001519 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001520 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001521 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001522 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001523 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001524 case OMPC_threadprivate:
1525 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001526 llvm_unreachable("Clause is not allowed.");
1527 }
1528 return Res;
1529}
1530
Alexey Bataeved09d242014-05-28 05:53:51 +00001531OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001532 SourceLocation LParenLoc,
1533 SourceLocation EndLoc) {
1534 Expr *ValExpr = Condition;
1535 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1536 !Condition->isInstantiationDependent() &&
1537 !Condition->containsUnexpandedParameterPack()) {
1538 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001539 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001540 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001541 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001542
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001543 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001544 }
1545
1546 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1547}
1548
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001549ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1550 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001551 if (!Op)
1552 return ExprError();
1553
1554 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1555 public:
1556 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001557 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001558 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1559 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001560 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1561 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001562 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1563 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001564 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1565 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001566 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1567 QualType T,
1568 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001569 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1570 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001571 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1572 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001573 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001574 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001575 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001576 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1577 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001578 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1579 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001580 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1581 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001582 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001583 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001584 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001585 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1586 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001587 llvm_unreachable("conversion functions are permitted");
1588 }
1589 } ConvertDiagnoser;
1590 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1591}
1592
1593OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1594 SourceLocation StartLoc,
1595 SourceLocation LParenLoc,
1596 SourceLocation EndLoc) {
1597 Expr *ValExpr = NumThreads;
1598 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1599 !NumThreads->isInstantiationDependent() &&
1600 !NumThreads->containsUnexpandedParameterPack()) {
1601 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1602 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001603 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001604 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001605 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001606
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001607 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001608
1609 // OpenMP [2.5, Restrictions]
1610 // The num_threads expression must evaluate to a positive integer value.
1611 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001612 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1613 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001614 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1615 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001616 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001617 }
1618 }
1619
Alexey Bataeved09d242014-05-28 05:53:51 +00001620 return new (Context)
1621 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001622}
1623
Alexey Bataev62c87d22014-03-21 04:51:18 +00001624ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1625 OpenMPClauseKind CKind) {
1626 if (!E)
1627 return ExprError();
1628 if (E->isValueDependent() || E->isTypeDependent() ||
1629 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001630 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001631 llvm::APSInt Result;
1632 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1633 if (ICE.isInvalid())
1634 return ExprError();
1635 if (!Result.isStrictlyPositive()) {
1636 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1637 << getOpenMPClauseName(CKind) << E->getSourceRange();
1638 return ExprError();
1639 }
1640 return ICE;
1641}
1642
1643OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1644 SourceLocation LParenLoc,
1645 SourceLocation EndLoc) {
1646 // OpenMP [2.8.1, simd construct, Description]
1647 // The parameter of the safelen clause must be a constant
1648 // positive integer expression.
1649 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1650 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001651 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001652 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001653 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001654}
1655
Alexander Musman64d33f12014-06-04 07:53:32 +00001656OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1657 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001658 SourceLocation LParenLoc,
1659 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001660 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001661 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001662 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001663 // The parameter of the collapse clause must be a constant
1664 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001665 ExprResult NumForLoopsResult =
1666 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1667 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001668 return nullptr;
1669 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001670 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001671}
1672
Alexey Bataeved09d242014-05-28 05:53:51 +00001673OMPClause *Sema::ActOnOpenMPSimpleClause(
1674 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1675 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001676 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001677 switch (Kind) {
1678 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001679 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001680 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1681 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001682 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001683 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001684 Res = ActOnOpenMPProcBindClause(
1685 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1686 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001687 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001688 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001689 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001690 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001691 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001692 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001693 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001694 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001695 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001696 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001697 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001698 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001699 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001700 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001701 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001702 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001703 case OMPC_threadprivate:
1704 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001705 llvm_unreachable("Clause is not allowed.");
1706 }
1707 return Res;
1708}
1709
1710OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1711 SourceLocation KindKwLoc,
1712 SourceLocation StartLoc,
1713 SourceLocation LParenLoc,
1714 SourceLocation EndLoc) {
1715 if (Kind == OMPC_DEFAULT_unknown) {
1716 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001717 static_assert(OMPC_DEFAULT_unknown > 0,
1718 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001719 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001720 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001721 Values += "'";
1722 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1723 Values += "'";
1724 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001725 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001726 Values += " or ";
1727 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001728 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001729 break;
1730 default:
1731 Values += Sep;
1732 break;
1733 }
1734 }
1735 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001736 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001737 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001738 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001739 switch (Kind) {
1740 case OMPC_DEFAULT_none:
1741 DSAStack->setDefaultDSANone();
1742 break;
1743 case OMPC_DEFAULT_shared:
1744 DSAStack->setDefaultDSAShared();
1745 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001746 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001747 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001748 break;
1749 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001750 return new (Context)
1751 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001752}
1753
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001754OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1755 SourceLocation KindKwLoc,
1756 SourceLocation StartLoc,
1757 SourceLocation LParenLoc,
1758 SourceLocation EndLoc) {
1759 if (Kind == OMPC_PROC_BIND_unknown) {
1760 std::string Values;
1761 std::string Sep(", ");
1762 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1763 Values += "'";
1764 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1765 Values += "'";
1766 switch (i) {
1767 case OMPC_PROC_BIND_unknown - 2:
1768 Values += " or ";
1769 break;
1770 case OMPC_PROC_BIND_unknown - 1:
1771 break;
1772 default:
1773 Values += Sep;
1774 break;
1775 }
1776 }
1777 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001778 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001779 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001780 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001781 return new (Context)
1782 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001783}
1784
Alexey Bataev56dafe82014-06-20 07:16:17 +00001785OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1786 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
1787 SourceLocation StartLoc, SourceLocation LParenLoc,
1788 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
1789 SourceLocation EndLoc) {
1790 OMPClause *Res = nullptr;
1791 switch (Kind) {
1792 case OMPC_schedule:
1793 Res = ActOnOpenMPScheduleClause(
1794 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
1795 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
1796 break;
1797 case OMPC_if:
1798 case OMPC_num_threads:
1799 case OMPC_safelen:
1800 case OMPC_collapse:
1801 case OMPC_default:
1802 case OMPC_proc_bind:
1803 case OMPC_private:
1804 case OMPC_firstprivate:
1805 case OMPC_lastprivate:
1806 case OMPC_shared:
1807 case OMPC_reduction:
1808 case OMPC_linear:
1809 case OMPC_aligned:
1810 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001811 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001812 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001813 case OMPC_threadprivate:
1814 case OMPC_unknown:
1815 llvm_unreachable("Clause is not allowed.");
1816 }
1817 return Res;
1818}
1819
1820OMPClause *Sema::ActOnOpenMPScheduleClause(
1821 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
1822 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
1823 SourceLocation EndLoc) {
1824 if (Kind == OMPC_SCHEDULE_unknown) {
1825 std::string Values;
1826 std::string Sep(", ");
1827 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
1828 Values += "'";
1829 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
1830 Values += "'";
1831 switch (i) {
1832 case OMPC_SCHEDULE_unknown - 2:
1833 Values += " or ";
1834 break;
1835 case OMPC_SCHEDULE_unknown - 1:
1836 break;
1837 default:
1838 Values += Sep;
1839 break;
1840 }
1841 }
1842 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
1843 << Values << getOpenMPClauseName(OMPC_schedule);
1844 return nullptr;
1845 }
1846 Expr *ValExpr = ChunkSize;
1847 if (ChunkSize) {
1848 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
1849 !ChunkSize->isInstantiationDependent() &&
1850 !ChunkSize->containsUnexpandedParameterPack()) {
1851 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
1852 ExprResult Val =
1853 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
1854 if (Val.isInvalid())
1855 return nullptr;
1856
1857 ValExpr = Val.get();
1858
1859 // OpenMP [2.7.1, Restrictions]
1860 // chunk_size must be a loop invariant integer expression with a positive
1861 // value.
1862 llvm::APSInt Result;
1863 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
1864 Result.isSigned() && !Result.isStrictlyPositive()) {
1865 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
1866 << "schedule" << ChunkSize->getSourceRange();
1867 return nullptr;
1868 }
1869 }
1870 }
1871
1872 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
1873 EndLoc, Kind, ValExpr);
1874}
1875
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001876OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
1877 SourceLocation StartLoc,
1878 SourceLocation EndLoc) {
1879 OMPClause *Res = nullptr;
1880 switch (Kind) {
1881 case OMPC_ordered:
1882 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
1883 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001884 case OMPC_nowait:
1885 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
1886 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001887 case OMPC_if:
1888 case OMPC_num_threads:
1889 case OMPC_safelen:
1890 case OMPC_collapse:
1891 case OMPC_schedule:
1892 case OMPC_private:
1893 case OMPC_firstprivate:
1894 case OMPC_lastprivate:
1895 case OMPC_shared:
1896 case OMPC_reduction:
1897 case OMPC_linear:
1898 case OMPC_aligned:
1899 case OMPC_copyin:
1900 case OMPC_default:
1901 case OMPC_proc_bind:
1902 case OMPC_threadprivate:
1903 case OMPC_unknown:
1904 llvm_unreachable("Clause is not allowed.");
1905 }
1906 return Res;
1907}
1908
1909OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
1910 SourceLocation EndLoc) {
1911 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
1912}
1913
Alexey Bataev236070f2014-06-20 11:19:47 +00001914OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
1915 SourceLocation EndLoc) {
1916 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
1917}
1918
Alexey Bataevc5e02582014-06-16 07:08:35 +00001919OMPClause *Sema::ActOnOpenMPVarListClause(
1920 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
1921 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
1922 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
1923 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001924 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001925 switch (Kind) {
1926 case OMPC_private:
1927 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1928 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001929 case OMPC_firstprivate:
1930 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1931 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00001932 case OMPC_lastprivate:
1933 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1934 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001935 case OMPC_shared:
1936 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1937 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001938 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00001939 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
1940 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001941 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001942 case OMPC_linear:
1943 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1944 ColonLoc, EndLoc);
1945 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001946 case OMPC_aligned:
1947 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
1948 ColonLoc, EndLoc);
1949 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001950 case OMPC_copyin:
1951 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1952 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001953 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001954 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001955 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001956 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001957 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001958 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001959 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001960 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001961 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001962 case OMPC_threadprivate:
1963 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001964 llvm_unreachable("Clause is not allowed.");
1965 }
1966 return Res;
1967}
1968
1969OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1970 SourceLocation StartLoc,
1971 SourceLocation LParenLoc,
1972 SourceLocation EndLoc) {
1973 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001974 for (auto &RefExpr : VarList) {
1975 assert(RefExpr && "NULL expr in OpenMP private clause.");
1976 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001977 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001978 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001979 continue;
1980 }
1981
Alexey Bataeved09d242014-05-28 05:53:51 +00001982 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001983 // OpenMP [2.1, C/C++]
1984 // A list item is a variable name.
1985 // OpenMP [2.9.3.3, Restrictions, p.1]
1986 // A variable that is part of another variable (as an array or
1987 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001988 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001989 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001990 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001991 continue;
1992 }
1993 Decl *D = DE->getDecl();
1994 VarDecl *VD = cast<VarDecl>(D);
1995
1996 QualType Type = VD->getType();
1997 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1998 // It will be analyzed later.
1999 Vars.push_back(DE);
2000 continue;
2001 }
2002
2003 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2004 // A variable that appears in a private clause must not have an incomplete
2005 // type or a reference type.
2006 if (RequireCompleteType(ELoc, Type,
2007 diag::err_omp_private_incomplete_type)) {
2008 continue;
2009 }
2010 if (Type->isReferenceType()) {
2011 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002012 << getOpenMPClauseName(OMPC_private) << Type;
2013 bool IsDecl =
2014 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2015 Diag(VD->getLocation(),
2016 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2017 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002018 continue;
2019 }
2020
2021 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2022 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002023 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002024 // class type.
2025 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002026 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2027 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002028 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002029 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2030 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2031 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002032 // FIXME This code must be replaced by actual constructing/destructing of
2033 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002034 if (RD) {
2035 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2036 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002037 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002038 if (!CD ||
2039 CheckConstructorAccess(ELoc, CD,
2040 InitializedEntity::InitializeTemporary(Type),
2041 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002042 CD->isDeleted()) {
2043 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002044 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002045 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2046 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002047 Diag(VD->getLocation(),
2048 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2049 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002050 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2051 continue;
2052 }
2053 MarkFunctionReferenced(ELoc, CD);
2054 DiagnoseUseOfDecl(CD, ELoc);
2055
2056 CXXDestructorDecl *DD = RD->getDestructor();
2057 if (DD) {
2058 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2059 DD->isDeleted()) {
2060 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002061 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002062 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2063 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002064 Diag(VD->getLocation(),
2065 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2066 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002067 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2068 continue;
2069 }
2070 MarkFunctionReferenced(ELoc, DD);
2071 DiagnoseUseOfDecl(DD, ELoc);
2072 }
2073 }
2074
Alexey Bataev758e55e2013-09-06 18:03:48 +00002075 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2076 // in a Construct]
2077 // Variables with the predetermined data-sharing attributes may not be
2078 // listed in data-sharing attributes clauses, except for the cases
2079 // listed below. For these exceptions only, listing a predetermined
2080 // variable in a data-sharing attribute clause is allowed and overrides
2081 // the variable's predetermined data-sharing attributes.
2082 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2083 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002084 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2085 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002086 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002087 continue;
2088 }
2089
2090 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002091 Vars.push_back(DE);
2092 }
2093
Alexey Bataeved09d242014-05-28 05:53:51 +00002094 if (Vars.empty())
2095 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002096
2097 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2098}
2099
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002100OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2101 SourceLocation StartLoc,
2102 SourceLocation LParenLoc,
2103 SourceLocation EndLoc) {
2104 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002105 for (auto &RefExpr : VarList) {
2106 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2107 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002108 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002109 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002110 continue;
2111 }
2112
Alexey Bataeved09d242014-05-28 05:53:51 +00002113 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002114 // OpenMP [2.1, C/C++]
2115 // A list item is a variable name.
2116 // OpenMP [2.9.3.3, Restrictions, p.1]
2117 // A variable that is part of another variable (as an array or
2118 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002119 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002120 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002121 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002122 continue;
2123 }
2124 Decl *D = DE->getDecl();
2125 VarDecl *VD = cast<VarDecl>(D);
2126
2127 QualType Type = VD->getType();
2128 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2129 // It will be analyzed later.
2130 Vars.push_back(DE);
2131 continue;
2132 }
2133
2134 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2135 // A variable that appears in a private clause must not have an incomplete
2136 // type or a reference type.
2137 if (RequireCompleteType(ELoc, Type,
2138 diag::err_omp_firstprivate_incomplete_type)) {
2139 continue;
2140 }
2141 if (Type->isReferenceType()) {
2142 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002143 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2144 bool IsDecl =
2145 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2146 Diag(VD->getLocation(),
2147 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2148 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002149 continue;
2150 }
2151
2152 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2153 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002154 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002155 // class type.
2156 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002157 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2158 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2159 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002160 // FIXME This code must be replaced by actual constructing/destructing of
2161 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002162 if (RD) {
2163 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2164 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002165 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002166 if (!CD ||
2167 CheckConstructorAccess(ELoc, CD,
2168 InitializedEntity::InitializeTemporary(Type),
2169 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002170 CD->isDeleted()) {
2171 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002172 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002173 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2174 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002175 Diag(VD->getLocation(),
2176 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2177 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002178 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2179 continue;
2180 }
2181 MarkFunctionReferenced(ELoc, CD);
2182 DiagnoseUseOfDecl(CD, ELoc);
2183
2184 CXXDestructorDecl *DD = RD->getDestructor();
2185 if (DD) {
2186 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2187 DD->isDeleted()) {
2188 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002189 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002190 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2191 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002192 Diag(VD->getLocation(),
2193 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2194 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002195 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2196 continue;
2197 }
2198 MarkFunctionReferenced(ELoc, DD);
2199 DiagnoseUseOfDecl(DD, ELoc);
2200 }
2201 }
2202
2203 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2204 // variable and it was checked already.
2205 if (StartLoc.isValid() && EndLoc.isValid()) {
2206 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2207 Type = Type.getNonReferenceType().getCanonicalType();
2208 bool IsConstant = Type.isConstant(Context);
2209 Type = Context.getBaseElementType(Type);
2210 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2211 // A list item that specifies a given variable may not appear in more
2212 // than one clause on the same directive, except that a variable may be
2213 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002214 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002215 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002216 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002217 << getOpenMPClauseName(DVar.CKind)
2218 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002219 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002220 continue;
2221 }
2222
2223 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2224 // in a Construct]
2225 // Variables with the predetermined data-sharing attributes may not be
2226 // listed in data-sharing attributes clauses, except for the cases
2227 // listed below. For these exceptions only, listing a predetermined
2228 // variable in a data-sharing attribute clause is allowed and overrides
2229 // the variable's predetermined data-sharing attributes.
2230 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2231 // in a Construct, C/C++, p.2]
2232 // Variables with const-qualified type having no mutable member may be
2233 // listed in a firstprivate clause, even if they are static data members.
2234 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2235 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2236 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002237 << getOpenMPClauseName(DVar.CKind)
2238 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002239 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002240 continue;
2241 }
2242
Alexey Bataevf29276e2014-06-18 04:14:57 +00002243 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002244 // OpenMP [2.9.3.4, Restrictions, p.2]
2245 // A list item that is private within a parallel region must not appear
2246 // in a firstprivate clause on a worksharing construct if any of the
2247 // worksharing regions arising from the worksharing construct ever bind
2248 // to any of the parallel regions arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002249 if (isOpenMPWorksharingDirective(CurrDir)) {
2250 DVar = DSAStack->getImplicitDSA(VD);
2251 if (DVar.CKind != OMPC_shared) {
2252 Diag(ELoc, diag::err_omp_required_access)
2253 << getOpenMPClauseName(OMPC_firstprivate)
2254 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002255 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002256 continue;
2257 }
2258 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002259 // OpenMP [2.9.3.4, Restrictions, p.3]
2260 // A list item that appears in a reduction clause of a parallel construct
2261 // must not appear in a firstprivate clause on a worksharing or task
2262 // construct if any of the worksharing or task regions arising from the
2263 // worksharing or task construct ever bind to any of the parallel regions
2264 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002265 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002266 // OpenMP [2.9.3.4, Restrictions, p.4]
2267 // A list item that appears in a reduction clause in worksharing
2268 // construct must not appear in a firstprivate clause in a task construct
2269 // encountered during execution of any of the worksharing regions arising
2270 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002271 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002272 }
2273
2274 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2275 Vars.push_back(DE);
2276 }
2277
Alexey Bataeved09d242014-05-28 05:53:51 +00002278 if (Vars.empty())
2279 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002280
2281 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2282 Vars);
2283}
2284
Alexander Musman1bb328c2014-06-04 13:06:39 +00002285OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2286 SourceLocation StartLoc,
2287 SourceLocation LParenLoc,
2288 SourceLocation EndLoc) {
2289 SmallVector<Expr *, 8> Vars;
2290 for (auto &RefExpr : VarList) {
2291 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2292 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2293 // It will be analyzed later.
2294 Vars.push_back(RefExpr);
2295 continue;
2296 }
2297
2298 SourceLocation ELoc = RefExpr->getExprLoc();
2299 // OpenMP [2.1, C/C++]
2300 // A list item is a variable name.
2301 // OpenMP [2.14.3.5, Restrictions, p.1]
2302 // A variable that is part of another variable (as an array or structure
2303 // element) cannot appear in a lastprivate clause.
2304 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2305 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2306 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2307 continue;
2308 }
2309 Decl *D = DE->getDecl();
2310 VarDecl *VD = cast<VarDecl>(D);
2311
2312 QualType Type = VD->getType();
2313 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2314 // It will be analyzed later.
2315 Vars.push_back(DE);
2316 continue;
2317 }
2318
2319 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2320 // A variable that appears in a lastprivate clause must not have an
2321 // incomplete type or a reference type.
2322 if (RequireCompleteType(ELoc, Type,
2323 diag::err_omp_lastprivate_incomplete_type)) {
2324 continue;
2325 }
2326 if (Type->isReferenceType()) {
2327 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2328 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2329 bool IsDecl =
2330 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2331 Diag(VD->getLocation(),
2332 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2333 << VD;
2334 continue;
2335 }
2336
2337 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2338 // in a Construct]
2339 // Variables with the predetermined data-sharing attributes may not be
2340 // listed in data-sharing attributes clauses, except for the cases
2341 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002342 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2343 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2344 DVar.CKind != OMPC_firstprivate &&
2345 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2346 Diag(ELoc, diag::err_omp_wrong_dsa)
2347 << getOpenMPClauseName(DVar.CKind)
2348 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002349 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002350 continue;
2351 }
2352
Alexey Bataevf29276e2014-06-18 04:14:57 +00002353 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2354 // OpenMP [2.14.3.5, Restrictions, p.2]
2355 // A list item that is private within a parallel region, or that appears in
2356 // the reduction clause of a parallel construct, must not appear in a
2357 // lastprivate clause on a worksharing construct if any of the corresponding
2358 // worksharing regions ever binds to any of the corresponding parallel
2359 // regions.
2360 if (isOpenMPWorksharingDirective(CurrDir)) {
2361 DVar = DSAStack->getImplicitDSA(VD);
2362 if (DVar.CKind != OMPC_shared) {
2363 Diag(ELoc, diag::err_omp_required_access)
2364 << getOpenMPClauseName(OMPC_lastprivate)
2365 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002366 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002367 continue;
2368 }
2369 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002370 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002371 // A variable of class type (or array thereof) that appears in a
2372 // lastprivate clause requires an accessible, unambiguous default
2373 // constructor for the class type, unless the list item is also specified
2374 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002375 // A variable of class type (or array thereof) that appears in a
2376 // lastprivate clause requires an accessible, unambiguous copy assignment
2377 // operator for the class type.
2378 while (Type.getNonReferenceType()->isArrayType())
2379 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2380 ->getElementType();
2381 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2382 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2383 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002384 // FIXME This code must be replaced by actual copying and destructing of the
2385 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002386 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002387 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2388 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002389 if (MD) {
2390 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2391 MD->isDeleted()) {
2392 Diag(ELoc, diag::err_omp_required_method)
2393 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2394 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2395 VarDecl::DeclarationOnly;
2396 Diag(VD->getLocation(),
2397 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2398 << VD;
2399 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2400 continue;
2401 }
2402 MarkFunctionReferenced(ELoc, MD);
2403 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002404 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002405
2406 CXXDestructorDecl *DD = RD->getDestructor();
2407 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002408 PartialDiagnostic PD =
2409 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002410 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2411 DD->isDeleted()) {
2412 Diag(ELoc, diag::err_omp_required_method)
2413 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2414 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2415 VarDecl::DeclarationOnly;
2416 Diag(VD->getLocation(),
2417 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2418 << VD;
2419 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2420 continue;
2421 }
2422 MarkFunctionReferenced(ELoc, DD);
2423 DiagnoseUseOfDecl(DD, ELoc);
2424 }
2425 }
2426
Alexey Bataevf29276e2014-06-18 04:14:57 +00002427 if (DVar.CKind != OMPC_firstprivate)
2428 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002429 Vars.push_back(DE);
2430 }
2431
2432 if (Vars.empty())
2433 return nullptr;
2434
2435 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2436 Vars);
2437}
2438
Alexey Bataev758e55e2013-09-06 18:03:48 +00002439OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2440 SourceLocation StartLoc,
2441 SourceLocation LParenLoc,
2442 SourceLocation EndLoc) {
2443 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002444 for (auto &RefExpr : VarList) {
2445 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2446 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002447 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002448 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002449 continue;
2450 }
2451
Alexey Bataeved09d242014-05-28 05:53:51 +00002452 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002453 // OpenMP [2.1, C/C++]
2454 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002455 // OpenMP [2.14.3.2, Restrictions, p.1]
2456 // A variable that is part of another variable (as an array or structure
2457 // element) cannot appear in a shared unless it is a static data member
2458 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002459 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002460 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002461 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002462 continue;
2463 }
2464 Decl *D = DE->getDecl();
2465 VarDecl *VD = cast<VarDecl>(D);
2466
2467 QualType Type = VD->getType();
2468 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2469 // It will be analyzed later.
2470 Vars.push_back(DE);
2471 continue;
2472 }
2473
2474 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2475 // in a Construct]
2476 // Variables with the predetermined data-sharing attributes may not be
2477 // listed in data-sharing attributes clauses, except for the cases
2478 // listed below. For these exceptions only, listing a predetermined
2479 // variable in a data-sharing attribute clause is allowed and overrides
2480 // the variable's predetermined data-sharing attributes.
2481 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002482 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2483 DVar.RefExpr) {
2484 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2485 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002486 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002487 continue;
2488 }
2489
2490 DSAStack->addDSA(VD, DE, OMPC_shared);
2491 Vars.push_back(DE);
2492 }
2493
Alexey Bataeved09d242014-05-28 05:53:51 +00002494 if (Vars.empty())
2495 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002496
2497 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2498}
2499
Alexey Bataevc5e02582014-06-16 07:08:35 +00002500namespace {
2501class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2502 DSAStackTy *Stack;
2503
2504public:
2505 bool VisitDeclRefExpr(DeclRefExpr *E) {
2506 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2507 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2508 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2509 return false;
2510 if (DVar.CKind != OMPC_unknown)
2511 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002512 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002513 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002514 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002515 return true;
2516 return false;
2517 }
2518 return false;
2519 }
2520 bool VisitStmt(Stmt *S) {
2521 for (auto Child : S->children()) {
2522 if (Child && Visit(Child))
2523 return true;
2524 }
2525 return false;
2526 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002527 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002528};
Alexey Bataev23b69422014-06-18 07:08:49 +00002529} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002530
2531OMPClause *Sema::ActOnOpenMPReductionClause(
2532 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2533 SourceLocation ColonLoc, SourceLocation EndLoc,
2534 CXXScopeSpec &ReductionIdScopeSpec,
2535 const DeclarationNameInfo &ReductionId) {
2536 // TODO: Allow scope specification search when 'declare reduction' is
2537 // supported.
2538 assert(ReductionIdScopeSpec.isEmpty() &&
2539 "No support for scoped reduction identifiers yet.");
2540
2541 auto DN = ReductionId.getName();
2542 auto OOK = DN.getCXXOverloadedOperator();
2543 BinaryOperatorKind BOK = BO_Comma;
2544
2545 // OpenMP [2.14.3.6, reduction clause]
2546 // C
2547 // reduction-identifier is either an identifier or one of the following
2548 // operators: +, -, *, &, |, ^, && and ||
2549 // C++
2550 // reduction-identifier is either an id-expression or one of the following
2551 // operators: +, -, *, &, |, ^, && and ||
2552 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2553 switch (OOK) {
2554 case OO_Plus:
2555 case OO_Minus:
2556 BOK = BO_AddAssign;
2557 break;
2558 case OO_Star:
2559 BOK = BO_MulAssign;
2560 break;
2561 case OO_Amp:
2562 BOK = BO_AndAssign;
2563 break;
2564 case OO_Pipe:
2565 BOK = BO_OrAssign;
2566 break;
2567 case OO_Caret:
2568 BOK = BO_XorAssign;
2569 break;
2570 case OO_AmpAmp:
2571 BOK = BO_LAnd;
2572 break;
2573 case OO_PipePipe:
2574 BOK = BO_LOr;
2575 break;
2576 default:
2577 if (auto II = DN.getAsIdentifierInfo()) {
2578 if (II->isStr("max"))
2579 BOK = BO_GT;
2580 else if (II->isStr("min"))
2581 BOK = BO_LT;
2582 }
2583 break;
2584 }
2585 SourceRange ReductionIdRange;
2586 if (ReductionIdScopeSpec.isValid()) {
2587 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2588 }
2589 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2590 if (BOK == BO_Comma) {
2591 // Not allowed reduction identifier is found.
2592 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2593 << ReductionIdRange;
2594 return nullptr;
2595 }
2596
2597 SmallVector<Expr *, 8> Vars;
2598 for (auto RefExpr : VarList) {
2599 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2600 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2601 // It will be analyzed later.
2602 Vars.push_back(RefExpr);
2603 continue;
2604 }
2605
2606 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2607 RefExpr->isInstantiationDependent() ||
2608 RefExpr->containsUnexpandedParameterPack()) {
2609 // It will be analyzed later.
2610 Vars.push_back(RefExpr);
2611 continue;
2612 }
2613
2614 auto ELoc = RefExpr->getExprLoc();
2615 auto ERange = RefExpr->getSourceRange();
2616 // OpenMP [2.1, C/C++]
2617 // A list item is a variable or array section, subject to the restrictions
2618 // specified in Section 2.4 on page 42 and in each of the sections
2619 // describing clauses and directives for which a list appears.
2620 // OpenMP [2.14.3.3, Restrictions, p.1]
2621 // A variable that is part of another variable (as an array or
2622 // structure element) cannot appear in a private clause.
2623 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2624 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2625 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2626 continue;
2627 }
2628 auto D = DE->getDecl();
2629 auto VD = cast<VarDecl>(D);
2630 auto Type = VD->getType();
2631 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2632 // A variable that appears in a private clause must not have an incomplete
2633 // type or a reference type.
2634 if (RequireCompleteType(ELoc, Type,
2635 diag::err_omp_reduction_incomplete_type))
2636 continue;
2637 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2638 // Arrays may not appear in a reduction clause.
2639 if (Type.getNonReferenceType()->isArrayType()) {
2640 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2641 bool IsDecl =
2642 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2643 Diag(VD->getLocation(),
2644 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2645 << VD;
2646 continue;
2647 }
2648 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2649 // A list item that appears in a reduction clause must not be
2650 // const-qualified.
2651 if (Type.getNonReferenceType().isConstant(Context)) {
2652 Diag(ELoc, diag::err_omp_const_variable)
2653 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2654 bool IsDecl =
2655 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2656 Diag(VD->getLocation(),
2657 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2658 << VD;
2659 continue;
2660 }
2661 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2662 // If a list-item is a reference type then it must bind to the same object
2663 // for all threads of the team.
2664 VarDecl *VDDef = VD->getDefinition();
2665 if (Type->isReferenceType() && VDDef) {
2666 DSARefChecker Check(DSAStack);
2667 if (Check.Visit(VDDef->getInit())) {
2668 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2669 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2670 continue;
2671 }
2672 }
2673 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2674 // The type of a list item that appears in a reduction clause must be valid
2675 // for the reduction-identifier. For a max or min reduction in C, the type
2676 // of the list item must be an allowed arithmetic data type: char, int,
2677 // float, double, or _Bool, possibly modified with long, short, signed, or
2678 // unsigned. For a max or min reduction in C++, the type of the list item
2679 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2680 // double, or bool, possibly modified with long, short, signed, or unsigned.
2681 if ((BOK == BO_GT || BOK == BO_LT) &&
2682 !(Type->isScalarType() ||
2683 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2684 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2685 << getLangOpts().CPlusPlus;
2686 bool IsDecl =
2687 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2688 Diag(VD->getLocation(),
2689 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2690 << VD;
2691 continue;
2692 }
2693 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2694 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2695 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2696 bool IsDecl =
2697 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2698 Diag(VD->getLocation(),
2699 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2700 << VD;
2701 continue;
2702 }
2703 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2704 getDiagnostics().setSuppressAllDiagnostics(true);
2705 ExprResult ReductionOp =
2706 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2707 RefExpr, RefExpr);
2708 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2709 if (ReductionOp.isInvalid()) {
2710 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002711 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002712 bool IsDecl =
2713 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2714 Diag(VD->getLocation(),
2715 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2716 << VD;
2717 continue;
2718 }
2719
2720 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2721 // in a Construct]
2722 // Variables with the predetermined data-sharing attributes may not be
2723 // listed in data-sharing attributes clauses, except for the cases
2724 // listed below. For these exceptions only, listing a predetermined
2725 // variable in a data-sharing attribute clause is allowed and overrides
2726 // the variable's predetermined data-sharing attributes.
2727 // OpenMP [2.14.3.6, Restrictions, p.3]
2728 // Any number of reduction clauses can be specified on the directive,
2729 // but a list item can appear only once in the reduction clauses for that
2730 // directive.
2731 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2732 if (DVar.CKind == OMPC_reduction) {
2733 Diag(ELoc, diag::err_omp_once_referenced)
2734 << getOpenMPClauseName(OMPC_reduction);
2735 if (DVar.RefExpr) {
2736 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2737 }
2738 } else if (DVar.CKind != OMPC_unknown) {
2739 Diag(ELoc, diag::err_omp_wrong_dsa)
2740 << getOpenMPClauseName(DVar.CKind)
2741 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002742 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002743 continue;
2744 }
2745
2746 // OpenMP [2.14.3.6, Restrictions, p.1]
2747 // A list item that appears in a reduction clause of a worksharing
2748 // construct must be shared in the parallel regions to which any of the
2749 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002750 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2751 if (isOpenMPWorksharingDirective(CurrDir)) {
2752 DVar = DSAStack->getImplicitDSA(VD);
2753 if (DVar.CKind != OMPC_shared) {
2754 Diag(ELoc, diag::err_omp_required_access)
2755 << getOpenMPClauseName(OMPC_reduction)
2756 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002757 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002758 continue;
2759 }
2760 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00002761
2762 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2763 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2764 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002765 // FIXME This code must be replaced by actual constructing/destructing of
2766 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00002767 if (RD) {
2768 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2769 PartialDiagnostic PD =
2770 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00002771 if (!CD ||
2772 CheckConstructorAccess(ELoc, CD,
2773 InitializedEntity::InitializeTemporary(Type),
2774 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00002775 CD->isDeleted()) {
2776 Diag(ELoc, diag::err_omp_required_method)
2777 << getOpenMPClauseName(OMPC_reduction) << 0;
2778 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2779 VarDecl::DeclarationOnly;
2780 Diag(VD->getLocation(),
2781 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2782 << VD;
2783 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2784 continue;
2785 }
2786 MarkFunctionReferenced(ELoc, CD);
2787 DiagnoseUseOfDecl(CD, ELoc);
2788
2789 CXXDestructorDecl *DD = RD->getDestructor();
2790 if (DD) {
2791 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2792 DD->isDeleted()) {
2793 Diag(ELoc, diag::err_omp_required_method)
2794 << getOpenMPClauseName(OMPC_reduction) << 4;
2795 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2796 VarDecl::DeclarationOnly;
2797 Diag(VD->getLocation(),
2798 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2799 << VD;
2800 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2801 continue;
2802 }
2803 MarkFunctionReferenced(ELoc, DD);
2804 DiagnoseUseOfDecl(DD, ELoc);
2805 }
2806 }
2807
2808 DSAStack->addDSA(VD, DE, OMPC_reduction);
2809 Vars.push_back(DE);
2810 }
2811
2812 if (Vars.empty())
2813 return nullptr;
2814
2815 return OMPReductionClause::Create(
2816 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
2817 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
2818}
2819
Alexander Musman8dba6642014-04-22 13:09:42 +00002820OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2821 SourceLocation StartLoc,
2822 SourceLocation LParenLoc,
2823 SourceLocation ColonLoc,
2824 SourceLocation EndLoc) {
2825 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002826 for (auto &RefExpr : VarList) {
2827 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2828 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002829 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002830 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002831 continue;
2832 }
2833
2834 // OpenMP [2.14.3.7, linear clause]
2835 // A list item that appears in a linear clause is subject to the private
2836 // clause semantics described in Section 2.14.3.3 on page 159 except as
2837 // noted. In addition, the value of the new list item on each iteration
2838 // of the associated loop(s) corresponds to the value of the original
2839 // list item before entering the construct plus the logical number of
2840 // the iteration times linear-step.
2841
Alexey Bataeved09d242014-05-28 05:53:51 +00002842 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00002843 // OpenMP [2.1, C/C++]
2844 // A list item is a variable name.
2845 // OpenMP [2.14.3.3, Restrictions, p.1]
2846 // A variable that is part of another variable (as an array or
2847 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002848 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002849 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002850 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00002851 continue;
2852 }
2853
2854 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2855
2856 // OpenMP [2.14.3.7, linear clause]
2857 // A list-item cannot appear in more than one linear clause.
2858 // A list-item that appears in a linear clause cannot appear in any
2859 // other data-sharing attribute clause.
2860 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2861 if (DVar.RefExpr) {
2862 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2863 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002864 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00002865 continue;
2866 }
2867
2868 QualType QType = VD->getType();
2869 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2870 // It will be analyzed later.
2871 Vars.push_back(DE);
2872 continue;
2873 }
2874
2875 // A variable must not have an incomplete type or a reference type.
2876 if (RequireCompleteType(ELoc, QType,
2877 diag::err_omp_linear_incomplete_type)) {
2878 continue;
2879 }
2880 if (QType->isReferenceType()) {
2881 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2882 << getOpenMPClauseName(OMPC_linear) << QType;
2883 bool IsDecl =
2884 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2885 Diag(VD->getLocation(),
2886 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2887 << VD;
2888 continue;
2889 }
2890
2891 // A list item must not be const-qualified.
2892 if (QType.isConstant(Context)) {
2893 Diag(ELoc, diag::err_omp_const_variable)
2894 << getOpenMPClauseName(OMPC_linear);
2895 bool IsDecl =
2896 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2897 Diag(VD->getLocation(),
2898 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2899 << VD;
2900 continue;
2901 }
2902
2903 // A list item must be of integral or pointer type.
2904 QType = QType.getUnqualifiedType().getCanonicalType();
2905 const Type *Ty = QType.getTypePtrOrNull();
2906 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
2907 !Ty->isPointerType())) {
2908 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
2909 bool IsDecl =
2910 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2911 Diag(VD->getLocation(),
2912 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2913 << VD;
2914 continue;
2915 }
2916
2917 DSAStack->addDSA(VD, DE, OMPC_linear);
2918 Vars.push_back(DE);
2919 }
2920
2921 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002922 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00002923
2924 Expr *StepExpr = Step;
2925 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2926 !Step->isInstantiationDependent() &&
2927 !Step->containsUnexpandedParameterPack()) {
2928 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002929 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00002930 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002931 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002932 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00002933
2934 // Warn about zero linear step (it would be probably better specified as
2935 // making corresponding variables 'const').
2936 llvm::APSInt Result;
2937 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
2938 !Result.isNegative() && !Result.isStrictlyPositive())
2939 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
2940 << (Vars.size() > 1);
2941 }
2942
2943 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
2944 Vars, StepExpr);
2945}
2946
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002947OMPClause *Sema::ActOnOpenMPAlignedClause(
2948 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
2949 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
2950
2951 SmallVector<Expr *, 8> Vars;
2952 for (auto &RefExpr : VarList) {
2953 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
2954 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2955 // It will be analyzed later.
2956 Vars.push_back(RefExpr);
2957 continue;
2958 }
2959
2960 SourceLocation ELoc = RefExpr->getExprLoc();
2961 // OpenMP [2.1, C/C++]
2962 // A list item is a variable name.
2963 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
2964 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2965 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2966 continue;
2967 }
2968
2969 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2970
2971 // OpenMP [2.8.1, simd construct, Restrictions]
2972 // The type of list items appearing in the aligned clause must be
2973 // array, pointer, reference to array, or reference to pointer.
2974 QualType QType = DE->getType()
2975 .getNonReferenceType()
2976 .getUnqualifiedType()
2977 .getCanonicalType();
2978 const Type *Ty = QType.getTypePtrOrNull();
2979 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
2980 !Ty->isPointerType())) {
2981 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
2982 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
2983 bool IsDecl =
2984 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2985 Diag(VD->getLocation(),
2986 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2987 << VD;
2988 continue;
2989 }
2990
2991 // OpenMP [2.8.1, simd construct, Restrictions]
2992 // A list-item cannot appear in more than one aligned clause.
2993 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
2994 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
2995 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
2996 << getOpenMPClauseName(OMPC_aligned);
2997 continue;
2998 }
2999
3000 Vars.push_back(DE);
3001 }
3002
3003 // OpenMP [2.8.1, simd construct, Description]
3004 // The parameter of the aligned clause, alignment, must be a constant
3005 // positive integer expression.
3006 // If no optional parameter is specified, implementation-defined default
3007 // alignments for SIMD instructions on the target platforms are assumed.
3008 if (Alignment != nullptr) {
3009 ExprResult AlignResult =
3010 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3011 if (AlignResult.isInvalid())
3012 return nullptr;
3013 Alignment = AlignResult.get();
3014 }
3015 if (Vars.empty())
3016 return nullptr;
3017
3018 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3019 EndLoc, Vars, Alignment);
3020}
3021
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003022OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3023 SourceLocation StartLoc,
3024 SourceLocation LParenLoc,
3025 SourceLocation EndLoc) {
3026 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003027 for (auto &RefExpr : VarList) {
3028 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3029 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003030 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003031 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003032 continue;
3033 }
3034
Alexey Bataeved09d242014-05-28 05:53:51 +00003035 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003036 // OpenMP [2.1, C/C++]
3037 // A list item is a variable name.
3038 // OpenMP [2.14.4.1, Restrictions, p.1]
3039 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003040 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003041 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003042 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003043 continue;
3044 }
3045
3046 Decl *D = DE->getDecl();
3047 VarDecl *VD = cast<VarDecl>(D);
3048
3049 QualType Type = VD->getType();
3050 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3051 // It will be analyzed later.
3052 Vars.push_back(DE);
3053 continue;
3054 }
3055
3056 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3057 // A list item that appears in a copyin clause must be threadprivate.
3058 if (!DSAStack->isThreadPrivate(VD)) {
3059 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003060 << getOpenMPClauseName(OMPC_copyin)
3061 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003062 continue;
3063 }
3064
3065 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3066 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003067 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003068 // operator for the class type.
3069 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003070 CXXRecordDecl *RD =
3071 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003072 // FIXME This code must be replaced by actual assignment of the
3073 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003074 if (RD) {
3075 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3076 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003077 if (MD) {
3078 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3079 MD->isDeleted()) {
3080 Diag(ELoc, diag::err_omp_required_method)
3081 << getOpenMPClauseName(OMPC_copyin) << 2;
3082 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3083 VarDecl::DeclarationOnly;
3084 Diag(VD->getLocation(),
3085 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3086 << VD;
3087 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3088 continue;
3089 }
3090 MarkFunctionReferenced(ELoc, MD);
3091 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003092 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003093 }
3094
3095 DSAStack->addDSA(VD, DE, OMPC_copyin);
3096 Vars.push_back(DE);
3097 }
3098
Alexey Bataeved09d242014-05-28 05:53:51 +00003099 if (Vars.empty())
3100 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003101
3102 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3103}
3104
Alexey Bataev758e55e2013-09-06 18:03:48 +00003105#undef DSAStack