blob: 515bd5f8c488ebcecc39bc19813800667a408e6d [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 Bataevaadd52e2014-02-13 05:29:23 +00001525 case OMPC_threadprivate:
1526 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001527 llvm_unreachable("Clause is not allowed.");
1528 }
1529 return Res;
1530}
1531
Alexey Bataeved09d242014-05-28 05:53:51 +00001532OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001533 SourceLocation LParenLoc,
1534 SourceLocation EndLoc) {
1535 Expr *ValExpr = Condition;
1536 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1537 !Condition->isInstantiationDependent() &&
1538 !Condition->containsUnexpandedParameterPack()) {
1539 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001540 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001541 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001542 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001543
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001544 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001545 }
1546
1547 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1548}
1549
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001550ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1551 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001552 if (!Op)
1553 return ExprError();
1554
1555 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1556 public:
1557 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001558 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001559 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1560 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001561 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1562 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001563 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1564 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001565 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1566 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001567 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1568 QualType T,
1569 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001570 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1571 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001572 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1573 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001574 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001575 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001576 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001577 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1578 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001579 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1580 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001581 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1582 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001583 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001584 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001585 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001586 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1587 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001588 llvm_unreachable("conversion functions are permitted");
1589 }
1590 } ConvertDiagnoser;
1591 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1592}
1593
1594OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1595 SourceLocation StartLoc,
1596 SourceLocation LParenLoc,
1597 SourceLocation EndLoc) {
1598 Expr *ValExpr = NumThreads;
1599 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1600 !NumThreads->isInstantiationDependent() &&
1601 !NumThreads->containsUnexpandedParameterPack()) {
1602 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1603 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001604 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001605 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001606 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001607
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001608 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001609
1610 // OpenMP [2.5, Restrictions]
1611 // The num_threads expression must evaluate to a positive integer value.
1612 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001613 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1614 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001615 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1616 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001617 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001618 }
1619 }
1620
Alexey Bataeved09d242014-05-28 05:53:51 +00001621 return new (Context)
1622 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001623}
1624
Alexey Bataev62c87d22014-03-21 04:51:18 +00001625ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1626 OpenMPClauseKind CKind) {
1627 if (!E)
1628 return ExprError();
1629 if (E->isValueDependent() || E->isTypeDependent() ||
1630 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001631 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001632 llvm::APSInt Result;
1633 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1634 if (ICE.isInvalid())
1635 return ExprError();
1636 if (!Result.isStrictlyPositive()) {
1637 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1638 << getOpenMPClauseName(CKind) << E->getSourceRange();
1639 return ExprError();
1640 }
1641 return ICE;
1642}
1643
1644OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1645 SourceLocation LParenLoc,
1646 SourceLocation EndLoc) {
1647 // OpenMP [2.8.1, simd construct, Description]
1648 // The parameter of the safelen clause must be a constant
1649 // positive integer expression.
1650 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1651 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001652 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001653 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001654 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001655}
1656
Alexander Musman64d33f12014-06-04 07:53:32 +00001657OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1658 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001659 SourceLocation LParenLoc,
1660 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001661 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001662 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001663 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001664 // The parameter of the collapse clause must be a constant
1665 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001666 ExprResult NumForLoopsResult =
1667 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1668 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001669 return nullptr;
1670 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001671 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001672}
1673
Alexey Bataeved09d242014-05-28 05:53:51 +00001674OMPClause *Sema::ActOnOpenMPSimpleClause(
1675 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1676 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001677 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001678 switch (Kind) {
1679 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001680 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001681 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1682 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001683 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001684 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001685 Res = ActOnOpenMPProcBindClause(
1686 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1687 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001688 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001689 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001690 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001691 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001692 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001693 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001694 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001695 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001696 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001697 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001698 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001699 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001700 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001701 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001702 case OMPC_threadprivate:
1703 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001704 llvm_unreachable("Clause is not allowed.");
1705 }
1706 return Res;
1707}
1708
1709OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1710 SourceLocation KindKwLoc,
1711 SourceLocation StartLoc,
1712 SourceLocation LParenLoc,
1713 SourceLocation EndLoc) {
1714 if (Kind == OMPC_DEFAULT_unknown) {
1715 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001716 static_assert(OMPC_DEFAULT_unknown > 0,
1717 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001718 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001719 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001720 Values += "'";
1721 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1722 Values += "'";
1723 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001724 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001725 Values += " or ";
1726 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001727 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001728 break;
1729 default:
1730 Values += Sep;
1731 break;
1732 }
1733 }
1734 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001735 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001736 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001737 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001738 switch (Kind) {
1739 case OMPC_DEFAULT_none:
1740 DSAStack->setDefaultDSANone();
1741 break;
1742 case OMPC_DEFAULT_shared:
1743 DSAStack->setDefaultDSAShared();
1744 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001745 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001746 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001747 break;
1748 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001749 return new (Context)
1750 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001751}
1752
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001753OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1754 SourceLocation KindKwLoc,
1755 SourceLocation StartLoc,
1756 SourceLocation LParenLoc,
1757 SourceLocation EndLoc) {
1758 if (Kind == OMPC_PROC_BIND_unknown) {
1759 std::string Values;
1760 std::string Sep(", ");
1761 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1762 Values += "'";
1763 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1764 Values += "'";
1765 switch (i) {
1766 case OMPC_PROC_BIND_unknown - 2:
1767 Values += " or ";
1768 break;
1769 case OMPC_PROC_BIND_unknown - 1:
1770 break;
1771 default:
1772 Values += Sep;
1773 break;
1774 }
1775 }
1776 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001777 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001778 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001779 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001780 return new (Context)
1781 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001782}
1783
Alexey Bataev56dafe82014-06-20 07:16:17 +00001784OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1785 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
1786 SourceLocation StartLoc, SourceLocation LParenLoc,
1787 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
1788 SourceLocation EndLoc) {
1789 OMPClause *Res = nullptr;
1790 switch (Kind) {
1791 case OMPC_schedule:
1792 Res = ActOnOpenMPScheduleClause(
1793 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
1794 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
1795 break;
1796 case OMPC_if:
1797 case OMPC_num_threads:
1798 case OMPC_safelen:
1799 case OMPC_collapse:
1800 case OMPC_default:
1801 case OMPC_proc_bind:
1802 case OMPC_private:
1803 case OMPC_firstprivate:
1804 case OMPC_lastprivate:
1805 case OMPC_shared:
1806 case OMPC_reduction:
1807 case OMPC_linear:
1808 case OMPC_aligned:
1809 case OMPC_copyin:
1810 case OMPC_threadprivate:
1811 case OMPC_unknown:
1812 llvm_unreachable("Clause is not allowed.");
1813 }
1814 return Res;
1815}
1816
1817OMPClause *Sema::ActOnOpenMPScheduleClause(
1818 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
1819 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
1820 SourceLocation EndLoc) {
1821 if (Kind == OMPC_SCHEDULE_unknown) {
1822 std::string Values;
1823 std::string Sep(", ");
1824 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
1825 Values += "'";
1826 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
1827 Values += "'";
1828 switch (i) {
1829 case OMPC_SCHEDULE_unknown - 2:
1830 Values += " or ";
1831 break;
1832 case OMPC_SCHEDULE_unknown - 1:
1833 break;
1834 default:
1835 Values += Sep;
1836 break;
1837 }
1838 }
1839 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
1840 << Values << getOpenMPClauseName(OMPC_schedule);
1841 return nullptr;
1842 }
1843 Expr *ValExpr = ChunkSize;
1844 if (ChunkSize) {
1845 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
1846 !ChunkSize->isInstantiationDependent() &&
1847 !ChunkSize->containsUnexpandedParameterPack()) {
1848 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
1849 ExprResult Val =
1850 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
1851 if (Val.isInvalid())
1852 return nullptr;
1853
1854 ValExpr = Val.get();
1855
1856 // OpenMP [2.7.1, Restrictions]
1857 // chunk_size must be a loop invariant integer expression with a positive
1858 // value.
1859 llvm::APSInt Result;
1860 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
1861 Result.isSigned() && !Result.isStrictlyPositive()) {
1862 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
1863 << "schedule" << ChunkSize->getSourceRange();
1864 return nullptr;
1865 }
1866 }
1867 }
1868
1869 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
1870 EndLoc, Kind, ValExpr);
1871}
1872
Alexey Bataevc5e02582014-06-16 07:08:35 +00001873OMPClause *Sema::ActOnOpenMPVarListClause(
1874 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
1875 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
1876 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
1877 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001878 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001879 switch (Kind) {
1880 case OMPC_private:
1881 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1882 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001883 case OMPC_firstprivate:
1884 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1885 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00001886 case OMPC_lastprivate:
1887 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1888 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001889 case OMPC_shared:
1890 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1891 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001892 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00001893 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
1894 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001895 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001896 case OMPC_linear:
1897 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1898 ColonLoc, EndLoc);
1899 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001900 case OMPC_aligned:
1901 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
1902 ColonLoc, EndLoc);
1903 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001904 case OMPC_copyin:
1905 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1906 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001907 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001908 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001909 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001910 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001911 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001912 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001913 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001914 case OMPC_threadprivate:
1915 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001916 llvm_unreachable("Clause is not allowed.");
1917 }
1918 return Res;
1919}
1920
1921OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1922 SourceLocation StartLoc,
1923 SourceLocation LParenLoc,
1924 SourceLocation EndLoc) {
1925 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001926 for (auto &RefExpr : VarList) {
1927 assert(RefExpr && "NULL expr in OpenMP private clause.");
1928 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001929 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001930 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001931 continue;
1932 }
1933
Alexey Bataeved09d242014-05-28 05:53:51 +00001934 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001935 // OpenMP [2.1, C/C++]
1936 // A list item is a variable name.
1937 // OpenMP [2.9.3.3, Restrictions, p.1]
1938 // A variable that is part of another variable (as an array or
1939 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001940 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001941 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001942 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001943 continue;
1944 }
1945 Decl *D = DE->getDecl();
1946 VarDecl *VD = cast<VarDecl>(D);
1947
1948 QualType Type = VD->getType();
1949 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1950 // It will be analyzed later.
1951 Vars.push_back(DE);
1952 continue;
1953 }
1954
1955 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1956 // A variable that appears in a private clause must not have an incomplete
1957 // type or a reference type.
1958 if (RequireCompleteType(ELoc, Type,
1959 diag::err_omp_private_incomplete_type)) {
1960 continue;
1961 }
1962 if (Type->isReferenceType()) {
1963 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001964 << getOpenMPClauseName(OMPC_private) << Type;
1965 bool IsDecl =
1966 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1967 Diag(VD->getLocation(),
1968 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1969 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001970 continue;
1971 }
1972
1973 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1974 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00001975 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001976 // class type.
1977 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001978 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
1979 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001980 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001981 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1982 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1983 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00001984 // FIXME This code must be replaced by actual constructing/destructing of
1985 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001986 if (RD) {
1987 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1988 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001989 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00001990 if (!CD ||
1991 CheckConstructorAccess(ELoc, CD,
1992 InitializedEntity::InitializeTemporary(Type),
1993 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001994 CD->isDeleted()) {
1995 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001996 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001997 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1998 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001999 Diag(VD->getLocation(),
2000 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2001 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002002 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2003 continue;
2004 }
2005 MarkFunctionReferenced(ELoc, CD);
2006 DiagnoseUseOfDecl(CD, ELoc);
2007
2008 CXXDestructorDecl *DD = RD->getDestructor();
2009 if (DD) {
2010 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2011 DD->isDeleted()) {
2012 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002013 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002014 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2015 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002016 Diag(VD->getLocation(),
2017 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2018 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002019 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2020 continue;
2021 }
2022 MarkFunctionReferenced(ELoc, DD);
2023 DiagnoseUseOfDecl(DD, ELoc);
2024 }
2025 }
2026
Alexey Bataev758e55e2013-09-06 18:03:48 +00002027 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2028 // in a Construct]
2029 // Variables with the predetermined data-sharing attributes may not be
2030 // listed in data-sharing attributes clauses, except for the cases
2031 // listed below. For these exceptions only, listing a predetermined
2032 // variable in a data-sharing attribute clause is allowed and overrides
2033 // the variable's predetermined data-sharing attributes.
2034 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2035 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002036 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2037 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002038 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002039 continue;
2040 }
2041
2042 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002043 Vars.push_back(DE);
2044 }
2045
Alexey Bataeved09d242014-05-28 05:53:51 +00002046 if (Vars.empty())
2047 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002048
2049 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2050}
2051
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002052OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2053 SourceLocation StartLoc,
2054 SourceLocation LParenLoc,
2055 SourceLocation EndLoc) {
2056 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002057 for (auto &RefExpr : VarList) {
2058 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2059 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002060 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002061 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002062 continue;
2063 }
2064
Alexey Bataeved09d242014-05-28 05:53:51 +00002065 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002066 // OpenMP [2.1, C/C++]
2067 // A list item is a variable name.
2068 // OpenMP [2.9.3.3, Restrictions, p.1]
2069 // A variable that is part of another variable (as an array or
2070 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002071 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002072 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002073 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002074 continue;
2075 }
2076 Decl *D = DE->getDecl();
2077 VarDecl *VD = cast<VarDecl>(D);
2078
2079 QualType Type = VD->getType();
2080 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2081 // It will be analyzed later.
2082 Vars.push_back(DE);
2083 continue;
2084 }
2085
2086 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2087 // A variable that appears in a private clause must not have an incomplete
2088 // type or a reference type.
2089 if (RequireCompleteType(ELoc, Type,
2090 diag::err_omp_firstprivate_incomplete_type)) {
2091 continue;
2092 }
2093 if (Type->isReferenceType()) {
2094 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002095 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2096 bool IsDecl =
2097 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2098 Diag(VD->getLocation(),
2099 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2100 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002101 continue;
2102 }
2103
2104 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2105 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002106 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002107 // class type.
2108 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002109 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2110 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2111 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002112 // FIXME This code must be replaced by actual constructing/destructing of
2113 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002114 if (RD) {
2115 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2116 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002117 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002118 if (!CD ||
2119 CheckConstructorAccess(ELoc, CD,
2120 InitializedEntity::InitializeTemporary(Type),
2121 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002122 CD->isDeleted()) {
2123 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002124 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002125 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2126 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002127 Diag(VD->getLocation(),
2128 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2129 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002130 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2131 continue;
2132 }
2133 MarkFunctionReferenced(ELoc, CD);
2134 DiagnoseUseOfDecl(CD, ELoc);
2135
2136 CXXDestructorDecl *DD = RD->getDestructor();
2137 if (DD) {
2138 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2139 DD->isDeleted()) {
2140 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002141 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002142 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2143 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002144 Diag(VD->getLocation(),
2145 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2146 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002147 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2148 continue;
2149 }
2150 MarkFunctionReferenced(ELoc, DD);
2151 DiagnoseUseOfDecl(DD, ELoc);
2152 }
2153 }
2154
2155 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2156 // variable and it was checked already.
2157 if (StartLoc.isValid() && EndLoc.isValid()) {
2158 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2159 Type = Type.getNonReferenceType().getCanonicalType();
2160 bool IsConstant = Type.isConstant(Context);
2161 Type = Context.getBaseElementType(Type);
2162 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2163 // A list item that specifies a given variable may not appear in more
2164 // than one clause on the same directive, except that a variable may be
2165 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002166 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002167 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002168 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002169 << getOpenMPClauseName(DVar.CKind)
2170 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002171 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002172 continue;
2173 }
2174
2175 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2176 // in a Construct]
2177 // Variables with the predetermined data-sharing attributes may not be
2178 // listed in data-sharing attributes clauses, except for the cases
2179 // listed below. For these exceptions only, listing a predetermined
2180 // variable in a data-sharing attribute clause is allowed and overrides
2181 // the variable's predetermined data-sharing attributes.
2182 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2183 // in a Construct, C/C++, p.2]
2184 // Variables with const-qualified type having no mutable member may be
2185 // listed in a firstprivate clause, even if they are static data members.
2186 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2187 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2188 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002189 << getOpenMPClauseName(DVar.CKind)
2190 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002191 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002192 continue;
2193 }
2194
Alexey Bataevf29276e2014-06-18 04:14:57 +00002195 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002196 // OpenMP [2.9.3.4, Restrictions, p.2]
2197 // A list item that is private within a parallel region must not appear
2198 // in a firstprivate clause on a worksharing construct if any of the
2199 // worksharing regions arising from the worksharing construct ever bind
2200 // to any of the parallel regions arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002201 if (isOpenMPWorksharingDirective(CurrDir)) {
2202 DVar = DSAStack->getImplicitDSA(VD);
2203 if (DVar.CKind != OMPC_shared) {
2204 Diag(ELoc, diag::err_omp_required_access)
2205 << getOpenMPClauseName(OMPC_firstprivate)
2206 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002207 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002208 continue;
2209 }
2210 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002211 // OpenMP [2.9.3.4, Restrictions, p.3]
2212 // A list item that appears in a reduction clause of a parallel construct
2213 // must not appear in a firstprivate clause on a worksharing or task
2214 // construct if any of the worksharing or task regions arising from the
2215 // worksharing or task construct ever bind to any of the parallel regions
2216 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002217 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002218 // OpenMP [2.9.3.4, Restrictions, p.4]
2219 // A list item that appears in a reduction clause in worksharing
2220 // construct must not appear in a firstprivate clause in a task construct
2221 // encountered during execution of any of the worksharing regions arising
2222 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002223 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002224 }
2225
2226 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2227 Vars.push_back(DE);
2228 }
2229
Alexey Bataeved09d242014-05-28 05:53:51 +00002230 if (Vars.empty())
2231 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002232
2233 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2234 Vars);
2235}
2236
Alexander Musman1bb328c2014-06-04 13:06:39 +00002237OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2238 SourceLocation StartLoc,
2239 SourceLocation LParenLoc,
2240 SourceLocation EndLoc) {
2241 SmallVector<Expr *, 8> Vars;
2242 for (auto &RefExpr : VarList) {
2243 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2244 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2245 // It will be analyzed later.
2246 Vars.push_back(RefExpr);
2247 continue;
2248 }
2249
2250 SourceLocation ELoc = RefExpr->getExprLoc();
2251 // OpenMP [2.1, C/C++]
2252 // A list item is a variable name.
2253 // OpenMP [2.14.3.5, Restrictions, p.1]
2254 // A variable that is part of another variable (as an array or structure
2255 // element) cannot appear in a lastprivate clause.
2256 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2257 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2258 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2259 continue;
2260 }
2261 Decl *D = DE->getDecl();
2262 VarDecl *VD = cast<VarDecl>(D);
2263
2264 QualType Type = VD->getType();
2265 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2266 // It will be analyzed later.
2267 Vars.push_back(DE);
2268 continue;
2269 }
2270
2271 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2272 // A variable that appears in a lastprivate clause must not have an
2273 // incomplete type or a reference type.
2274 if (RequireCompleteType(ELoc, Type,
2275 diag::err_omp_lastprivate_incomplete_type)) {
2276 continue;
2277 }
2278 if (Type->isReferenceType()) {
2279 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2280 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2281 bool IsDecl =
2282 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2283 Diag(VD->getLocation(),
2284 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2285 << VD;
2286 continue;
2287 }
2288
2289 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2290 // in a Construct]
2291 // Variables with the predetermined data-sharing attributes may not be
2292 // listed in data-sharing attributes clauses, except for the cases
2293 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002294 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2295 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2296 DVar.CKind != OMPC_firstprivate &&
2297 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2298 Diag(ELoc, diag::err_omp_wrong_dsa)
2299 << getOpenMPClauseName(DVar.CKind)
2300 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002301 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002302 continue;
2303 }
2304
Alexey Bataevf29276e2014-06-18 04:14:57 +00002305 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2306 // OpenMP [2.14.3.5, Restrictions, p.2]
2307 // A list item that is private within a parallel region, or that appears in
2308 // the reduction clause of a parallel construct, must not appear in a
2309 // lastprivate clause on a worksharing construct if any of the corresponding
2310 // worksharing regions ever binds to any of the corresponding parallel
2311 // regions.
2312 if (isOpenMPWorksharingDirective(CurrDir)) {
2313 DVar = DSAStack->getImplicitDSA(VD);
2314 if (DVar.CKind != OMPC_shared) {
2315 Diag(ELoc, diag::err_omp_required_access)
2316 << getOpenMPClauseName(OMPC_lastprivate)
2317 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002318 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002319 continue;
2320 }
2321 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002322 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002323 // A variable of class type (or array thereof) that appears in a
2324 // lastprivate clause requires an accessible, unambiguous default
2325 // constructor for the class type, unless the list item is also specified
2326 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002327 // A variable of class type (or array thereof) that appears in a
2328 // lastprivate clause requires an accessible, unambiguous copy assignment
2329 // operator for the class type.
2330 while (Type.getNonReferenceType()->isArrayType())
2331 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2332 ->getElementType();
2333 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2334 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2335 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002336 // FIXME This code must be replaced by actual copying and destructing of the
2337 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002338 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002339 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2340 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002341 if (MD) {
2342 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2343 MD->isDeleted()) {
2344 Diag(ELoc, diag::err_omp_required_method)
2345 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2346 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2347 VarDecl::DeclarationOnly;
2348 Diag(VD->getLocation(),
2349 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2350 << VD;
2351 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2352 continue;
2353 }
2354 MarkFunctionReferenced(ELoc, MD);
2355 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002356 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002357
2358 CXXDestructorDecl *DD = RD->getDestructor();
2359 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002360 PartialDiagnostic PD =
2361 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002362 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2363 DD->isDeleted()) {
2364 Diag(ELoc, diag::err_omp_required_method)
2365 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2366 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2367 VarDecl::DeclarationOnly;
2368 Diag(VD->getLocation(),
2369 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2370 << VD;
2371 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2372 continue;
2373 }
2374 MarkFunctionReferenced(ELoc, DD);
2375 DiagnoseUseOfDecl(DD, ELoc);
2376 }
2377 }
2378
Alexey Bataevf29276e2014-06-18 04:14:57 +00002379 if (DVar.CKind != OMPC_firstprivate)
2380 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002381 Vars.push_back(DE);
2382 }
2383
2384 if (Vars.empty())
2385 return nullptr;
2386
2387 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2388 Vars);
2389}
2390
Alexey Bataev758e55e2013-09-06 18:03:48 +00002391OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2392 SourceLocation StartLoc,
2393 SourceLocation LParenLoc,
2394 SourceLocation EndLoc) {
2395 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002396 for (auto &RefExpr : VarList) {
2397 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2398 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002399 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002400 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002401 continue;
2402 }
2403
Alexey Bataeved09d242014-05-28 05:53:51 +00002404 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002405 // OpenMP [2.1, C/C++]
2406 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002407 // OpenMP [2.14.3.2, Restrictions, p.1]
2408 // A variable that is part of another variable (as an array or structure
2409 // element) cannot appear in a shared unless it is a static data member
2410 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002411 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002412 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002413 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002414 continue;
2415 }
2416 Decl *D = DE->getDecl();
2417 VarDecl *VD = cast<VarDecl>(D);
2418
2419 QualType Type = VD->getType();
2420 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2421 // It will be analyzed later.
2422 Vars.push_back(DE);
2423 continue;
2424 }
2425
2426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2427 // in a Construct]
2428 // Variables with the predetermined data-sharing attributes may not be
2429 // listed in data-sharing attributes clauses, except for the cases
2430 // listed below. For these exceptions only, listing a predetermined
2431 // variable in a data-sharing attribute clause is allowed and overrides
2432 // the variable's predetermined data-sharing attributes.
2433 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002434 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2435 DVar.RefExpr) {
2436 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2437 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002438 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002439 continue;
2440 }
2441
2442 DSAStack->addDSA(VD, DE, OMPC_shared);
2443 Vars.push_back(DE);
2444 }
2445
Alexey Bataeved09d242014-05-28 05:53:51 +00002446 if (Vars.empty())
2447 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002448
2449 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2450}
2451
Alexey Bataevc5e02582014-06-16 07:08:35 +00002452namespace {
2453class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2454 DSAStackTy *Stack;
2455
2456public:
2457 bool VisitDeclRefExpr(DeclRefExpr *E) {
2458 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2459 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2460 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2461 return false;
2462 if (DVar.CKind != OMPC_unknown)
2463 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002464 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002465 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002466 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002467 return true;
2468 return false;
2469 }
2470 return false;
2471 }
2472 bool VisitStmt(Stmt *S) {
2473 for (auto Child : S->children()) {
2474 if (Child && Visit(Child))
2475 return true;
2476 }
2477 return false;
2478 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002479 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002480};
Alexey Bataev23b69422014-06-18 07:08:49 +00002481} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002482
2483OMPClause *Sema::ActOnOpenMPReductionClause(
2484 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2485 SourceLocation ColonLoc, SourceLocation EndLoc,
2486 CXXScopeSpec &ReductionIdScopeSpec,
2487 const DeclarationNameInfo &ReductionId) {
2488 // TODO: Allow scope specification search when 'declare reduction' is
2489 // supported.
2490 assert(ReductionIdScopeSpec.isEmpty() &&
2491 "No support for scoped reduction identifiers yet.");
2492
2493 auto DN = ReductionId.getName();
2494 auto OOK = DN.getCXXOverloadedOperator();
2495 BinaryOperatorKind BOK = BO_Comma;
2496
2497 // OpenMP [2.14.3.6, reduction clause]
2498 // C
2499 // reduction-identifier is either an identifier or one of the following
2500 // operators: +, -, *, &, |, ^, && and ||
2501 // C++
2502 // reduction-identifier is either an id-expression or one of the following
2503 // operators: +, -, *, &, |, ^, && and ||
2504 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2505 switch (OOK) {
2506 case OO_Plus:
2507 case OO_Minus:
2508 BOK = BO_AddAssign;
2509 break;
2510 case OO_Star:
2511 BOK = BO_MulAssign;
2512 break;
2513 case OO_Amp:
2514 BOK = BO_AndAssign;
2515 break;
2516 case OO_Pipe:
2517 BOK = BO_OrAssign;
2518 break;
2519 case OO_Caret:
2520 BOK = BO_XorAssign;
2521 break;
2522 case OO_AmpAmp:
2523 BOK = BO_LAnd;
2524 break;
2525 case OO_PipePipe:
2526 BOK = BO_LOr;
2527 break;
2528 default:
2529 if (auto II = DN.getAsIdentifierInfo()) {
2530 if (II->isStr("max"))
2531 BOK = BO_GT;
2532 else if (II->isStr("min"))
2533 BOK = BO_LT;
2534 }
2535 break;
2536 }
2537 SourceRange ReductionIdRange;
2538 if (ReductionIdScopeSpec.isValid()) {
2539 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2540 }
2541 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2542 if (BOK == BO_Comma) {
2543 // Not allowed reduction identifier is found.
2544 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2545 << ReductionIdRange;
2546 return nullptr;
2547 }
2548
2549 SmallVector<Expr *, 8> Vars;
2550 for (auto RefExpr : VarList) {
2551 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2552 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2553 // It will be analyzed later.
2554 Vars.push_back(RefExpr);
2555 continue;
2556 }
2557
2558 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2559 RefExpr->isInstantiationDependent() ||
2560 RefExpr->containsUnexpandedParameterPack()) {
2561 // It will be analyzed later.
2562 Vars.push_back(RefExpr);
2563 continue;
2564 }
2565
2566 auto ELoc = RefExpr->getExprLoc();
2567 auto ERange = RefExpr->getSourceRange();
2568 // OpenMP [2.1, C/C++]
2569 // A list item is a variable or array section, subject to the restrictions
2570 // specified in Section 2.4 on page 42 and in each of the sections
2571 // describing clauses and directives for which a list appears.
2572 // OpenMP [2.14.3.3, Restrictions, p.1]
2573 // A variable that is part of another variable (as an array or
2574 // structure element) cannot appear in a private clause.
2575 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2576 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2577 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2578 continue;
2579 }
2580 auto D = DE->getDecl();
2581 auto VD = cast<VarDecl>(D);
2582 auto Type = VD->getType();
2583 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2584 // A variable that appears in a private clause must not have an incomplete
2585 // type or a reference type.
2586 if (RequireCompleteType(ELoc, Type,
2587 diag::err_omp_reduction_incomplete_type))
2588 continue;
2589 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2590 // Arrays may not appear in a reduction clause.
2591 if (Type.getNonReferenceType()->isArrayType()) {
2592 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2593 bool IsDecl =
2594 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2595 Diag(VD->getLocation(),
2596 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2597 << VD;
2598 continue;
2599 }
2600 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2601 // A list item that appears in a reduction clause must not be
2602 // const-qualified.
2603 if (Type.getNonReferenceType().isConstant(Context)) {
2604 Diag(ELoc, diag::err_omp_const_variable)
2605 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2606 bool IsDecl =
2607 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2608 Diag(VD->getLocation(),
2609 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2610 << VD;
2611 continue;
2612 }
2613 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2614 // If a list-item is a reference type then it must bind to the same object
2615 // for all threads of the team.
2616 VarDecl *VDDef = VD->getDefinition();
2617 if (Type->isReferenceType() && VDDef) {
2618 DSARefChecker Check(DSAStack);
2619 if (Check.Visit(VDDef->getInit())) {
2620 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2621 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2622 continue;
2623 }
2624 }
2625 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2626 // The type of a list item that appears in a reduction clause must be valid
2627 // for the reduction-identifier. For a max or min reduction in C, the type
2628 // of the list item must be an allowed arithmetic data type: char, int,
2629 // float, double, or _Bool, possibly modified with long, short, signed, or
2630 // unsigned. For a max or min reduction in C++, the type of the list item
2631 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2632 // double, or bool, possibly modified with long, short, signed, or unsigned.
2633 if ((BOK == BO_GT || BOK == BO_LT) &&
2634 !(Type->isScalarType() ||
2635 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2636 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2637 << getLangOpts().CPlusPlus;
2638 bool IsDecl =
2639 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2640 Diag(VD->getLocation(),
2641 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2642 << VD;
2643 continue;
2644 }
2645 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2646 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2647 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2648 bool IsDecl =
2649 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2650 Diag(VD->getLocation(),
2651 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2652 << VD;
2653 continue;
2654 }
2655 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2656 getDiagnostics().setSuppressAllDiagnostics(true);
2657 ExprResult ReductionOp =
2658 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2659 RefExpr, RefExpr);
2660 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2661 if (ReductionOp.isInvalid()) {
2662 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002663 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002664 bool IsDecl =
2665 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2666 Diag(VD->getLocation(),
2667 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2668 << VD;
2669 continue;
2670 }
2671
2672 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2673 // in a Construct]
2674 // Variables with the predetermined data-sharing attributes may not be
2675 // listed in data-sharing attributes clauses, except for the cases
2676 // listed below. For these exceptions only, listing a predetermined
2677 // variable in a data-sharing attribute clause is allowed and overrides
2678 // the variable's predetermined data-sharing attributes.
2679 // OpenMP [2.14.3.6, Restrictions, p.3]
2680 // Any number of reduction clauses can be specified on the directive,
2681 // but a list item can appear only once in the reduction clauses for that
2682 // directive.
2683 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2684 if (DVar.CKind == OMPC_reduction) {
2685 Diag(ELoc, diag::err_omp_once_referenced)
2686 << getOpenMPClauseName(OMPC_reduction);
2687 if (DVar.RefExpr) {
2688 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2689 }
2690 } else if (DVar.CKind != OMPC_unknown) {
2691 Diag(ELoc, diag::err_omp_wrong_dsa)
2692 << getOpenMPClauseName(DVar.CKind)
2693 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002694 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002695 continue;
2696 }
2697
2698 // OpenMP [2.14.3.6, Restrictions, p.1]
2699 // A list item that appears in a reduction clause of a worksharing
2700 // construct must be shared in the parallel regions to which any of the
2701 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002702 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2703 if (isOpenMPWorksharingDirective(CurrDir)) {
2704 DVar = DSAStack->getImplicitDSA(VD);
2705 if (DVar.CKind != OMPC_shared) {
2706 Diag(ELoc, diag::err_omp_required_access)
2707 << getOpenMPClauseName(OMPC_reduction)
2708 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002709 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002710 continue;
2711 }
2712 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00002713
2714 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2715 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2716 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002717 // FIXME This code must be replaced by actual constructing/destructing of
2718 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00002719 if (RD) {
2720 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2721 PartialDiagnostic PD =
2722 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00002723 if (!CD ||
2724 CheckConstructorAccess(ELoc, CD,
2725 InitializedEntity::InitializeTemporary(Type),
2726 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00002727 CD->isDeleted()) {
2728 Diag(ELoc, diag::err_omp_required_method)
2729 << getOpenMPClauseName(OMPC_reduction) << 0;
2730 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2731 VarDecl::DeclarationOnly;
2732 Diag(VD->getLocation(),
2733 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2734 << VD;
2735 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2736 continue;
2737 }
2738 MarkFunctionReferenced(ELoc, CD);
2739 DiagnoseUseOfDecl(CD, ELoc);
2740
2741 CXXDestructorDecl *DD = RD->getDestructor();
2742 if (DD) {
2743 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2744 DD->isDeleted()) {
2745 Diag(ELoc, diag::err_omp_required_method)
2746 << getOpenMPClauseName(OMPC_reduction) << 4;
2747 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2748 VarDecl::DeclarationOnly;
2749 Diag(VD->getLocation(),
2750 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2751 << VD;
2752 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2753 continue;
2754 }
2755 MarkFunctionReferenced(ELoc, DD);
2756 DiagnoseUseOfDecl(DD, ELoc);
2757 }
2758 }
2759
2760 DSAStack->addDSA(VD, DE, OMPC_reduction);
2761 Vars.push_back(DE);
2762 }
2763
2764 if (Vars.empty())
2765 return nullptr;
2766
2767 return OMPReductionClause::Create(
2768 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
2769 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
2770}
2771
Alexander Musman8dba6642014-04-22 13:09:42 +00002772OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2773 SourceLocation StartLoc,
2774 SourceLocation LParenLoc,
2775 SourceLocation ColonLoc,
2776 SourceLocation EndLoc) {
2777 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002778 for (auto &RefExpr : VarList) {
2779 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2780 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002781 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002782 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002783 continue;
2784 }
2785
2786 // OpenMP [2.14.3.7, linear clause]
2787 // A list item that appears in a linear clause is subject to the private
2788 // clause semantics described in Section 2.14.3.3 on page 159 except as
2789 // noted. In addition, the value of the new list item on each iteration
2790 // of the associated loop(s) corresponds to the value of the original
2791 // list item before entering the construct plus the logical number of
2792 // the iteration times linear-step.
2793
Alexey Bataeved09d242014-05-28 05:53:51 +00002794 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00002795 // OpenMP [2.1, C/C++]
2796 // A list item is a variable name.
2797 // OpenMP [2.14.3.3, Restrictions, p.1]
2798 // A variable that is part of another variable (as an array or
2799 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002800 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002801 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002802 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00002803 continue;
2804 }
2805
2806 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2807
2808 // OpenMP [2.14.3.7, linear clause]
2809 // A list-item cannot appear in more than one linear clause.
2810 // A list-item that appears in a linear clause cannot appear in any
2811 // other data-sharing attribute clause.
2812 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2813 if (DVar.RefExpr) {
2814 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2815 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002816 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00002817 continue;
2818 }
2819
2820 QualType QType = VD->getType();
2821 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2822 // It will be analyzed later.
2823 Vars.push_back(DE);
2824 continue;
2825 }
2826
2827 // A variable must not have an incomplete type or a reference type.
2828 if (RequireCompleteType(ELoc, QType,
2829 diag::err_omp_linear_incomplete_type)) {
2830 continue;
2831 }
2832 if (QType->isReferenceType()) {
2833 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2834 << getOpenMPClauseName(OMPC_linear) << QType;
2835 bool IsDecl =
2836 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2837 Diag(VD->getLocation(),
2838 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2839 << VD;
2840 continue;
2841 }
2842
2843 // A list item must not be const-qualified.
2844 if (QType.isConstant(Context)) {
2845 Diag(ELoc, diag::err_omp_const_variable)
2846 << getOpenMPClauseName(OMPC_linear);
2847 bool IsDecl =
2848 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2849 Diag(VD->getLocation(),
2850 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2851 << VD;
2852 continue;
2853 }
2854
2855 // A list item must be of integral or pointer type.
2856 QType = QType.getUnqualifiedType().getCanonicalType();
2857 const Type *Ty = QType.getTypePtrOrNull();
2858 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
2859 !Ty->isPointerType())) {
2860 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
2861 bool IsDecl =
2862 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2863 Diag(VD->getLocation(),
2864 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2865 << VD;
2866 continue;
2867 }
2868
2869 DSAStack->addDSA(VD, DE, OMPC_linear);
2870 Vars.push_back(DE);
2871 }
2872
2873 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002874 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00002875
2876 Expr *StepExpr = Step;
2877 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2878 !Step->isInstantiationDependent() &&
2879 !Step->containsUnexpandedParameterPack()) {
2880 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002881 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00002882 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002883 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002884 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00002885
2886 // Warn about zero linear step (it would be probably better specified as
2887 // making corresponding variables 'const').
2888 llvm::APSInt Result;
2889 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
2890 !Result.isNegative() && !Result.isStrictlyPositive())
2891 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
2892 << (Vars.size() > 1);
2893 }
2894
2895 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
2896 Vars, StepExpr);
2897}
2898
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002899OMPClause *Sema::ActOnOpenMPAlignedClause(
2900 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
2901 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
2902
2903 SmallVector<Expr *, 8> Vars;
2904 for (auto &RefExpr : VarList) {
2905 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
2906 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2907 // It will be analyzed later.
2908 Vars.push_back(RefExpr);
2909 continue;
2910 }
2911
2912 SourceLocation ELoc = RefExpr->getExprLoc();
2913 // OpenMP [2.1, C/C++]
2914 // A list item is a variable name.
2915 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
2916 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2917 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2918 continue;
2919 }
2920
2921 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2922
2923 // OpenMP [2.8.1, simd construct, Restrictions]
2924 // The type of list items appearing in the aligned clause must be
2925 // array, pointer, reference to array, or reference to pointer.
2926 QualType QType = DE->getType()
2927 .getNonReferenceType()
2928 .getUnqualifiedType()
2929 .getCanonicalType();
2930 const Type *Ty = QType.getTypePtrOrNull();
2931 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
2932 !Ty->isPointerType())) {
2933 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
2934 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
2935 bool IsDecl =
2936 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2937 Diag(VD->getLocation(),
2938 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2939 << VD;
2940 continue;
2941 }
2942
2943 // OpenMP [2.8.1, simd construct, Restrictions]
2944 // A list-item cannot appear in more than one aligned clause.
2945 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
2946 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
2947 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
2948 << getOpenMPClauseName(OMPC_aligned);
2949 continue;
2950 }
2951
2952 Vars.push_back(DE);
2953 }
2954
2955 // OpenMP [2.8.1, simd construct, Description]
2956 // The parameter of the aligned clause, alignment, must be a constant
2957 // positive integer expression.
2958 // If no optional parameter is specified, implementation-defined default
2959 // alignments for SIMD instructions on the target platforms are assumed.
2960 if (Alignment != nullptr) {
2961 ExprResult AlignResult =
2962 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
2963 if (AlignResult.isInvalid())
2964 return nullptr;
2965 Alignment = AlignResult.get();
2966 }
2967 if (Vars.empty())
2968 return nullptr;
2969
2970 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
2971 EndLoc, Vars, Alignment);
2972}
2973
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002974OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
2975 SourceLocation StartLoc,
2976 SourceLocation LParenLoc,
2977 SourceLocation EndLoc) {
2978 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002979 for (auto &RefExpr : VarList) {
2980 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
2981 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002982 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002983 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002984 continue;
2985 }
2986
Alexey Bataeved09d242014-05-28 05:53:51 +00002987 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002988 // OpenMP [2.1, C/C++]
2989 // A list item is a variable name.
2990 // OpenMP [2.14.4.1, Restrictions, p.1]
2991 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00002992 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002993 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002994 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002995 continue;
2996 }
2997
2998 Decl *D = DE->getDecl();
2999 VarDecl *VD = cast<VarDecl>(D);
3000
3001 QualType Type = VD->getType();
3002 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3003 // It will be analyzed later.
3004 Vars.push_back(DE);
3005 continue;
3006 }
3007
3008 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3009 // A list item that appears in a copyin clause must be threadprivate.
3010 if (!DSAStack->isThreadPrivate(VD)) {
3011 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003012 << getOpenMPClauseName(OMPC_copyin)
3013 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003014 continue;
3015 }
3016
3017 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3018 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003019 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003020 // operator for the class type.
3021 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003022 CXXRecordDecl *RD =
3023 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003024 // FIXME This code must be replaced by actual assignment of the
3025 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003026 if (RD) {
3027 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3028 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003029 if (MD) {
3030 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3031 MD->isDeleted()) {
3032 Diag(ELoc, diag::err_omp_required_method)
3033 << getOpenMPClauseName(OMPC_copyin) << 2;
3034 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3035 VarDecl::DeclarationOnly;
3036 Diag(VD->getLocation(),
3037 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3038 << VD;
3039 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3040 continue;
3041 }
3042 MarkFunctionReferenced(ELoc, MD);
3043 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003044 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003045 }
3046
3047 DSAStack->addDSA(VD, DE, OMPC_copyin);
3048 Vars.push_back(DE);
3049 }
3050
Alexey Bataeved09d242014-05-28 05:53:51 +00003051 if (Vars.empty())
3052 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003053
3054 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3055}
3056
Alexey Bataev758e55e2013-09-06 18:03:48 +00003057#undef DSAStack