blob: 23c003815a71a686d11569e42fdef1aaf6743ce0 [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.
232 if (DVar.DKind == OMPD_parallel) {
233 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 Bataeved09d242014-05-28 05:53:51 +0000260 if (I->Directive == OMPD_parallel)
261 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;
Fraser Cormack111023c2014-04-15 08:59:09 +0000306 while (I != E && I->Directive != OMPD_parallel) {
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();
342 if (Kind != OMPD_parallel) {
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++].
1391 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1392 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001393 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1394 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001395 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001396 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1397 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) ||
1398 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1399 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001400 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001401 // The loop iteration variable in the associated for-loop of a simd
1402 // construct with just one associated for-loop may be listed in a linear
1403 // clause with a constant-linear-step that is the increment of the
1404 // associated for-loop.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001405 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1406 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001407 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001408 HasErrors = true;
1409 } else {
1410 // Make the loop iteration variable private by default.
1411 DSA.addDSA(Var, nullptr, OMPC_private);
1412 }
1413
Alexey Bataev7ff55242014-06-19 09:13:45 +00001414 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001415
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001416 // Check test-expr.
1417 HasErrors |= ISC.CheckCond(For->getCond());
1418
1419 // Check incr-expr.
1420 HasErrors |= ISC.CheckInc(For->getInc());
1421
1422 if (ISC.Dependent())
1423 return HasErrors;
1424
1425 // FIXME: Build loop's iteration space representation.
1426 return HasErrors;
1427}
1428
1429/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1430/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1431/// to get the first for loop.
1432static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1433 if (IgnoreCaptured)
1434 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1435 S = CapS->getCapturedStmt();
1436 // OpenMP [2.8.1, simd construct, Restrictions]
1437 // All loops associated with the construct must be perfectly nested; that is,
1438 // there must be no intervening code nor any OpenMP directive between any two
1439 // loops.
1440 while (true) {
1441 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1442 S = AS->getSubStmt();
1443 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1444 if (CS->size() != 1)
1445 break;
1446 S = CS->body_back();
1447 } else
1448 break;
1449 }
1450 return S;
1451}
1452
1453/// \brief Called on a for stmt to check itself and nested loops (if any).
1454static bool CheckOpenMPLoop(OpenMPDirectiveKind DKind, unsigned NestedLoopCount,
1455 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA) {
1456 // This is helper routine for loop directives (e.g., 'for', 'simd',
1457 // 'for simd', etc.).
1458 assert(NestedLoopCount == 1);
1459 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1460 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
1461 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA))
1462 return true;
1463 // Move on to the next nested for loop, or to the loop body.
1464 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1465 }
1466
1467 // FIXME: Build resulting iteration space for IR generation (collapsing
1468 // iteration spaces when loop count > 1 ('collapse' clause)).
1469 return false;
1470}
1471
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001472StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001473 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001474 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001475 // In presence of clause 'collapse', it will define the nested loops number.
1476 // For now, pass default value of 1.
1477 if (CheckOpenMPLoop(OMPD_simd, 1, AStmt, *this, *DSAStack))
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001478 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001479
1480 getCurFunction()->setHasBranchProtectedScope();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001481 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001482}
1483
Alexey Bataevf29276e2014-06-18 04:14:57 +00001484StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses,
1485 Stmt *AStmt, SourceLocation StartLoc,
1486 SourceLocation EndLoc) {
1487 // In presence of clause 'collapse', it will define the nested loops number.
1488 // For now, pass default value of 1.
1489 if (CheckOpenMPLoop(OMPD_for, 1, AStmt, *this, *DSAStack))
1490 return StmtError();
1491
1492 getCurFunction()->setHasBranchProtectedScope();
1493 return OMPForDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1494}
1495
Alexey Bataeved09d242014-05-28 05:53:51 +00001496OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001497 SourceLocation StartLoc,
1498 SourceLocation LParenLoc,
1499 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001500 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001501 switch (Kind) {
1502 case OMPC_if:
1503 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1504 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001505 case OMPC_num_threads:
1506 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1507 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001508 case OMPC_safelen:
1509 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1510 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001511 case OMPC_collapse:
1512 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1513 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001514 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001515 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001516 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001517 case OMPC_private:
1518 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001519 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001520 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001521 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001522 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001523 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001524 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001525 case OMPC_ordered:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001526 case OMPC_threadprivate:
1527 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001528 llvm_unreachable("Clause is not allowed.");
1529 }
1530 return Res;
1531}
1532
Alexey Bataeved09d242014-05-28 05:53:51 +00001533OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001534 SourceLocation LParenLoc,
1535 SourceLocation EndLoc) {
1536 Expr *ValExpr = Condition;
1537 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1538 !Condition->isInstantiationDependent() &&
1539 !Condition->containsUnexpandedParameterPack()) {
1540 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001541 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001542 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001543 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001544
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001545 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001546 }
1547
1548 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1549}
1550
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001551ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1552 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001553 if (!Op)
1554 return ExprError();
1555
1556 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1557 public:
1558 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001559 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001560 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1561 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001562 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1563 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001564 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1565 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001566 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1567 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001568 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1569 QualType T,
1570 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001571 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1572 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001573 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1574 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001575 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001576 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001577 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001578 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1579 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001580 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1581 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001582 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1583 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001584 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001585 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001586 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001587 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1588 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001589 llvm_unreachable("conversion functions are permitted");
1590 }
1591 } ConvertDiagnoser;
1592 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1593}
1594
1595OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1596 SourceLocation StartLoc,
1597 SourceLocation LParenLoc,
1598 SourceLocation EndLoc) {
1599 Expr *ValExpr = NumThreads;
1600 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1601 !NumThreads->isInstantiationDependent() &&
1602 !NumThreads->containsUnexpandedParameterPack()) {
1603 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1604 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001605 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001606 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001607 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001608
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001609 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001610
1611 // OpenMP [2.5, Restrictions]
1612 // The num_threads expression must evaluate to a positive integer value.
1613 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001614 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1615 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001616 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1617 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001618 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001619 }
1620 }
1621
Alexey Bataeved09d242014-05-28 05:53:51 +00001622 return new (Context)
1623 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001624}
1625
Alexey Bataev62c87d22014-03-21 04:51:18 +00001626ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1627 OpenMPClauseKind CKind) {
1628 if (!E)
1629 return ExprError();
1630 if (E->isValueDependent() || E->isTypeDependent() ||
1631 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001632 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001633 llvm::APSInt Result;
1634 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1635 if (ICE.isInvalid())
1636 return ExprError();
1637 if (!Result.isStrictlyPositive()) {
1638 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1639 << getOpenMPClauseName(CKind) << E->getSourceRange();
1640 return ExprError();
1641 }
1642 return ICE;
1643}
1644
1645OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1646 SourceLocation LParenLoc,
1647 SourceLocation EndLoc) {
1648 // OpenMP [2.8.1, simd construct, Description]
1649 // The parameter of the safelen clause must be a constant
1650 // positive integer expression.
1651 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1652 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001653 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001654 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001655 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001656}
1657
Alexander Musman64d33f12014-06-04 07:53:32 +00001658OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1659 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001660 SourceLocation LParenLoc,
1661 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001662 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001663 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001664 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001665 // The parameter of the collapse clause must be a constant
1666 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001667 ExprResult NumForLoopsResult =
1668 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1669 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001670 return nullptr;
1671 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001672 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001673}
1674
Alexey Bataeved09d242014-05-28 05:53:51 +00001675OMPClause *Sema::ActOnOpenMPSimpleClause(
1676 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1677 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001678 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001679 switch (Kind) {
1680 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001681 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001682 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1683 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001684 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001685 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001686 Res = ActOnOpenMPProcBindClause(
1687 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1688 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001689 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001690 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001691 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001692 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001693 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001694 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001695 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001696 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001697 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001698 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001699 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001700 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001701 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001702 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001703 case OMPC_ordered:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001704 case OMPC_threadprivate:
1705 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001706 llvm_unreachable("Clause is not allowed.");
1707 }
1708 return Res;
1709}
1710
1711OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1712 SourceLocation KindKwLoc,
1713 SourceLocation StartLoc,
1714 SourceLocation LParenLoc,
1715 SourceLocation EndLoc) {
1716 if (Kind == OMPC_DEFAULT_unknown) {
1717 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001718 static_assert(OMPC_DEFAULT_unknown > 0,
1719 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001720 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001721 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001722 Values += "'";
1723 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1724 Values += "'";
1725 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001726 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001727 Values += " or ";
1728 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001729 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001730 break;
1731 default:
1732 Values += Sep;
1733 break;
1734 }
1735 }
1736 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001737 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001738 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001739 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001740 switch (Kind) {
1741 case OMPC_DEFAULT_none:
1742 DSAStack->setDefaultDSANone();
1743 break;
1744 case OMPC_DEFAULT_shared:
1745 DSAStack->setDefaultDSAShared();
1746 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001747 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001748 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001749 break;
1750 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001751 return new (Context)
1752 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001753}
1754
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001755OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1756 SourceLocation KindKwLoc,
1757 SourceLocation StartLoc,
1758 SourceLocation LParenLoc,
1759 SourceLocation EndLoc) {
1760 if (Kind == OMPC_PROC_BIND_unknown) {
1761 std::string Values;
1762 std::string Sep(", ");
1763 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1764 Values += "'";
1765 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1766 Values += "'";
1767 switch (i) {
1768 case OMPC_PROC_BIND_unknown - 2:
1769 Values += " or ";
1770 break;
1771 case OMPC_PROC_BIND_unknown - 1:
1772 break;
1773 default:
1774 Values += Sep;
1775 break;
1776 }
1777 }
1778 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001779 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001780 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001781 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001782 return new (Context)
1783 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001784}
1785
Alexey Bataev56dafe82014-06-20 07:16:17 +00001786OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1787 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
1788 SourceLocation StartLoc, SourceLocation LParenLoc,
1789 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
1790 SourceLocation EndLoc) {
1791 OMPClause *Res = nullptr;
1792 switch (Kind) {
1793 case OMPC_schedule:
1794 Res = ActOnOpenMPScheduleClause(
1795 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
1796 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
1797 break;
1798 case OMPC_if:
1799 case OMPC_num_threads:
1800 case OMPC_safelen:
1801 case OMPC_collapse:
1802 case OMPC_default:
1803 case OMPC_proc_bind:
1804 case OMPC_private:
1805 case OMPC_firstprivate:
1806 case OMPC_lastprivate:
1807 case OMPC_shared:
1808 case OMPC_reduction:
1809 case OMPC_linear:
1810 case OMPC_aligned:
1811 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001812 case OMPC_ordered:
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;
1884 case OMPC_if:
1885 case OMPC_num_threads:
1886 case OMPC_safelen:
1887 case OMPC_collapse:
1888 case OMPC_schedule:
1889 case OMPC_private:
1890 case OMPC_firstprivate:
1891 case OMPC_lastprivate:
1892 case OMPC_shared:
1893 case OMPC_reduction:
1894 case OMPC_linear:
1895 case OMPC_aligned:
1896 case OMPC_copyin:
1897 case OMPC_default:
1898 case OMPC_proc_bind:
1899 case OMPC_threadprivate:
1900 case OMPC_unknown:
1901 llvm_unreachable("Clause is not allowed.");
1902 }
1903 return Res;
1904}
1905
1906OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
1907 SourceLocation EndLoc) {
1908 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
1909}
1910
Alexey Bataevc5e02582014-06-16 07:08:35 +00001911OMPClause *Sema::ActOnOpenMPVarListClause(
1912 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
1913 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
1914 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
1915 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001916 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001917 switch (Kind) {
1918 case OMPC_private:
1919 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1920 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001921 case OMPC_firstprivate:
1922 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1923 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00001924 case OMPC_lastprivate:
1925 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1926 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001927 case OMPC_shared:
1928 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1929 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001930 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00001931 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
1932 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001933 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001934 case OMPC_linear:
1935 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1936 ColonLoc, EndLoc);
1937 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001938 case OMPC_aligned:
1939 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
1940 ColonLoc, EndLoc);
1941 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001942 case OMPC_copyin:
1943 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1944 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001945 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001946 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001947 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001948 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001949 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001950 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001951 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001952 case OMPC_ordered:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001953 case OMPC_threadprivate:
1954 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001955 llvm_unreachable("Clause is not allowed.");
1956 }
1957 return Res;
1958}
1959
1960OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1961 SourceLocation StartLoc,
1962 SourceLocation LParenLoc,
1963 SourceLocation EndLoc) {
1964 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001965 for (auto &RefExpr : VarList) {
1966 assert(RefExpr && "NULL expr in OpenMP private clause.");
1967 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001968 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001969 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001970 continue;
1971 }
1972
Alexey Bataeved09d242014-05-28 05:53:51 +00001973 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001974 // OpenMP [2.1, C/C++]
1975 // A list item is a variable name.
1976 // OpenMP [2.9.3.3, Restrictions, p.1]
1977 // A variable that is part of another variable (as an array or
1978 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001979 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001980 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001981 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001982 continue;
1983 }
1984 Decl *D = DE->getDecl();
1985 VarDecl *VD = cast<VarDecl>(D);
1986
1987 QualType Type = VD->getType();
1988 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1989 // It will be analyzed later.
1990 Vars.push_back(DE);
1991 continue;
1992 }
1993
1994 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1995 // A variable that appears in a private clause must not have an incomplete
1996 // type or a reference type.
1997 if (RequireCompleteType(ELoc, Type,
1998 diag::err_omp_private_incomplete_type)) {
1999 continue;
2000 }
2001 if (Type->isReferenceType()) {
2002 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002003 << getOpenMPClauseName(OMPC_private) << Type;
2004 bool IsDecl =
2005 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2006 Diag(VD->getLocation(),
2007 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2008 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002009 continue;
2010 }
2011
2012 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2013 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002014 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002015 // class type.
2016 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002017 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2018 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002019 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002020 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2021 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2022 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002023 // FIXME This code must be replaced by actual constructing/destructing of
2024 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002025 if (RD) {
2026 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2027 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002028 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002029 if (!CD ||
2030 CheckConstructorAccess(ELoc, CD,
2031 InitializedEntity::InitializeTemporary(Type),
2032 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002033 CD->isDeleted()) {
2034 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002035 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002036 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2037 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002038 Diag(VD->getLocation(),
2039 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2040 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002041 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2042 continue;
2043 }
2044 MarkFunctionReferenced(ELoc, CD);
2045 DiagnoseUseOfDecl(CD, ELoc);
2046
2047 CXXDestructorDecl *DD = RD->getDestructor();
2048 if (DD) {
2049 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2050 DD->isDeleted()) {
2051 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002052 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002053 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2054 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002055 Diag(VD->getLocation(),
2056 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2057 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002058 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2059 continue;
2060 }
2061 MarkFunctionReferenced(ELoc, DD);
2062 DiagnoseUseOfDecl(DD, ELoc);
2063 }
2064 }
2065
Alexey Bataev758e55e2013-09-06 18:03:48 +00002066 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2067 // in a Construct]
2068 // Variables with the predetermined data-sharing attributes may not be
2069 // listed in data-sharing attributes clauses, except for the cases
2070 // listed below. For these exceptions only, listing a predetermined
2071 // variable in a data-sharing attribute clause is allowed and overrides
2072 // the variable's predetermined data-sharing attributes.
2073 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2074 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002075 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2076 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002077 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002078 continue;
2079 }
2080
2081 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002082 Vars.push_back(DE);
2083 }
2084
Alexey Bataeved09d242014-05-28 05:53:51 +00002085 if (Vars.empty())
2086 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002087
2088 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2089}
2090
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002091OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2092 SourceLocation StartLoc,
2093 SourceLocation LParenLoc,
2094 SourceLocation EndLoc) {
2095 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002096 for (auto &RefExpr : VarList) {
2097 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2098 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002099 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002100 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002101 continue;
2102 }
2103
Alexey Bataeved09d242014-05-28 05:53:51 +00002104 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002105 // OpenMP [2.1, C/C++]
2106 // A list item is a variable name.
2107 // OpenMP [2.9.3.3, Restrictions, p.1]
2108 // A variable that is part of another variable (as an array or
2109 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002110 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002111 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002112 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002113 continue;
2114 }
2115 Decl *D = DE->getDecl();
2116 VarDecl *VD = cast<VarDecl>(D);
2117
2118 QualType Type = VD->getType();
2119 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2120 // It will be analyzed later.
2121 Vars.push_back(DE);
2122 continue;
2123 }
2124
2125 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2126 // A variable that appears in a private clause must not have an incomplete
2127 // type or a reference type.
2128 if (RequireCompleteType(ELoc, Type,
2129 diag::err_omp_firstprivate_incomplete_type)) {
2130 continue;
2131 }
2132 if (Type->isReferenceType()) {
2133 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002134 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2135 bool IsDecl =
2136 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2137 Diag(VD->getLocation(),
2138 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2139 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002140 continue;
2141 }
2142
2143 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2144 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002145 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002146 // class type.
2147 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002148 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2149 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2150 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002151 // FIXME This code must be replaced by actual constructing/destructing of
2152 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002153 if (RD) {
2154 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2155 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002156 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002157 if (!CD ||
2158 CheckConstructorAccess(ELoc, CD,
2159 InitializedEntity::InitializeTemporary(Type),
2160 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002161 CD->isDeleted()) {
2162 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002163 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002164 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2165 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002166 Diag(VD->getLocation(),
2167 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2168 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002169 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2170 continue;
2171 }
2172 MarkFunctionReferenced(ELoc, CD);
2173 DiagnoseUseOfDecl(CD, ELoc);
2174
2175 CXXDestructorDecl *DD = RD->getDestructor();
2176 if (DD) {
2177 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2178 DD->isDeleted()) {
2179 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002180 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002181 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2182 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002183 Diag(VD->getLocation(),
2184 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2185 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002186 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2187 continue;
2188 }
2189 MarkFunctionReferenced(ELoc, DD);
2190 DiagnoseUseOfDecl(DD, ELoc);
2191 }
2192 }
2193
2194 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2195 // variable and it was checked already.
2196 if (StartLoc.isValid() && EndLoc.isValid()) {
2197 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2198 Type = Type.getNonReferenceType().getCanonicalType();
2199 bool IsConstant = Type.isConstant(Context);
2200 Type = Context.getBaseElementType(Type);
2201 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2202 // A list item that specifies a given variable may not appear in more
2203 // than one clause on the same directive, except that a variable may be
2204 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002205 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002206 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002207 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002208 << getOpenMPClauseName(DVar.CKind)
2209 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002210 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002211 continue;
2212 }
2213
2214 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2215 // in a Construct]
2216 // Variables with the predetermined data-sharing attributes may not be
2217 // listed in data-sharing attributes clauses, except for the cases
2218 // listed below. For these exceptions only, listing a predetermined
2219 // variable in a data-sharing attribute clause is allowed and overrides
2220 // the variable's predetermined data-sharing attributes.
2221 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2222 // in a Construct, C/C++, p.2]
2223 // Variables with const-qualified type having no mutable member may be
2224 // listed in a firstprivate clause, even if they are static data members.
2225 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2226 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2227 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002228 << getOpenMPClauseName(DVar.CKind)
2229 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002230 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002231 continue;
2232 }
2233
Alexey Bataevf29276e2014-06-18 04:14:57 +00002234 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002235 // OpenMP [2.9.3.4, Restrictions, p.2]
2236 // A list item that is private within a parallel region must not appear
2237 // in a firstprivate clause on a worksharing construct if any of the
2238 // worksharing regions arising from the worksharing construct ever bind
2239 // to any of the parallel regions arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002240 if (isOpenMPWorksharingDirective(CurrDir)) {
2241 DVar = DSAStack->getImplicitDSA(VD);
2242 if (DVar.CKind != OMPC_shared) {
2243 Diag(ELoc, diag::err_omp_required_access)
2244 << getOpenMPClauseName(OMPC_firstprivate)
2245 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002246 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002247 continue;
2248 }
2249 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002250 // OpenMP [2.9.3.4, Restrictions, p.3]
2251 // A list item that appears in a reduction clause of a parallel construct
2252 // must not appear in a firstprivate clause on a worksharing or task
2253 // construct if any of the worksharing or task regions arising from the
2254 // worksharing or task construct ever bind to any of the parallel regions
2255 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002256 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002257 // OpenMP [2.9.3.4, Restrictions, p.4]
2258 // A list item that appears in a reduction clause in worksharing
2259 // construct must not appear in a firstprivate clause in a task construct
2260 // encountered during execution of any of the worksharing regions arising
2261 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002262 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002263 }
2264
2265 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2266 Vars.push_back(DE);
2267 }
2268
Alexey Bataeved09d242014-05-28 05:53:51 +00002269 if (Vars.empty())
2270 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002271
2272 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2273 Vars);
2274}
2275
Alexander Musman1bb328c2014-06-04 13:06:39 +00002276OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2277 SourceLocation StartLoc,
2278 SourceLocation LParenLoc,
2279 SourceLocation EndLoc) {
2280 SmallVector<Expr *, 8> Vars;
2281 for (auto &RefExpr : VarList) {
2282 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2283 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2284 // It will be analyzed later.
2285 Vars.push_back(RefExpr);
2286 continue;
2287 }
2288
2289 SourceLocation ELoc = RefExpr->getExprLoc();
2290 // OpenMP [2.1, C/C++]
2291 // A list item is a variable name.
2292 // OpenMP [2.14.3.5, Restrictions, p.1]
2293 // A variable that is part of another variable (as an array or structure
2294 // element) cannot appear in a lastprivate clause.
2295 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2296 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2297 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2298 continue;
2299 }
2300 Decl *D = DE->getDecl();
2301 VarDecl *VD = cast<VarDecl>(D);
2302
2303 QualType Type = VD->getType();
2304 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2305 // It will be analyzed later.
2306 Vars.push_back(DE);
2307 continue;
2308 }
2309
2310 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2311 // A variable that appears in a lastprivate clause must not have an
2312 // incomplete type or a reference type.
2313 if (RequireCompleteType(ELoc, Type,
2314 diag::err_omp_lastprivate_incomplete_type)) {
2315 continue;
2316 }
2317 if (Type->isReferenceType()) {
2318 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2319 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2320 bool IsDecl =
2321 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2322 Diag(VD->getLocation(),
2323 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2324 << VD;
2325 continue;
2326 }
2327
2328 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2329 // in a Construct]
2330 // Variables with the predetermined data-sharing attributes may not be
2331 // listed in data-sharing attributes clauses, except for the cases
2332 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002333 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2334 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2335 DVar.CKind != OMPC_firstprivate &&
2336 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2337 Diag(ELoc, diag::err_omp_wrong_dsa)
2338 << getOpenMPClauseName(DVar.CKind)
2339 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002340 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002341 continue;
2342 }
2343
Alexey Bataevf29276e2014-06-18 04:14:57 +00002344 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2345 // OpenMP [2.14.3.5, Restrictions, p.2]
2346 // A list item that is private within a parallel region, or that appears in
2347 // the reduction clause of a parallel construct, must not appear in a
2348 // lastprivate clause on a worksharing construct if any of the corresponding
2349 // worksharing regions ever binds to any of the corresponding parallel
2350 // regions.
2351 if (isOpenMPWorksharingDirective(CurrDir)) {
2352 DVar = DSAStack->getImplicitDSA(VD);
2353 if (DVar.CKind != OMPC_shared) {
2354 Diag(ELoc, diag::err_omp_required_access)
2355 << getOpenMPClauseName(OMPC_lastprivate)
2356 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002357 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002358 continue;
2359 }
2360 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002361 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002362 // A variable of class type (or array thereof) that appears in a
2363 // lastprivate clause requires an accessible, unambiguous default
2364 // constructor for the class type, unless the list item is also specified
2365 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002366 // A variable of class type (or array thereof) that appears in a
2367 // lastprivate clause requires an accessible, unambiguous copy assignment
2368 // operator for the class type.
2369 while (Type.getNonReferenceType()->isArrayType())
2370 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2371 ->getElementType();
2372 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2373 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2374 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002375 // FIXME This code must be replaced by actual copying and destructing of the
2376 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002377 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002378 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2379 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002380 if (MD) {
2381 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2382 MD->isDeleted()) {
2383 Diag(ELoc, diag::err_omp_required_method)
2384 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2385 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2386 VarDecl::DeclarationOnly;
2387 Diag(VD->getLocation(),
2388 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2389 << VD;
2390 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2391 continue;
2392 }
2393 MarkFunctionReferenced(ELoc, MD);
2394 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002395 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002396
2397 CXXDestructorDecl *DD = RD->getDestructor();
2398 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002399 PartialDiagnostic PD =
2400 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002401 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2402 DD->isDeleted()) {
2403 Diag(ELoc, diag::err_omp_required_method)
2404 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2405 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2406 VarDecl::DeclarationOnly;
2407 Diag(VD->getLocation(),
2408 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2409 << VD;
2410 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2411 continue;
2412 }
2413 MarkFunctionReferenced(ELoc, DD);
2414 DiagnoseUseOfDecl(DD, ELoc);
2415 }
2416 }
2417
Alexey Bataevf29276e2014-06-18 04:14:57 +00002418 if (DVar.CKind != OMPC_firstprivate)
2419 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002420 Vars.push_back(DE);
2421 }
2422
2423 if (Vars.empty())
2424 return nullptr;
2425
2426 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2427 Vars);
2428}
2429
Alexey Bataev758e55e2013-09-06 18:03:48 +00002430OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2431 SourceLocation StartLoc,
2432 SourceLocation LParenLoc,
2433 SourceLocation EndLoc) {
2434 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002435 for (auto &RefExpr : VarList) {
2436 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2437 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002438 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002439 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002440 continue;
2441 }
2442
Alexey Bataeved09d242014-05-28 05:53:51 +00002443 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002444 // OpenMP [2.1, C/C++]
2445 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002446 // OpenMP [2.14.3.2, Restrictions, p.1]
2447 // A variable that is part of another variable (as an array or structure
2448 // element) cannot appear in a shared unless it is a static data member
2449 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002450 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002451 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002452 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002453 continue;
2454 }
2455 Decl *D = DE->getDecl();
2456 VarDecl *VD = cast<VarDecl>(D);
2457
2458 QualType Type = VD->getType();
2459 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2460 // It will be analyzed later.
2461 Vars.push_back(DE);
2462 continue;
2463 }
2464
2465 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2466 // in a Construct]
2467 // Variables with the predetermined data-sharing attributes may not be
2468 // listed in data-sharing attributes clauses, except for the cases
2469 // listed below. For these exceptions only, listing a predetermined
2470 // variable in a data-sharing attribute clause is allowed and overrides
2471 // the variable's predetermined data-sharing attributes.
2472 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002473 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2474 DVar.RefExpr) {
2475 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2476 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002477 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002478 continue;
2479 }
2480
2481 DSAStack->addDSA(VD, DE, OMPC_shared);
2482 Vars.push_back(DE);
2483 }
2484
Alexey Bataeved09d242014-05-28 05:53:51 +00002485 if (Vars.empty())
2486 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002487
2488 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2489}
2490
Alexey Bataevc5e02582014-06-16 07:08:35 +00002491namespace {
2492class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2493 DSAStackTy *Stack;
2494
2495public:
2496 bool VisitDeclRefExpr(DeclRefExpr *E) {
2497 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2498 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2499 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2500 return false;
2501 if (DVar.CKind != OMPC_unknown)
2502 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002503 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002504 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002505 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002506 return true;
2507 return false;
2508 }
2509 return false;
2510 }
2511 bool VisitStmt(Stmt *S) {
2512 for (auto Child : S->children()) {
2513 if (Child && Visit(Child))
2514 return true;
2515 }
2516 return false;
2517 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002518 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002519};
Alexey Bataev23b69422014-06-18 07:08:49 +00002520} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002521
2522OMPClause *Sema::ActOnOpenMPReductionClause(
2523 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2524 SourceLocation ColonLoc, SourceLocation EndLoc,
2525 CXXScopeSpec &ReductionIdScopeSpec,
2526 const DeclarationNameInfo &ReductionId) {
2527 // TODO: Allow scope specification search when 'declare reduction' is
2528 // supported.
2529 assert(ReductionIdScopeSpec.isEmpty() &&
2530 "No support for scoped reduction identifiers yet.");
2531
2532 auto DN = ReductionId.getName();
2533 auto OOK = DN.getCXXOverloadedOperator();
2534 BinaryOperatorKind BOK = BO_Comma;
2535
2536 // OpenMP [2.14.3.6, reduction clause]
2537 // C
2538 // reduction-identifier is either an identifier or one of the following
2539 // operators: +, -, *, &, |, ^, && and ||
2540 // C++
2541 // reduction-identifier is either an id-expression or one of the following
2542 // operators: +, -, *, &, |, ^, && and ||
2543 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2544 switch (OOK) {
2545 case OO_Plus:
2546 case OO_Minus:
2547 BOK = BO_AddAssign;
2548 break;
2549 case OO_Star:
2550 BOK = BO_MulAssign;
2551 break;
2552 case OO_Amp:
2553 BOK = BO_AndAssign;
2554 break;
2555 case OO_Pipe:
2556 BOK = BO_OrAssign;
2557 break;
2558 case OO_Caret:
2559 BOK = BO_XorAssign;
2560 break;
2561 case OO_AmpAmp:
2562 BOK = BO_LAnd;
2563 break;
2564 case OO_PipePipe:
2565 BOK = BO_LOr;
2566 break;
2567 default:
2568 if (auto II = DN.getAsIdentifierInfo()) {
2569 if (II->isStr("max"))
2570 BOK = BO_GT;
2571 else if (II->isStr("min"))
2572 BOK = BO_LT;
2573 }
2574 break;
2575 }
2576 SourceRange ReductionIdRange;
2577 if (ReductionIdScopeSpec.isValid()) {
2578 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2579 }
2580 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2581 if (BOK == BO_Comma) {
2582 // Not allowed reduction identifier is found.
2583 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2584 << ReductionIdRange;
2585 return nullptr;
2586 }
2587
2588 SmallVector<Expr *, 8> Vars;
2589 for (auto RefExpr : VarList) {
2590 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2591 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2592 // It will be analyzed later.
2593 Vars.push_back(RefExpr);
2594 continue;
2595 }
2596
2597 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2598 RefExpr->isInstantiationDependent() ||
2599 RefExpr->containsUnexpandedParameterPack()) {
2600 // It will be analyzed later.
2601 Vars.push_back(RefExpr);
2602 continue;
2603 }
2604
2605 auto ELoc = RefExpr->getExprLoc();
2606 auto ERange = RefExpr->getSourceRange();
2607 // OpenMP [2.1, C/C++]
2608 // A list item is a variable or array section, subject to the restrictions
2609 // specified in Section 2.4 on page 42 and in each of the sections
2610 // describing clauses and directives for which a list appears.
2611 // OpenMP [2.14.3.3, Restrictions, p.1]
2612 // A variable that is part of another variable (as an array or
2613 // structure element) cannot appear in a private clause.
2614 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2615 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2616 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2617 continue;
2618 }
2619 auto D = DE->getDecl();
2620 auto VD = cast<VarDecl>(D);
2621 auto Type = VD->getType();
2622 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2623 // A variable that appears in a private clause must not have an incomplete
2624 // type or a reference type.
2625 if (RequireCompleteType(ELoc, Type,
2626 diag::err_omp_reduction_incomplete_type))
2627 continue;
2628 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2629 // Arrays may not appear in a reduction clause.
2630 if (Type.getNonReferenceType()->isArrayType()) {
2631 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2632 bool IsDecl =
2633 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2634 Diag(VD->getLocation(),
2635 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2636 << VD;
2637 continue;
2638 }
2639 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2640 // A list item that appears in a reduction clause must not be
2641 // const-qualified.
2642 if (Type.getNonReferenceType().isConstant(Context)) {
2643 Diag(ELoc, diag::err_omp_const_variable)
2644 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2645 bool IsDecl =
2646 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2647 Diag(VD->getLocation(),
2648 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2649 << VD;
2650 continue;
2651 }
2652 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2653 // If a list-item is a reference type then it must bind to the same object
2654 // for all threads of the team.
2655 VarDecl *VDDef = VD->getDefinition();
2656 if (Type->isReferenceType() && VDDef) {
2657 DSARefChecker Check(DSAStack);
2658 if (Check.Visit(VDDef->getInit())) {
2659 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2660 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2661 continue;
2662 }
2663 }
2664 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2665 // The type of a list item that appears in a reduction clause must be valid
2666 // for the reduction-identifier. For a max or min reduction in C, the type
2667 // of the list item must be an allowed arithmetic data type: char, int,
2668 // float, double, or _Bool, possibly modified with long, short, signed, or
2669 // unsigned. For a max or min reduction in C++, the type of the list item
2670 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2671 // double, or bool, possibly modified with long, short, signed, or unsigned.
2672 if ((BOK == BO_GT || BOK == BO_LT) &&
2673 !(Type->isScalarType() ||
2674 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2675 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2676 << getLangOpts().CPlusPlus;
2677 bool IsDecl =
2678 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2679 Diag(VD->getLocation(),
2680 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2681 << VD;
2682 continue;
2683 }
2684 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2685 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2686 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2687 bool IsDecl =
2688 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2689 Diag(VD->getLocation(),
2690 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2691 << VD;
2692 continue;
2693 }
2694 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2695 getDiagnostics().setSuppressAllDiagnostics(true);
2696 ExprResult ReductionOp =
2697 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2698 RefExpr, RefExpr);
2699 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2700 if (ReductionOp.isInvalid()) {
2701 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002702 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002703 bool IsDecl =
2704 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2705 Diag(VD->getLocation(),
2706 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2707 << VD;
2708 continue;
2709 }
2710
2711 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2712 // in a Construct]
2713 // Variables with the predetermined data-sharing attributes may not be
2714 // listed in data-sharing attributes clauses, except for the cases
2715 // listed below. For these exceptions only, listing a predetermined
2716 // variable in a data-sharing attribute clause is allowed and overrides
2717 // the variable's predetermined data-sharing attributes.
2718 // OpenMP [2.14.3.6, Restrictions, p.3]
2719 // Any number of reduction clauses can be specified on the directive,
2720 // but a list item can appear only once in the reduction clauses for that
2721 // directive.
2722 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2723 if (DVar.CKind == OMPC_reduction) {
2724 Diag(ELoc, diag::err_omp_once_referenced)
2725 << getOpenMPClauseName(OMPC_reduction);
2726 if (DVar.RefExpr) {
2727 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2728 }
2729 } else if (DVar.CKind != OMPC_unknown) {
2730 Diag(ELoc, diag::err_omp_wrong_dsa)
2731 << getOpenMPClauseName(DVar.CKind)
2732 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002733 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002734 continue;
2735 }
2736
2737 // OpenMP [2.14.3.6, Restrictions, p.1]
2738 // A list item that appears in a reduction clause of a worksharing
2739 // construct must be shared in the parallel regions to which any of the
2740 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002741 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2742 if (isOpenMPWorksharingDirective(CurrDir)) {
2743 DVar = DSAStack->getImplicitDSA(VD);
2744 if (DVar.CKind != OMPC_shared) {
2745 Diag(ELoc, diag::err_omp_required_access)
2746 << getOpenMPClauseName(OMPC_reduction)
2747 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002748 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002749 continue;
2750 }
2751 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00002752
2753 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2754 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2755 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002756 // FIXME This code must be replaced by actual constructing/destructing of
2757 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00002758 if (RD) {
2759 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2760 PartialDiagnostic PD =
2761 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00002762 if (!CD ||
2763 CheckConstructorAccess(ELoc, CD,
2764 InitializedEntity::InitializeTemporary(Type),
2765 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00002766 CD->isDeleted()) {
2767 Diag(ELoc, diag::err_omp_required_method)
2768 << getOpenMPClauseName(OMPC_reduction) << 0;
2769 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2770 VarDecl::DeclarationOnly;
2771 Diag(VD->getLocation(),
2772 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2773 << VD;
2774 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2775 continue;
2776 }
2777 MarkFunctionReferenced(ELoc, CD);
2778 DiagnoseUseOfDecl(CD, ELoc);
2779
2780 CXXDestructorDecl *DD = RD->getDestructor();
2781 if (DD) {
2782 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2783 DD->isDeleted()) {
2784 Diag(ELoc, diag::err_omp_required_method)
2785 << getOpenMPClauseName(OMPC_reduction) << 4;
2786 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2787 VarDecl::DeclarationOnly;
2788 Diag(VD->getLocation(),
2789 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2790 << VD;
2791 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2792 continue;
2793 }
2794 MarkFunctionReferenced(ELoc, DD);
2795 DiagnoseUseOfDecl(DD, ELoc);
2796 }
2797 }
2798
2799 DSAStack->addDSA(VD, DE, OMPC_reduction);
2800 Vars.push_back(DE);
2801 }
2802
2803 if (Vars.empty())
2804 return nullptr;
2805
2806 return OMPReductionClause::Create(
2807 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
2808 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
2809}
2810
Alexander Musman8dba6642014-04-22 13:09:42 +00002811OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2812 SourceLocation StartLoc,
2813 SourceLocation LParenLoc,
2814 SourceLocation ColonLoc,
2815 SourceLocation EndLoc) {
2816 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002817 for (auto &RefExpr : VarList) {
2818 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2819 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002820 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002821 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002822 continue;
2823 }
2824
2825 // OpenMP [2.14.3.7, linear clause]
2826 // A list item that appears in a linear clause is subject to the private
2827 // clause semantics described in Section 2.14.3.3 on page 159 except as
2828 // noted. In addition, the value of the new list item on each iteration
2829 // of the associated loop(s) corresponds to the value of the original
2830 // list item before entering the construct plus the logical number of
2831 // the iteration times linear-step.
2832
Alexey Bataeved09d242014-05-28 05:53:51 +00002833 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00002834 // OpenMP [2.1, C/C++]
2835 // A list item is a variable name.
2836 // OpenMP [2.14.3.3, Restrictions, p.1]
2837 // A variable that is part of another variable (as an array or
2838 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002839 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002840 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002841 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00002842 continue;
2843 }
2844
2845 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2846
2847 // OpenMP [2.14.3.7, linear clause]
2848 // A list-item cannot appear in more than one linear clause.
2849 // A list-item that appears in a linear clause cannot appear in any
2850 // other data-sharing attribute clause.
2851 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2852 if (DVar.RefExpr) {
2853 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2854 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002855 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00002856 continue;
2857 }
2858
2859 QualType QType = VD->getType();
2860 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2861 // It will be analyzed later.
2862 Vars.push_back(DE);
2863 continue;
2864 }
2865
2866 // A variable must not have an incomplete type or a reference type.
2867 if (RequireCompleteType(ELoc, QType,
2868 diag::err_omp_linear_incomplete_type)) {
2869 continue;
2870 }
2871 if (QType->isReferenceType()) {
2872 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2873 << getOpenMPClauseName(OMPC_linear) << QType;
2874 bool IsDecl =
2875 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2876 Diag(VD->getLocation(),
2877 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2878 << VD;
2879 continue;
2880 }
2881
2882 // A list item must not be const-qualified.
2883 if (QType.isConstant(Context)) {
2884 Diag(ELoc, diag::err_omp_const_variable)
2885 << getOpenMPClauseName(OMPC_linear);
2886 bool IsDecl =
2887 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2888 Diag(VD->getLocation(),
2889 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2890 << VD;
2891 continue;
2892 }
2893
2894 // A list item must be of integral or pointer type.
2895 QType = QType.getUnqualifiedType().getCanonicalType();
2896 const Type *Ty = QType.getTypePtrOrNull();
2897 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
2898 !Ty->isPointerType())) {
2899 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
2900 bool IsDecl =
2901 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2902 Diag(VD->getLocation(),
2903 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2904 << VD;
2905 continue;
2906 }
2907
2908 DSAStack->addDSA(VD, DE, OMPC_linear);
2909 Vars.push_back(DE);
2910 }
2911
2912 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002913 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00002914
2915 Expr *StepExpr = Step;
2916 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2917 !Step->isInstantiationDependent() &&
2918 !Step->containsUnexpandedParameterPack()) {
2919 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002920 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00002921 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002922 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002923 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00002924
2925 // Warn about zero linear step (it would be probably better specified as
2926 // making corresponding variables 'const').
2927 llvm::APSInt Result;
2928 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
2929 !Result.isNegative() && !Result.isStrictlyPositive())
2930 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
2931 << (Vars.size() > 1);
2932 }
2933
2934 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
2935 Vars, StepExpr);
2936}
2937
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002938OMPClause *Sema::ActOnOpenMPAlignedClause(
2939 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
2940 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
2941
2942 SmallVector<Expr *, 8> Vars;
2943 for (auto &RefExpr : VarList) {
2944 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
2945 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2946 // It will be analyzed later.
2947 Vars.push_back(RefExpr);
2948 continue;
2949 }
2950
2951 SourceLocation ELoc = RefExpr->getExprLoc();
2952 // OpenMP [2.1, C/C++]
2953 // A list item is a variable name.
2954 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
2955 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2956 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2957 continue;
2958 }
2959
2960 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2961
2962 // OpenMP [2.8.1, simd construct, Restrictions]
2963 // The type of list items appearing in the aligned clause must be
2964 // array, pointer, reference to array, or reference to pointer.
2965 QualType QType = DE->getType()
2966 .getNonReferenceType()
2967 .getUnqualifiedType()
2968 .getCanonicalType();
2969 const Type *Ty = QType.getTypePtrOrNull();
2970 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
2971 !Ty->isPointerType())) {
2972 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
2973 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
2974 bool IsDecl =
2975 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2976 Diag(VD->getLocation(),
2977 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2978 << VD;
2979 continue;
2980 }
2981
2982 // OpenMP [2.8.1, simd construct, Restrictions]
2983 // A list-item cannot appear in more than one aligned clause.
2984 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
2985 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
2986 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
2987 << getOpenMPClauseName(OMPC_aligned);
2988 continue;
2989 }
2990
2991 Vars.push_back(DE);
2992 }
2993
2994 // OpenMP [2.8.1, simd construct, Description]
2995 // The parameter of the aligned clause, alignment, must be a constant
2996 // positive integer expression.
2997 // If no optional parameter is specified, implementation-defined default
2998 // alignments for SIMD instructions on the target platforms are assumed.
2999 if (Alignment != nullptr) {
3000 ExprResult AlignResult =
3001 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3002 if (AlignResult.isInvalid())
3003 return nullptr;
3004 Alignment = AlignResult.get();
3005 }
3006 if (Vars.empty())
3007 return nullptr;
3008
3009 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3010 EndLoc, Vars, Alignment);
3011}
3012
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003013OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3014 SourceLocation StartLoc,
3015 SourceLocation LParenLoc,
3016 SourceLocation EndLoc) {
3017 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003018 for (auto &RefExpr : VarList) {
3019 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3020 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003021 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003022 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003023 continue;
3024 }
3025
Alexey Bataeved09d242014-05-28 05:53:51 +00003026 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003027 // OpenMP [2.1, C/C++]
3028 // A list item is a variable name.
3029 // OpenMP [2.14.4.1, Restrictions, p.1]
3030 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003031 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003032 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003033 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003034 continue;
3035 }
3036
3037 Decl *D = DE->getDecl();
3038 VarDecl *VD = cast<VarDecl>(D);
3039
3040 QualType Type = VD->getType();
3041 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3042 // It will be analyzed later.
3043 Vars.push_back(DE);
3044 continue;
3045 }
3046
3047 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3048 // A list item that appears in a copyin clause must be threadprivate.
3049 if (!DSAStack->isThreadPrivate(VD)) {
3050 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003051 << getOpenMPClauseName(OMPC_copyin)
3052 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003053 continue;
3054 }
3055
3056 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3057 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003058 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003059 // operator for the class type.
3060 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003061 CXXRecordDecl *RD =
3062 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003063 // FIXME This code must be replaced by actual assignment of the
3064 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003065 if (RD) {
3066 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3067 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003068 if (MD) {
3069 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3070 MD->isDeleted()) {
3071 Diag(ELoc, diag::err_omp_required_method)
3072 << getOpenMPClauseName(OMPC_copyin) << 2;
3073 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3074 VarDecl::DeclarationOnly;
3075 Diag(VD->getLocation(),
3076 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3077 << VD;
3078 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3079 continue;
3080 }
3081 MarkFunctionReferenced(ELoc, MD);
3082 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003083 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003084 }
3085
3086 DSAStack->addDSA(VD, DE, OMPC_copyin);
3087 Vars.push_back(DE);
3088 }
3089
Alexey Bataeved09d242014-05-28 05:53:51 +00003090 if (Vars.empty())
3091 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003092
3093 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3094}
3095
Alexey Bataev758e55e2013-09-06 18:03:48 +00003096#undef DSAStack