blob: 0dfec2669203d39edc0edabb90e3f8220bf0a78d [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;
Alexey Bataevbae9a792014-06-27 10:37:06 +000071 SourceLocation ImplicitDSALoc;
72 DSAVarData()
73 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
74 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000075 };
Alexey Bataeved09d242014-05-28 05:53:51 +000076
Alexey Bataev758e55e2013-09-06 18:03:48 +000077private:
78 struct DSAInfo {
79 OpenMPClauseKind Attributes;
80 DeclRefExpr *RefExpr;
81 };
82 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000083 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000084
85 struct SharingMapTy {
86 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000087 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 OpenMPDirectiveKind Directive;
91 DeclarationNameInfo DirectiveName;
92 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +000094 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000095 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000096 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +000097 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
98 ConstructLoc(Loc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000099 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000100 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000101 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
102 ConstructLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 };
104
105 typedef SmallVector<SharingMapTy, 64> StackTy;
106
107 /// \brief Stack of used declaration and their data-sharing attributes.
108 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000109 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000110
111 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
112
113 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000114
115 /// \brief Checks if the variable is a local for OpenMP region.
116 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000117
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000119 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120
121 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000122 Scope *CurScope, SourceLocation Loc) {
123 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
124 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000125 }
126
127 void pop() {
128 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
129 Stack.pop_back();
130 }
131
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000132 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000133 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000134 /// for diagnostics.
135 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
136
Alexey Bataev758e55e2013-09-06 18:03:48 +0000137 /// \brief Adds explicit data sharing attribute to the specified declaration.
138 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 /// \brief Returns data sharing attributes from top of the stack for the
141 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000142 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000143 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000144 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000145 /// \brief Checks if the specified variables has data-sharing attributes which
146 /// match specified \a CPred predicate in any directive which matches \a DPred
147 /// predicate.
148 template <class ClausesPredicate, class DirectivesPredicate>
149 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000150 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000151 /// \brief Checks if the specified variables has data-sharing attributes which
152 /// match specified \a CPred predicate in any innermost directive which
153 /// matches \a DPred predicate.
154 template <class ClausesPredicate, class DirectivesPredicate>
155 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000156 DirectivesPredicate DPred,
157 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000158
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 /// \brief Returns currently analyzed directive.
160 OpenMPDirectiveKind getCurrentDirective() const {
161 return Stack.back().Directive;
162 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000163 /// \brief Returns parent directive.
164 OpenMPDirectiveKind getParentDirective() const {
165 if (Stack.size() > 2)
166 return Stack[Stack.size() - 2].Directive;
167 return OMPD_unknown;
168 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000169
170 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000171 void setDefaultDSANone(SourceLocation Loc) {
172 Stack.back().DefaultAttr = DSA_none;
173 Stack.back().DefaultAttrLoc = Loc;
174 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000176 void setDefaultDSAShared(SourceLocation Loc) {
177 Stack.back().DefaultAttr = DSA_shared;
178 Stack.back().DefaultAttrLoc = Loc;
179 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180
181 DefaultDataSharingAttributes getDefaultDSA() const {
182 return Stack.back().DefaultAttr;
183 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000184 SourceLocation getDefaultDSALocation() const {
185 return Stack.back().DefaultAttrLoc;
186 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000187
Alexey Bataevf29276e2014-06-18 04:14:57 +0000188 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000189 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000190 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000192 }
193
194 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000195 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000196 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000198bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
199 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
200 DKind == OMPD_unknown;
201}
Alexey Bataeved09d242014-05-28 05:53:51 +0000202} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203
204DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
205 VarDecl *D) {
206 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000207 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000208 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
209 // in a region but not in construct]
210 // File-scope or namespace-scope variables referenced in called routines
211 // in the region are shared unless they appear in a threadprivate
212 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000213 if (!D->isFunctionOrMethodVarDecl())
214 DVar.CKind = OMPC_shared;
215
216 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
217 // in a region but not in construct]
218 // Variables with static storage duration that are declared in called
219 // routines in the region are shared.
220 if (D->hasGlobalStorage())
221 DVar.CKind = OMPC_shared;
222
Alexey Bataev758e55e2013-09-06 18:03:48 +0000223 return DVar;
224 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000225
Alexey Bataev758e55e2013-09-06 18:03:48 +0000226 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000227 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
228 // in a Construct, C/C++, predetermined, p.1]
229 // Variables with automatic storage duration that are declared in a scope
230 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000231 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
232 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
233 DVar.CKind = OMPC_private;
234 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000235 }
236
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237 // Explicitly specified attributes and local variables with predetermined
238 // attributes.
239 if (Iter->SharingMap.count(D)) {
240 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
241 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000242 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000243 return DVar;
244 }
245
246 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
247 // in a Construct, C/C++, implicitly determined, p.1]
248 // In a parallel or task construct, the data-sharing attributes of these
249 // variables are determined by the default clause, if present.
250 switch (Iter->DefaultAttr) {
251 case DSA_shared:
252 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000253 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000254 return DVar;
255 case DSA_none:
256 return DVar;
257 case DSA_unspecified:
258 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
259 // in a Construct, implicitly determined, p.2]
260 // In a parallel construct, if no default clause is present, these
261 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000262 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000263 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264 DVar.CKind = OMPC_shared;
265 return DVar;
266 }
267
268 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
269 // in a Construct, implicitly determined, p.4]
270 // In a task construct, if no default clause is present, a variable that in
271 // the enclosing context is determined to be shared by all implicit tasks
272 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000273 if (DVar.DKind == OMPD_task) {
274 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000275 for (StackTy::reverse_iterator I = std::next(Iter),
276 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000277 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000278 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
279 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 // in a Construct, implicitly determined, p.6]
281 // In a task construct, if no default clause is present, a variable
282 // whose data-sharing attribute is not determined by the rules above is
283 // firstprivate.
284 DVarTemp = getDSA(I, D);
285 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000286 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000287 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000288 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 return DVar;
290 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000291 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000292 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000293 }
294 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000295 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000296 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000297 return DVar;
298 }
299 }
300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
301 // in a Construct, implicitly determined, p.3]
302 // For constructs other than task, if no default clause is present, these
303 // variables inherit their data-sharing attributes from the enclosing
304 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000305 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306}
307
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000308DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
309 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
310 auto It = Stack.back().AlignedMap.find(D);
311 if (It == Stack.back().AlignedMap.end()) {
312 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
313 Stack.back().AlignedMap[D] = NewDE;
314 return nullptr;
315 } else {
316 assert(It->second && "Unexpected nullptr expr in the aligned map");
317 return It->second;
318 }
319 return nullptr;
320}
321
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
323 if (A == OMPC_threadprivate) {
324 Stack[0].SharingMap[D].Attributes = A;
325 Stack[0].SharingMap[D].RefExpr = E;
326 } else {
327 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
328 Stack.back().SharingMap[D].Attributes = A;
329 Stack.back().SharingMap[D].RefExpr = E;
330 }
331}
332
Alexey Bataeved09d242014-05-28 05:53:51 +0000333bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000334 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000335 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000336 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000337 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000338 ++I;
339 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000340 if (I == E)
341 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000342 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000343 Scope *CurScope = getCurScope();
344 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000346 }
347 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000348 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000349 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000350}
351
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000352DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000353 DSAVarData DVar;
354
355 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
356 // in a Construct, C/C++, predetermined, p.1]
357 // Variables appearing in threadprivate directives are threadprivate.
358 if (D->getTLSKind() != VarDecl::TLS_None) {
359 DVar.CKind = OMPC_threadprivate;
360 return DVar;
361 }
362 if (Stack[0].SharingMap.count(D)) {
363 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
364 DVar.CKind = OMPC_threadprivate;
365 return DVar;
366 }
367
368 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
369 // in a Construct, C/C++, predetermined, p.1]
370 // Variables with automatic storage duration that are declared in a scope
371 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000372 OpenMPDirectiveKind Kind =
373 FromParent ? getParentDirective() : getCurrentDirective();
374 auto StartI = std::next(Stack.rbegin());
375 auto EndI = std::prev(Stack.rend());
376 if (FromParent && StartI != EndI) {
377 StartI = std::next(StartI);
378 }
379 if (!isParallelOrTaskRegion(Kind)) {
380 if (isOpenMPLocal(D, StartI) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000381 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000382 DVar.CKind = OMPC_private;
383 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000384 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385 }
386
387 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
388 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000389 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000390 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000391 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000392 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000393 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
394 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000395 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
396 return DVar;
397
Alexey Bataev758e55e2013-09-06 18:03:48 +0000398 DVar.CKind = OMPC_shared;
399 return DVar;
400 }
401
402 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000403 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 while (Type->isArrayType()) {
405 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
406 Type = ElemType.getNonReferenceType().getCanonicalType();
407 }
408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
409 // in a Construct, C/C++, predetermined, p.6]
410 // Variables with const qualified type having no mutable member are
411 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000412 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000413 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000415 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000416 // Variables with const-qualified type having no mutable member may be
417 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000418 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
419 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000420 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
421 return DVar;
422
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 DVar.CKind = OMPC_shared;
424 return DVar;
425 }
426
427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
428 // in a Construct, C/C++, predetermined, p.7]
429 // Variables with static storage duration that are declared in a scope
430 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000431 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432 DVar.CKind = OMPC_shared;
433 return DVar;
434 }
435
436 // Explicitly specified attributes and local variables with predetermined
437 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000438 auto I = std::prev(StartI);
439 if (I->SharingMap.count(D)) {
440 DVar.RefExpr = I->SharingMap[D].RefExpr;
441 DVar.CKind = I->SharingMap[D].Attributes;
442 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 }
444
445 return DVar;
446}
447
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000448DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
449 auto StartI = Stack.rbegin();
450 auto EndI = std::prev(Stack.rend());
451 if (FromParent && StartI != EndI) {
452 StartI = std::next(StartI);
453 }
454 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000455}
456
Alexey Bataevf29276e2014-06-18 04:14:57 +0000457template <class ClausesPredicate, class DirectivesPredicate>
458DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000459 DirectivesPredicate DPred,
460 bool FromParent) {
461 auto StartI = std::next(Stack.rbegin());
462 auto EndI = std::prev(Stack.rend());
463 if (FromParent && StartI != EndI) {
464 StartI = std::next(StartI);
465 }
466 for (auto I = StartI, EE = EndI; I != EE; ++I) {
467 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000468 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000469 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000470 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000471 return DVar;
472 }
473 return DSAVarData();
474}
475
Alexey Bataevf29276e2014-06-18 04:14:57 +0000476template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000477DSAStackTy::DSAVarData
478DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
479 DirectivesPredicate DPred, bool FromParent) {
480 auto StartI = std::next(Stack.rbegin());
481 auto EndI = std::prev(Stack.rend());
482 if (FromParent && StartI != EndI) {
483 StartI = std::next(StartI);
484 }
485 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000486 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000487 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000488 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000489 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000490 return DVar;
491 return DSAVarData();
492 }
493 return DSAVarData();
494}
495
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496void Sema::InitDataSharingAttributesStack() {
497 VarDataSharingAttributesStack = new DSAStackTy(*this);
498}
499
500#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
501
Alexey Bataeved09d242014-05-28 05:53:51 +0000502void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503
504void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
505 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000506 Scope *CurScope, SourceLocation Loc) {
507 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000508 PushExpressionEvaluationContext(PotentiallyEvaluated);
509}
510
511void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000512 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
513 // A variable of class type (or array thereof) that appears in a lastprivate
514 // clause requires an accessible, unambiguous default constructor for the
515 // class type, unless the list item is also specified in a firstprivate
516 // clause.
517 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
518 for (auto C : D->clauses()) {
519 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
520 for (auto VarRef : Clause->varlists()) {
521 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
522 continue;
523 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000524 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000525 if (DVar.CKind == OMPC_lastprivate) {
526 SourceLocation ELoc = VarRef->getExprLoc();
527 auto Type = VarRef->getType();
528 if (Type->isArrayType())
529 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
530 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000531 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
532 // FIXME This code must be replaced by actual constructing of the
533 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000534 if (RD) {
535 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
536 PartialDiagnostic PD =
537 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
538 if (!CD ||
539 CheckConstructorAccess(
540 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
541 CD->getAccess(), PD) == AR_inaccessible ||
542 CD->isDeleted()) {
543 Diag(ELoc, diag::err_omp_required_method)
544 << getOpenMPClauseName(OMPC_lastprivate) << 0;
545 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
546 VarDecl::DeclarationOnly;
547 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
548 : diag::note_defined_here)
549 << VD;
550 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
551 continue;
552 }
553 MarkFunctionReferenced(ELoc, CD);
554 DiagnoseUseOfDecl(CD, ELoc);
555 }
556 }
557 }
558 }
559 }
560 }
561
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562 DSAStack->pop();
563 DiscardCleanupsInEvaluationContext();
564 PopExpressionEvaluationContext();
565}
566
Alexey Bataeva769e072013-03-22 06:34:35 +0000567namespace {
568
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000569class VarDeclFilterCCC : public CorrectionCandidateCallback {
570private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000571 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000572
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000573public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000574 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000575 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000576 NamedDecl *ND = Candidate.getCorrectionDecl();
577 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
578 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000579 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
580 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000581 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000582 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000583 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000584};
Alexey Bataeved09d242014-05-28 05:53:51 +0000585} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000586
587ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
588 CXXScopeSpec &ScopeSpec,
589 const DeclarationNameInfo &Id) {
590 LookupResult Lookup(*this, Id, LookupOrdinaryName);
591 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
592
593 if (Lookup.isAmbiguous())
594 return ExprError();
595
596 VarDecl *VD;
597 if (!Lookup.isSingleResult()) {
598 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000599 if (TypoCorrection Corrected =
600 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
601 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000602 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000603 PDiag(Lookup.empty()
604 ? diag::err_undeclared_var_use_suggest
605 : diag::err_omp_expected_var_arg_suggest)
606 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000607 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000608 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000609 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
610 : diag::err_omp_expected_var_arg)
611 << Id.getName();
612 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614 } else {
615 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000616 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000617 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
618 return ExprError();
619 }
620 }
621 Lookup.suppressDiagnostics();
622
623 // OpenMP [2.9.2, Syntax, C/C++]
624 // Variables must be file-scope, namespace-scope, or static block-scope.
625 if (!VD->hasGlobalStorage()) {
626 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000627 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
628 bool IsDecl =
629 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000630 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000631 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
632 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000633 return ExprError();
634 }
635
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000636 VarDecl *CanonicalVD = VD->getCanonicalDecl();
637 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000638 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
639 // A threadprivate directive for file-scope variables must appear outside
640 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000641 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
642 !getCurLexicalContext()->isTranslationUnit()) {
643 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000644 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
645 bool IsDecl =
646 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
647 Diag(VD->getLocation(),
648 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
649 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000650 return ExprError();
651 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000652 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
653 // A threadprivate directive for static class member variables must appear
654 // in the class definition, in the same scope in which the member
655 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000656 if (CanonicalVD->isStaticDataMember() &&
657 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
658 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000659 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
660 bool IsDecl =
661 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
662 Diag(VD->getLocation(),
663 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
664 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000665 return ExprError();
666 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000667 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
668 // A threadprivate directive for namespace-scope variables must appear
669 // outside any definition or declaration other than the namespace
670 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000671 if (CanonicalVD->getDeclContext()->isNamespace() &&
672 (!getCurLexicalContext()->isFileContext() ||
673 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
674 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000675 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
676 bool IsDecl =
677 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
678 Diag(VD->getLocation(),
679 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
680 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000681 return ExprError();
682 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000683 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
684 // A threadprivate directive for static block-scope variables must appear
685 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000686 if (CanonicalVD->isStaticLocal() && CurScope &&
687 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000688 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000689 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
690 bool IsDecl =
691 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
692 Diag(VD->getLocation(),
693 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
694 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000695 return ExprError();
696 }
697
698 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
699 // A threadprivate directive must lexically precede all references to any
700 // of the variables in its list.
701 if (VD->isUsed()) {
702 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000703 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000704 return ExprError();
705 }
706
707 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000708 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000709 return DE;
710}
711
Alexey Bataeved09d242014-05-28 05:53:51 +0000712Sema::DeclGroupPtrTy
713Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
714 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000715 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000716 CurContext->addDecl(D);
717 return DeclGroupPtrTy::make(DeclGroupRef(D));
718 }
719 return DeclGroupPtrTy();
720}
721
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000722namespace {
723class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
724 Sema &SemaRef;
725
726public:
727 bool VisitDeclRefExpr(const DeclRefExpr *E) {
728 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
729 if (VD->hasLocalStorage()) {
730 SemaRef.Diag(E->getLocStart(),
731 diag::err_omp_local_var_in_threadprivate_init)
732 << E->getSourceRange();
733 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
734 << VD << VD->getSourceRange();
735 return true;
736 }
737 }
738 return false;
739 }
740 bool VisitStmt(const Stmt *S) {
741 for (auto Child : S->children()) {
742 if (Child && Visit(Child))
743 return true;
744 }
745 return false;
746 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000747 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000748};
749} // namespace
750
Alexey Bataeved09d242014-05-28 05:53:51 +0000751OMPThreadPrivateDecl *
752Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000753 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000754 for (auto &RefExpr : VarList) {
755 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000756 VarDecl *VD = cast<VarDecl>(DE->getDecl());
757 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000758
759 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
760 // A threadprivate variable must not have an incomplete type.
761 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000762 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000763 continue;
764 }
765
766 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
767 // A threadprivate variable must not have a reference type.
768 if (VD->getType()->isReferenceType()) {
769 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000770 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
771 bool IsDecl =
772 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
773 Diag(VD->getLocation(),
774 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
775 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000776 continue;
777 }
778
Richard Smithfd3834f2013-04-13 02:43:54 +0000779 // Check if this is a TLS variable.
780 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000781 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000782 bool IsDecl =
783 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
784 Diag(VD->getLocation(),
785 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
786 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000787 continue;
788 }
789
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000790 // Check if initial value of threadprivate variable reference variable with
791 // local storage (it is not supported by runtime).
792 if (auto Init = VD->getAnyInitializer()) {
793 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000794 if (Checker.Visit(Init))
795 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000796 }
797
Alexey Bataeved09d242014-05-28 05:53:51 +0000798 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000799 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000800 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000801 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000802 if (!Vars.empty()) {
803 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
804 Vars);
805 D->setAccess(AS_public);
806 }
807 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000808}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000809
Alexey Bataev7ff55242014-06-19 09:13:45 +0000810static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
811 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
812 bool IsLoopIterVar = false) {
813 if (DVar.RefExpr) {
814 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
815 << getOpenMPClauseName(DVar.CKind);
816 return;
817 }
818 enum {
819 PDSA_StaticMemberShared,
820 PDSA_StaticLocalVarShared,
821 PDSA_LoopIterVarPrivate,
822 PDSA_LoopIterVarLinear,
823 PDSA_LoopIterVarLastprivate,
824 PDSA_ConstVarShared,
825 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000826 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000827 PDSA_LocalVarPrivate,
828 PDSA_Implicit
829 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000830 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000831 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000832 if (IsLoopIterVar) {
833 if (DVar.CKind == OMPC_private)
834 Reason = PDSA_LoopIterVarPrivate;
835 else if (DVar.CKind == OMPC_lastprivate)
836 Reason = PDSA_LoopIterVarLastprivate;
837 else
838 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000839 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
840 Reason = PDSA_TaskVarFirstprivate;
841 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000842 } else if (VD->isStaticLocal())
843 Reason = PDSA_StaticLocalVarShared;
844 else if (VD->isStaticDataMember())
845 Reason = PDSA_StaticMemberShared;
846 else if (VD->isFileVarDecl())
847 Reason = PDSA_GlobalVarShared;
848 else if (VD->getType().isConstant(SemaRef.getASTContext()))
849 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000850 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000851 ReportHint = true;
852 Reason = PDSA_LocalVarPrivate;
853 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000854 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000855 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000856 << Reason << ReportHint
857 << getOpenMPDirectiveName(Stack->getCurrentDirective());
858 } else if (DVar.ImplicitDSALoc.isValid()) {
859 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
860 << getOpenMPClauseName(DVar.CKind);
861 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000862}
863
Alexey Bataev758e55e2013-09-06 18:03:48 +0000864namespace {
865class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
866 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000867 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000868 bool ErrorFound;
869 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000870 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000871 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000872
Alexey Bataev758e55e2013-09-06 18:03:48 +0000873public:
874 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000875 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000876 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000877 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
878 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000879
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000880 auto DVar = Stack->getTopDSA(VD, false);
881 // Check if the variable has explicit DSA set and stop analysis if it so.
882 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000883
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000884 auto ELoc = E->getExprLoc();
885 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000886 // The default(none) clause requires that each variable that is referenced
887 // in the construct, and does not have a predetermined data-sharing
888 // attribute, must have its data-sharing attribute explicitly determined
889 // by being listed in a data-sharing attribute clause.
890 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000891 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000892 VarsWithInheritedDSA.count(VD) == 0) {
893 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000894 return;
895 }
896
897 // OpenMP [2.9.3.6, Restrictions, p.2]
898 // A list item that appears in a reduction clause of the innermost
899 // enclosing worksharing or parallel construct may not be accessed in an
900 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000901 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000902 [](OpenMPDirectiveKind K) -> bool {
903 return isOpenMPParallelDirective(K) ||
904 isOpenMPWorksharingDirective(K);
905 },
906 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000907 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
908 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000909 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
910 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000911 return;
912 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000913
914 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000915 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000916 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000917 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000918 }
919 }
920 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000921 for (auto *C : S->clauses()) {
922 // Skip analysis of arguments of implicitly defined firstprivate clause
923 // for task directives.
924 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
925 for (auto *CC : C->children()) {
926 if (CC)
927 Visit(CC);
928 }
929 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000930 }
931 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000932 for (auto *C : S->children()) {
933 if (C && !isa<OMPExecutableDirective>(C))
934 Visit(C);
935 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000936 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000937
938 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000939 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000940 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
941 return VarsWithInheritedDSA;
942 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000943
Alexey Bataev7ff55242014-06-19 09:13:45 +0000944 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
945 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000946};
Alexey Bataeved09d242014-05-28 05:53:51 +0000947} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000948
Alexey Bataevbae9a792014-06-27 10:37:06 +0000949void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000950 switch (DKind) {
951 case OMPD_parallel: {
952 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
953 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000954 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000955 std::make_pair(".global_tid.", KmpInt32PtrTy),
956 std::make_pair(".bound_tid.", KmpInt32PtrTy),
957 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000958 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000959 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
960 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000961 break;
962 }
963 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000964 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000965 std::make_pair(StringRef(), QualType()) // __context with shared vars
966 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000967 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
968 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000969 break;
970 }
971 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000972 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000973 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000974 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000975 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
976 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000977 break;
978 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000979 case OMPD_sections: {
980 Sema::CapturedParamNameType Params[] = {
981 std::make_pair(StringRef(), QualType()) // __context with shared vars
982 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000983 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
984 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000985 break;
986 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000987 case OMPD_section: {
988 Sema::CapturedParamNameType Params[] = {
989 std::make_pair(StringRef(), QualType()) // __context with shared vars
990 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000991 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
992 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000993 break;
994 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000995 case OMPD_single: {
996 Sema::CapturedParamNameType Params[] = {
997 std::make_pair(StringRef(), QualType()) // __context with shared vars
998 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000999 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1000 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001001 break;
1002 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001003 case OMPD_parallel_for: {
1004 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1005 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1006 Sema::CapturedParamNameType Params[] = {
1007 std::make_pair(".global_tid.", KmpInt32PtrTy),
1008 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1009 std::make_pair(StringRef(), QualType()) // __context with shared vars
1010 };
1011 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1012 Params);
1013 break;
1014 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001015 case OMPD_parallel_sections: {
1016 Sema::CapturedParamNameType Params[] = {
1017 std::make_pair(StringRef(), QualType()) // __context with shared vars
1018 };
1019 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1020 Params);
1021 break;
1022 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001023 case OMPD_task: {
1024 Sema::CapturedParamNameType Params[] = {
1025 std::make_pair(StringRef(), QualType()) // __context with shared vars
1026 };
1027 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1028 Params);
1029 break;
1030 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001031 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001032 llvm_unreachable("OpenMP Directive is not allowed");
1033 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001034 llvm_unreachable("Unknown OpenMP directive");
1035 }
1036}
1037
Alexey Bataev549210e2014-06-24 04:39:47 +00001038bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1039 OpenMPDirectiveKind CurrentRegion,
1040 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001041 // Allowed nesting of constructs
1042 // +------------------+-----------------+------------------------------------+
1043 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1044 // +------------------+-----------------+------------------------------------+
1045 // | parallel | parallel | * |
1046 // | parallel | for | * |
1047 // | parallel | simd | * |
1048 // | parallel | sections | * |
1049 // | parallel | section | + |
1050 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001051 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001052 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001053 // | parallel | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001054 // +------------------+-----------------+------------------------------------+
1055 // | for | parallel | * |
1056 // | for | for | + |
1057 // | for | simd | * |
1058 // | for | sections | + |
1059 // | for | section | + |
1060 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001061 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001062 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001063 // | for | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001064 // +------------------+-----------------+------------------------------------+
1065 // | simd | parallel | |
1066 // | simd | for | |
1067 // | simd | simd | |
1068 // | simd | sections | |
1069 // | simd | section | |
1070 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001071 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001072 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001073 // | simd | task | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001074 // +------------------+-----------------+------------------------------------+
1075 // | sections | parallel | * |
1076 // | sections | for | + |
1077 // | sections | simd | * |
1078 // | sections | sections | + |
1079 // | sections | section | * |
1080 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001081 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001082 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001083 // | sections | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001084 // +------------------+-----------------+------------------------------------+
1085 // | section | parallel | * |
1086 // | section | for | + |
1087 // | section | simd | * |
1088 // | section | sections | + |
1089 // | section | section | + |
1090 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001091 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001092 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001093 // | section | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001094 // +------------------+-----------------+------------------------------------+
1095 // | single | parallel | * |
1096 // | single | for | + |
1097 // | single | simd | * |
1098 // | single | sections | + |
1099 // | single | section | + |
1100 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001101 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001102 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001103 // | single | task | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001104 // +------------------+-----------------+------------------------------------+
1105 // | parallel for | parallel | * |
1106 // | parallel for | for | + |
1107 // | parallel for | simd | * |
1108 // | parallel for | sections | + |
1109 // | parallel for | section | + |
1110 // | parallel for | single | + |
1111 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001112 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001113 // | parallel for | task | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001114 // +------------------+-----------------+------------------------------------+
1115 // | parallel sections| parallel | * |
1116 // | parallel sections| for | + |
1117 // | parallel sections| simd | * |
1118 // | parallel sections| sections | + |
1119 // | parallel sections| section | * |
1120 // | parallel sections| single | + |
1121 // | parallel sections| parallel for | * |
1122 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001123 // | parallel sections| task | * |
1124 // +------------------+-----------------+------------------------------------+
1125 // | task | parallel | * |
1126 // | task | for | + |
1127 // | task | simd | * |
1128 // | task | sections | + |
1129 // | task | section | + |
1130 // | task | single | + |
1131 // | task | parallel for | * |
1132 // | task |parallel sections| * |
1133 // | task | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001134 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001135 if (Stack->getCurScope()) {
1136 auto ParentRegion = Stack->getParentDirective();
1137 bool NestingProhibited = false;
1138 bool CloseNesting = true;
1139 bool ShouldBeInParallelRegion = false;
1140 if (isOpenMPSimdDirective(ParentRegion)) {
1141 // OpenMP [2.16, Nesting of Regions]
1142 // OpenMP constructs may not be nested inside a simd region.
1143 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1144 return true;
1145 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001146 if (CurrentRegion == OMPD_section) {
1147 // OpenMP [2.7.2, sections Construct, Restrictions]
1148 // Orphaned section directives are prohibited. That is, the section
1149 // directives must appear within the sections construct and must not be
1150 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001151 if (ParentRegion != OMPD_sections &&
1152 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001153 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1154 << (ParentRegion != OMPD_unknown)
1155 << getOpenMPDirectiveName(ParentRegion);
1156 return true;
1157 }
1158 return false;
1159 }
Alexey Bataev549210e2014-06-24 04:39:47 +00001160 if (isOpenMPWorksharingDirective(CurrentRegion) &&
1161 !isOpenMPParallelDirective(CurrentRegion) &&
1162 !isOpenMPSimdDirective(CurrentRegion)) {
1163 // OpenMP [2.16, Nesting of Regions]
1164 // A worksharing region may not be closely nested inside a worksharing,
1165 // explicit task, critical, ordered, atomic, or master region.
1166 // TODO
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001167 NestingProhibited = (isOpenMPWorksharingDirective(ParentRegion) &&
1168 !isOpenMPSimdDirective(ParentRegion)) ||
1169 ParentRegion == OMPD_task;
Alexey Bataev549210e2014-06-24 04:39:47 +00001170 ShouldBeInParallelRegion = true;
1171 }
1172 if (NestingProhibited) {
1173 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev41b97322014-07-02 03:04:53 +00001174 << CloseNesting << getOpenMPDirectiveName(ParentRegion)
1175 << ShouldBeInParallelRegion << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001176 return true;
1177 }
1178 }
1179 return false;
1180}
1181
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001182StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1183 ArrayRef<OMPClause *> Clauses,
1184 Stmt *AStmt,
1185 SourceLocation StartLoc,
1186 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001187 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1188
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001189 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +00001190 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
1191 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001192
1193 // Check default data sharing attributes for referenced variables.
1194 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1195 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1196 if (DSAChecker.isErrorFound())
1197 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001198 // Generate list of implicitly defined firstprivate variables.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001199 auto &VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001200 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1201 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1202
1203 bool ErrorFound = false;
1204 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001205 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1206 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1207 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001208 ClausesWithImplicit.push_back(Implicit);
1209 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +00001210 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001211 } else
1212 ErrorFound = true;
1213 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001214
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001215 switch (Kind) {
1216 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001217 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1218 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001219 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001220 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001221 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1222 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001223 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001224 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001225 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1226 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001227 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001228 case OMPD_sections:
1229 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1230 EndLoc);
1231 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001232 case OMPD_section:
1233 assert(ClausesWithImplicit.empty() &&
1234 "No clauses is allowed for 'omp section' directive");
1235 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1236 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001237 case OMPD_single:
1238 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1239 EndLoc);
1240 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001241 case OMPD_parallel_for:
1242 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1243 EndLoc, VarsWithInheritedDSA);
1244 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001245 case OMPD_parallel_sections:
1246 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1247 StartLoc, EndLoc);
1248 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001249 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001250 Res =
1251 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1252 break;
1253 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001254 llvm_unreachable("OpenMP Directive is not allowed");
1255 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001256 llvm_unreachable("Unknown OpenMP directive");
1257 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001258
Alexey Bataev4acb8592014-07-07 13:01:15 +00001259 for (auto P : VarsWithInheritedDSA) {
1260 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1261 << P.first << P.second->getSourceRange();
1262 }
1263 if (!VarsWithInheritedDSA.empty())
1264 return StmtError();
1265
Alexey Bataeved09d242014-05-28 05:53:51 +00001266 if (ErrorFound)
1267 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001268 return Res;
1269}
1270
1271StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1272 Stmt *AStmt,
1273 SourceLocation StartLoc,
1274 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001275 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1276 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1277 // 1.2.2 OpenMP Language Terminology
1278 // Structured block - An executable statement with a single entry at the
1279 // top and a single exit at the bottom.
1280 // The point of exit cannot be a branch out of the structured block.
1281 // longjmp() and throw() must not violate the entry/exit criteria.
1282 CS->getCapturedDecl()->setNothrow();
1283
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001284 getCurFunction()->setHasBranchProtectedScope();
1285
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001286 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1287 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001288}
1289
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001290namespace {
1291/// \brief Helper class for checking canonical form of the OpenMP loops and
1292/// extracting iteration space of each loop in the loop nest, that will be used
1293/// for IR generation.
1294class OpenMPIterationSpaceChecker {
1295 /// \brief Reference to Sema.
1296 Sema &SemaRef;
1297 /// \brief A location for diagnostics (when there is no some better location).
1298 SourceLocation DefaultLoc;
1299 /// \brief A location for diagnostics (when increment is not compatible).
1300 SourceLocation ConditionLoc;
1301 /// \brief A source location for referring to condition later.
1302 SourceRange ConditionSrcRange;
1303 /// \brief Loop variable.
1304 VarDecl *Var;
1305 /// \brief Lower bound (initializer for the var).
1306 Expr *LB;
1307 /// \brief Upper bound.
1308 Expr *UB;
1309 /// \brief Loop step (increment).
1310 Expr *Step;
1311 /// \brief This flag is true when condition is one of:
1312 /// Var < UB
1313 /// Var <= UB
1314 /// UB > Var
1315 /// UB >= Var
1316 bool TestIsLessOp;
1317 /// \brief This flag is true when condition is strict ( < or > ).
1318 bool TestIsStrictOp;
1319 /// \brief This flag is true when step is subtracted on each iteration.
1320 bool SubtractStep;
1321
1322public:
1323 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1324 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1325 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1326 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1327 SubtractStep(false) {}
1328 /// \brief Check init-expr for canonical loop form and save loop counter
1329 /// variable - #Var and its initialization value - #LB.
1330 bool CheckInit(Stmt *S);
1331 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1332 /// for less/greater and for strict/non-strict comparison.
1333 bool CheckCond(Expr *S);
1334 /// \brief Check incr-expr for canonical loop form and return true if it
1335 /// does not conform, otherwise save loop step (#Step).
1336 bool CheckInc(Expr *S);
1337 /// \brief Return the loop counter variable.
1338 VarDecl *GetLoopVar() const { return Var; }
1339 /// \brief Return true if any expression is dependent.
1340 bool Dependent() const;
1341
1342private:
1343 /// \brief Check the right-hand side of an assignment in the increment
1344 /// expression.
1345 bool CheckIncRHS(Expr *RHS);
1346 /// \brief Helper to set loop counter variable and its initializer.
1347 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1348 /// \brief Helper to set upper bound.
1349 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1350 const SourceLocation &SL);
1351 /// \brief Helper to set loop increment.
1352 bool SetStep(Expr *NewStep, bool Subtract);
1353};
1354
1355bool OpenMPIterationSpaceChecker::Dependent() const {
1356 if (!Var) {
1357 assert(!LB && !UB && !Step);
1358 return false;
1359 }
1360 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1361 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1362}
1363
1364bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1365 // State consistency checking to ensure correct usage.
1366 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1367 !TestIsLessOp && !TestIsStrictOp);
1368 if (!NewVar || !NewLB)
1369 return true;
1370 Var = NewVar;
1371 LB = NewLB;
1372 return false;
1373}
1374
1375bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1376 const SourceRange &SR,
1377 const SourceLocation &SL) {
1378 // State consistency checking to ensure correct usage.
1379 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1380 !TestIsLessOp && !TestIsStrictOp);
1381 if (!NewUB)
1382 return true;
1383 UB = NewUB;
1384 TestIsLessOp = LessOp;
1385 TestIsStrictOp = StrictOp;
1386 ConditionSrcRange = SR;
1387 ConditionLoc = SL;
1388 return false;
1389}
1390
1391bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1392 // State consistency checking to ensure correct usage.
1393 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1394 if (!NewStep)
1395 return true;
1396 if (!NewStep->isValueDependent()) {
1397 // Check that the step is integer expression.
1398 SourceLocation StepLoc = NewStep->getLocStart();
1399 ExprResult Val =
1400 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1401 if (Val.isInvalid())
1402 return true;
1403 NewStep = Val.get();
1404
1405 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1406 // If test-expr is of form var relational-op b and relational-op is < or
1407 // <= then incr-expr must cause var to increase on each iteration of the
1408 // loop. If test-expr is of form var relational-op b and relational-op is
1409 // > or >= then incr-expr must cause var to decrease on each iteration of
1410 // the loop.
1411 // If test-expr is of form b relational-op var and relational-op is < or
1412 // <= then incr-expr must cause var to decrease on each iteration of the
1413 // loop. If test-expr is of form b relational-op var and relational-op is
1414 // > or >= then incr-expr must cause var to increase on each iteration of
1415 // the loop.
1416 llvm::APSInt Result;
1417 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1418 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1419 bool IsConstNeg =
1420 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1421 bool IsConstZero = IsConstant && !Result.getBoolValue();
1422 if (UB && (IsConstZero ||
1423 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1424 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1425 SemaRef.Diag(NewStep->getExprLoc(),
1426 diag::err_omp_loop_incr_not_compatible)
1427 << Var << TestIsLessOp << NewStep->getSourceRange();
1428 SemaRef.Diag(ConditionLoc,
1429 diag::note_omp_loop_cond_requres_compatible_incr)
1430 << TestIsLessOp << ConditionSrcRange;
1431 return true;
1432 }
1433 }
1434
1435 Step = NewStep;
1436 SubtractStep = Subtract;
1437 return false;
1438}
1439
1440bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1441 // Check init-expr for canonical loop form and save loop counter
1442 // variable - #Var and its initialization value - #LB.
1443 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1444 // var = lb
1445 // integer-type var = lb
1446 // random-access-iterator-type var = lb
1447 // pointer-type var = lb
1448 //
1449 if (!S) {
1450 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1451 return true;
1452 }
1453 if (Expr *E = dyn_cast<Expr>(S))
1454 S = E->IgnoreParens();
1455 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1456 if (BO->getOpcode() == BO_Assign)
1457 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1458 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1459 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1460 if (DS->isSingleDecl()) {
1461 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1462 if (Var->hasInit()) {
1463 // Accept non-canonical init form here but emit ext. warning.
1464 if (Var->getInitStyle() != VarDecl::CInit)
1465 SemaRef.Diag(S->getLocStart(),
1466 diag::ext_omp_loop_not_canonical_init)
1467 << S->getSourceRange();
1468 return SetVarAndLB(Var, Var->getInit());
1469 }
1470 }
1471 }
1472 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1473 if (CE->getOperator() == OO_Equal)
1474 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1475 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1476
1477 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1478 << S->getSourceRange();
1479 return true;
1480}
1481
Alexey Bataev23b69422014-06-18 07:08:49 +00001482/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001483/// variable (which may be the loop variable) if possible.
1484static const VarDecl *GetInitVarDecl(const Expr *E) {
1485 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001486 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001487 E = E->IgnoreParenImpCasts();
1488 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1489 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1490 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1491 CE->getArg(0) != nullptr)
1492 E = CE->getArg(0)->IgnoreParenImpCasts();
1493 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1494 if (!DRE)
1495 return nullptr;
1496 return dyn_cast<VarDecl>(DRE->getDecl());
1497}
1498
1499bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1500 // Check test-expr for canonical form, save upper-bound UB, flags for
1501 // less/greater and for strict/non-strict comparison.
1502 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1503 // var relational-op b
1504 // b relational-op var
1505 //
1506 if (!S) {
1507 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1508 return true;
1509 }
1510 S = S->IgnoreParenImpCasts();
1511 SourceLocation CondLoc = S->getLocStart();
1512 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1513 if (BO->isRelationalOp()) {
1514 if (GetInitVarDecl(BO->getLHS()) == Var)
1515 return SetUB(BO->getRHS(),
1516 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1517 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1518 BO->getSourceRange(), BO->getOperatorLoc());
1519 if (GetInitVarDecl(BO->getRHS()) == Var)
1520 return SetUB(BO->getLHS(),
1521 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1522 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1523 BO->getSourceRange(), BO->getOperatorLoc());
1524 }
1525 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1526 if (CE->getNumArgs() == 2) {
1527 auto Op = CE->getOperator();
1528 switch (Op) {
1529 case OO_Greater:
1530 case OO_GreaterEqual:
1531 case OO_Less:
1532 case OO_LessEqual:
1533 if (GetInitVarDecl(CE->getArg(0)) == Var)
1534 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1535 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1536 CE->getOperatorLoc());
1537 if (GetInitVarDecl(CE->getArg(1)) == Var)
1538 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1539 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1540 CE->getOperatorLoc());
1541 break;
1542 default:
1543 break;
1544 }
1545 }
1546 }
1547 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1548 << S->getSourceRange() << Var;
1549 return true;
1550}
1551
1552bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1553 // RHS of canonical loop form increment can be:
1554 // var + incr
1555 // incr + var
1556 // var - incr
1557 //
1558 RHS = RHS->IgnoreParenImpCasts();
1559 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1560 if (BO->isAdditiveOp()) {
1561 bool IsAdd = BO->getOpcode() == BO_Add;
1562 if (GetInitVarDecl(BO->getLHS()) == Var)
1563 return SetStep(BO->getRHS(), !IsAdd);
1564 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1565 return SetStep(BO->getLHS(), false);
1566 }
1567 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1568 bool IsAdd = CE->getOperator() == OO_Plus;
1569 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1570 if (GetInitVarDecl(CE->getArg(0)) == Var)
1571 return SetStep(CE->getArg(1), !IsAdd);
1572 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1573 return SetStep(CE->getArg(0), false);
1574 }
1575 }
1576 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1577 << RHS->getSourceRange() << Var;
1578 return true;
1579}
1580
1581bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1582 // Check incr-expr for canonical loop form and return true if it
1583 // does not conform.
1584 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1585 // ++var
1586 // var++
1587 // --var
1588 // var--
1589 // var += incr
1590 // var -= incr
1591 // var = var + incr
1592 // var = incr + var
1593 // var = var - incr
1594 //
1595 if (!S) {
1596 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1597 return true;
1598 }
1599 S = S->IgnoreParens();
1600 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1601 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1602 return SetStep(
1603 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1604 (UO->isDecrementOp() ? -1 : 1)).get(),
1605 false);
1606 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1607 switch (BO->getOpcode()) {
1608 case BO_AddAssign:
1609 case BO_SubAssign:
1610 if (GetInitVarDecl(BO->getLHS()) == Var)
1611 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1612 break;
1613 case BO_Assign:
1614 if (GetInitVarDecl(BO->getLHS()) == Var)
1615 return CheckIncRHS(BO->getRHS());
1616 break;
1617 default:
1618 break;
1619 }
1620 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1621 switch (CE->getOperator()) {
1622 case OO_PlusPlus:
1623 case OO_MinusMinus:
1624 if (GetInitVarDecl(CE->getArg(0)) == Var)
1625 return SetStep(
1626 SemaRef.ActOnIntegerConstant(
1627 CE->getLocStart(),
1628 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1629 false);
1630 break;
1631 case OO_PlusEqual:
1632 case OO_MinusEqual:
1633 if (GetInitVarDecl(CE->getArg(0)) == Var)
1634 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1635 break;
1636 case OO_Equal:
1637 if (GetInitVarDecl(CE->getArg(0)) == Var)
1638 return CheckIncRHS(CE->getArg(1));
1639 break;
1640 default:
1641 break;
1642 }
1643 }
1644 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1645 << S->getSourceRange() << Var;
1646 return true;
1647}
Alexey Bataev23b69422014-06-18 07:08:49 +00001648} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001649
1650/// \brief Called on a for stmt to check and extract its iteration space
1651/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00001652static bool CheckOpenMPIterationSpace(
1653 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
1654 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
1655 Expr *NestedLoopCountExpr,
1656 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001657 // OpenMP [2.6, Canonical Loop Form]
1658 // for (init-expr; test-expr; incr-expr) structured-block
1659 auto For = dyn_cast_or_null<ForStmt>(S);
1660 if (!For) {
1661 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001662 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1663 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1664 << CurrentNestedLoopCount;
1665 if (NestedLoopCount > 1)
1666 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1667 diag::note_omp_collapse_expr)
1668 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001669 return true;
1670 }
1671 assert(For->getBody());
1672
1673 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1674
1675 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001676 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001677 if (ISC.CheckInit(Init)) {
1678 return true;
1679 }
1680
1681 bool HasErrors = false;
1682
1683 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001684 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001685
1686 // OpenMP [2.6, Canonical Loop Form]
1687 // Var is one of the following:
1688 // A variable of signed or unsigned integer type.
1689 // For C++, a variable of a random access iterator type.
1690 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001691 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001692 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1693 !VarType->isPointerType() &&
1694 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1695 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1696 << SemaRef.getLangOpts().CPlusPlus;
1697 HasErrors = true;
1698 }
1699
Alexey Bataev4acb8592014-07-07 13:01:15 +00001700 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
1701 // Construct
1702 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1703 // parallel for construct is (are) private.
1704 // The loop iteration variable in the associated for-loop of a simd construct
1705 // with just one associated for-loop is linear with a constant-linear-step
1706 // that is the increment of the associated for-loop.
1707 // Exclude loop var from the list of variables with implicitly defined data
1708 // sharing attributes.
1709 while (VarsWithImplicitDSA.count(Var) > 0)
1710 VarsWithImplicitDSA.erase(Var);
1711
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001712 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1713 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001714 // The loop iteration variable in the associated for-loop of a simd construct
1715 // with just one associated for-loop may be listed in a linear clause with a
1716 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001717 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1718 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001719 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001720 auto PredeterminedCKind =
1721 isOpenMPSimdDirective(DKind)
1722 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
1723 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001724 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001725 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00001726 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1727 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001728 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001729 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00001730 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
1731 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001732 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001733 HasErrors = true;
1734 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001735 // Make the loop iteration variable private (for worksharing constructs),
1736 // linear (for simd directives with the only one associated loop) or
1737 // lastprivate (for simd directives with several collapsed loops).
1738 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001739 }
1740
Alexey Bataev7ff55242014-06-19 09:13:45 +00001741 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001742
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001743 // Check test-expr.
1744 HasErrors |= ISC.CheckCond(For->getCond());
1745
1746 // Check incr-expr.
1747 HasErrors |= ISC.CheckInc(For->getInc());
1748
1749 if (ISC.Dependent())
1750 return HasErrors;
1751
1752 // FIXME: Build loop's iteration space representation.
1753 return HasErrors;
1754}
1755
1756/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1757/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1758/// to get the first for loop.
1759static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1760 if (IgnoreCaptured)
1761 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1762 S = CapS->getCapturedStmt();
1763 // OpenMP [2.8.1, simd construct, Restrictions]
1764 // All loops associated with the construct must be perfectly nested; that is,
1765 // there must be no intervening code nor any OpenMP directive between any two
1766 // loops.
1767 while (true) {
1768 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1769 S = AS->getSubStmt();
1770 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1771 if (CS->size() != 1)
1772 break;
1773 S = CS->body_back();
1774 } else
1775 break;
1776 }
1777 return S;
1778}
1779
1780/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001781/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1782/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001783static unsigned
1784CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
1785 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
1786 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001787 unsigned NestedLoopCount = 1;
1788 if (NestedLoopCountExpr) {
1789 // Found 'collapse' clause - calculate collapse number.
1790 llvm::APSInt Result;
1791 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1792 NestedLoopCount = Result.getLimitedValue();
1793 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001794 // This is helper routine for loop directives (e.g., 'for', 'simd',
1795 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001796 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1797 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001798 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00001799 NestedLoopCount, NestedLoopCountExpr,
1800 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001801 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001802 // Move on to the next nested for loop, or to the loop body.
1803 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1804 }
1805
1806 // FIXME: Build resulting iteration space for IR generation (collapsing
1807 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001808 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001809}
1810
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001811static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001812 auto CollapseFilter = [](const OMPClause *C) -> bool {
1813 return C->getClauseKind() == OMPC_collapse;
1814 };
1815 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1816 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001817 if (I)
1818 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1819 return nullptr;
1820}
1821
Alexey Bataev4acb8592014-07-07 13:01:15 +00001822StmtResult Sema::ActOnOpenMPSimdDirective(
1823 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1824 SourceLocation EndLoc,
1825 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001826 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001827 unsigned NestedLoopCount =
1828 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
1829 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001830 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001831 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001832
1833 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001834 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1835 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001836}
1837
Alexey Bataev4acb8592014-07-07 13:01:15 +00001838StmtResult Sema::ActOnOpenMPForDirective(
1839 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1840 SourceLocation EndLoc,
1841 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001842 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001843 unsigned NestedLoopCount =
1844 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
1845 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001846 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001847 return StmtError();
1848
1849 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001850 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1851 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001852}
1853
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001854StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1855 Stmt *AStmt,
1856 SourceLocation StartLoc,
1857 SourceLocation EndLoc) {
1858 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1859 auto BaseStmt = AStmt;
1860 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1861 BaseStmt = CS->getCapturedStmt();
1862 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1863 auto S = C->children();
1864 if (!S)
1865 return StmtError();
1866 // All associated statements must be '#pragma omp section' except for
1867 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001868 for (++S; S; ++S) {
1869 auto SectionStmt = *S;
1870 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1871 if (SectionStmt)
1872 Diag(SectionStmt->getLocStart(),
1873 diag::err_omp_sections_substmt_not_section);
1874 return StmtError();
1875 }
1876 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001877 } else {
1878 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1879 return StmtError();
1880 }
1881
1882 getCurFunction()->setHasBranchProtectedScope();
1883
1884 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1885 AStmt);
1886}
1887
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001888StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1889 SourceLocation StartLoc,
1890 SourceLocation EndLoc) {
1891 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1892
1893 getCurFunction()->setHasBranchProtectedScope();
1894
1895 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1896}
1897
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001898StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
1899 Stmt *AStmt,
1900 SourceLocation StartLoc,
1901 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00001902 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1903
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001904 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00001905
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001906 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1907}
1908
Alexey Bataev4acb8592014-07-07 13:01:15 +00001909StmtResult Sema::ActOnOpenMPParallelForDirective(
1910 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1911 SourceLocation EndLoc,
1912 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
1913 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1914 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1915 // 1.2.2 OpenMP Language Terminology
1916 // Structured block - An executable statement with a single entry at the
1917 // top and a single exit at the bottom.
1918 // The point of exit cannot be a branch out of the structured block.
1919 // longjmp() and throw() must not violate the entry/exit criteria.
1920 CS->getCapturedDecl()->setNothrow();
1921
1922 // In presence of clause 'collapse', it will define the nested loops number.
1923 unsigned NestedLoopCount =
1924 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
1925 *this, *DSAStack, VarsWithImplicitDSA);
1926 if (NestedLoopCount == 0)
1927 return StmtError();
1928
1929 getCurFunction()->setHasBranchProtectedScope();
1930 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
1931 NestedLoopCount, Clauses, AStmt);
1932}
1933
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001934StmtResult
1935Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
1936 Stmt *AStmt, SourceLocation StartLoc,
1937 SourceLocation EndLoc) {
1938 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1939 auto BaseStmt = AStmt;
1940 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1941 BaseStmt = CS->getCapturedStmt();
1942 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1943 auto S = C->children();
1944 if (!S)
1945 return StmtError();
1946 // All associated statements must be '#pragma omp section' except for
1947 // the first one.
1948 for (++S; S; ++S) {
1949 auto SectionStmt = *S;
1950 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1951 if (SectionStmt)
1952 Diag(SectionStmt->getLocStart(),
1953 diag::err_omp_parallel_sections_substmt_not_section);
1954 return StmtError();
1955 }
1956 }
1957 } else {
1958 Diag(AStmt->getLocStart(),
1959 diag::err_omp_parallel_sections_not_compound_stmt);
1960 return StmtError();
1961 }
1962
1963 getCurFunction()->setHasBranchProtectedScope();
1964
1965 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
1966 Clauses, AStmt);
1967}
1968
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001969StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
1970 Stmt *AStmt, SourceLocation StartLoc,
1971 SourceLocation EndLoc) {
1972 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1973 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1974 // 1.2.2 OpenMP Language Terminology
1975 // Structured block - An executable statement with a single entry at the
1976 // top and a single exit at the bottom.
1977 // The point of exit cannot be a branch out of the structured block.
1978 // longjmp() and throw() must not violate the entry/exit criteria.
1979 CS->getCapturedDecl()->setNothrow();
1980
1981 getCurFunction()->setHasBranchProtectedScope();
1982
1983 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1984}
1985
Alexey Bataeved09d242014-05-28 05:53:51 +00001986OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001987 SourceLocation StartLoc,
1988 SourceLocation LParenLoc,
1989 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001990 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001991 switch (Kind) {
1992 case OMPC_if:
1993 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1994 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001995 case OMPC_num_threads:
1996 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1997 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001998 case OMPC_safelen:
1999 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2000 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002001 case OMPC_collapse:
2002 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2003 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002004 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002005 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002006 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002007 case OMPC_private:
2008 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002009 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002010 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002011 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002012 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002013 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002014 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002015 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002016 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002017 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002018 case OMPC_threadprivate:
2019 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002020 llvm_unreachable("Clause is not allowed.");
2021 }
2022 return Res;
2023}
2024
Alexey Bataeved09d242014-05-28 05:53:51 +00002025OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002026 SourceLocation LParenLoc,
2027 SourceLocation EndLoc) {
2028 Expr *ValExpr = Condition;
2029 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2030 !Condition->isInstantiationDependent() &&
2031 !Condition->containsUnexpandedParameterPack()) {
2032 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002033 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002034 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002035 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002036
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002037 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002038 }
2039
2040 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2041}
2042
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002043ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2044 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002045 if (!Op)
2046 return ExprError();
2047
2048 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2049 public:
2050 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002051 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002052 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2053 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002054 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2055 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002056 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2057 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002058 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2059 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002060 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2061 QualType T,
2062 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002063 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2064 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002065 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2066 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002067 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002068 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002069 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002070 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2071 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002072 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2073 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002074 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2075 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002076 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002077 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002078 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002079 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2080 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002081 llvm_unreachable("conversion functions are permitted");
2082 }
2083 } ConvertDiagnoser;
2084 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2085}
2086
2087OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2088 SourceLocation StartLoc,
2089 SourceLocation LParenLoc,
2090 SourceLocation EndLoc) {
2091 Expr *ValExpr = NumThreads;
2092 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2093 !NumThreads->isInstantiationDependent() &&
2094 !NumThreads->containsUnexpandedParameterPack()) {
2095 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2096 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002097 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002098 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002099 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002100
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002101 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002102
2103 // OpenMP [2.5, Restrictions]
2104 // The num_threads expression must evaluate to a positive integer value.
2105 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002106 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2107 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002108 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2109 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002110 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002111 }
2112 }
2113
Alexey Bataeved09d242014-05-28 05:53:51 +00002114 return new (Context)
2115 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002116}
2117
Alexey Bataev62c87d22014-03-21 04:51:18 +00002118ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2119 OpenMPClauseKind CKind) {
2120 if (!E)
2121 return ExprError();
2122 if (E->isValueDependent() || E->isTypeDependent() ||
2123 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002124 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002125 llvm::APSInt Result;
2126 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2127 if (ICE.isInvalid())
2128 return ExprError();
2129 if (!Result.isStrictlyPositive()) {
2130 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2131 << getOpenMPClauseName(CKind) << E->getSourceRange();
2132 return ExprError();
2133 }
2134 return ICE;
2135}
2136
2137OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2138 SourceLocation LParenLoc,
2139 SourceLocation EndLoc) {
2140 // OpenMP [2.8.1, simd construct, Description]
2141 // The parameter of the safelen clause must be a constant
2142 // positive integer expression.
2143 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2144 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002145 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002146 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002147 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002148}
2149
Alexander Musman64d33f12014-06-04 07:53:32 +00002150OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2151 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002152 SourceLocation LParenLoc,
2153 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002154 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002155 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002156 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002157 // The parameter of the collapse clause must be a constant
2158 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002159 ExprResult NumForLoopsResult =
2160 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2161 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002162 return nullptr;
2163 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002164 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002165}
2166
Alexey Bataeved09d242014-05-28 05:53:51 +00002167OMPClause *Sema::ActOnOpenMPSimpleClause(
2168 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2169 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002170 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002171 switch (Kind) {
2172 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002173 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002174 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2175 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002176 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002177 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002178 Res = ActOnOpenMPProcBindClause(
2179 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2180 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002181 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002182 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002183 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002184 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002185 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002186 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002187 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002188 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002189 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002190 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002191 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002192 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002193 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002194 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002195 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002196 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002197 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002198 case OMPC_threadprivate:
2199 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002200 llvm_unreachable("Clause is not allowed.");
2201 }
2202 return Res;
2203}
2204
2205OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2206 SourceLocation KindKwLoc,
2207 SourceLocation StartLoc,
2208 SourceLocation LParenLoc,
2209 SourceLocation EndLoc) {
2210 if (Kind == OMPC_DEFAULT_unknown) {
2211 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002212 static_assert(OMPC_DEFAULT_unknown > 0,
2213 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002214 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002215 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002216 Values += "'";
2217 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2218 Values += "'";
2219 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002220 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002221 Values += " or ";
2222 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002223 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002224 break;
2225 default:
2226 Values += Sep;
2227 break;
2228 }
2229 }
2230 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002231 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002232 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002233 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002234 switch (Kind) {
2235 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002236 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002237 break;
2238 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002239 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002240 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002241 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002242 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002243 break;
2244 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002245 return new (Context)
2246 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002247}
2248
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002249OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2250 SourceLocation KindKwLoc,
2251 SourceLocation StartLoc,
2252 SourceLocation LParenLoc,
2253 SourceLocation EndLoc) {
2254 if (Kind == OMPC_PROC_BIND_unknown) {
2255 std::string Values;
2256 std::string Sep(", ");
2257 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2258 Values += "'";
2259 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2260 Values += "'";
2261 switch (i) {
2262 case OMPC_PROC_BIND_unknown - 2:
2263 Values += " or ";
2264 break;
2265 case OMPC_PROC_BIND_unknown - 1:
2266 break;
2267 default:
2268 Values += Sep;
2269 break;
2270 }
2271 }
2272 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002273 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002274 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002275 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002276 return new (Context)
2277 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002278}
2279
Alexey Bataev56dafe82014-06-20 07:16:17 +00002280OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2281 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2282 SourceLocation StartLoc, SourceLocation LParenLoc,
2283 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2284 SourceLocation EndLoc) {
2285 OMPClause *Res = nullptr;
2286 switch (Kind) {
2287 case OMPC_schedule:
2288 Res = ActOnOpenMPScheduleClause(
2289 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2290 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2291 break;
2292 case OMPC_if:
2293 case OMPC_num_threads:
2294 case OMPC_safelen:
2295 case OMPC_collapse:
2296 case OMPC_default:
2297 case OMPC_proc_bind:
2298 case OMPC_private:
2299 case OMPC_firstprivate:
2300 case OMPC_lastprivate:
2301 case OMPC_shared:
2302 case OMPC_reduction:
2303 case OMPC_linear:
2304 case OMPC_aligned:
2305 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002306 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002307 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002308 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002309 case OMPC_threadprivate:
2310 case OMPC_unknown:
2311 llvm_unreachable("Clause is not allowed.");
2312 }
2313 return Res;
2314}
2315
2316OMPClause *Sema::ActOnOpenMPScheduleClause(
2317 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2318 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2319 SourceLocation EndLoc) {
2320 if (Kind == OMPC_SCHEDULE_unknown) {
2321 std::string Values;
2322 std::string Sep(", ");
2323 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2324 Values += "'";
2325 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2326 Values += "'";
2327 switch (i) {
2328 case OMPC_SCHEDULE_unknown - 2:
2329 Values += " or ";
2330 break;
2331 case OMPC_SCHEDULE_unknown - 1:
2332 break;
2333 default:
2334 Values += Sep;
2335 break;
2336 }
2337 }
2338 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2339 << Values << getOpenMPClauseName(OMPC_schedule);
2340 return nullptr;
2341 }
2342 Expr *ValExpr = ChunkSize;
2343 if (ChunkSize) {
2344 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2345 !ChunkSize->isInstantiationDependent() &&
2346 !ChunkSize->containsUnexpandedParameterPack()) {
2347 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2348 ExprResult Val =
2349 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2350 if (Val.isInvalid())
2351 return nullptr;
2352
2353 ValExpr = Val.get();
2354
2355 // OpenMP [2.7.1, Restrictions]
2356 // chunk_size must be a loop invariant integer expression with a positive
2357 // value.
2358 llvm::APSInt Result;
2359 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2360 Result.isSigned() && !Result.isStrictlyPositive()) {
2361 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2362 << "schedule" << ChunkSize->getSourceRange();
2363 return nullptr;
2364 }
2365 }
2366 }
2367
2368 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2369 EndLoc, Kind, ValExpr);
2370}
2371
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002372OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2373 SourceLocation StartLoc,
2374 SourceLocation EndLoc) {
2375 OMPClause *Res = nullptr;
2376 switch (Kind) {
2377 case OMPC_ordered:
2378 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2379 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002380 case OMPC_nowait:
2381 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2382 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002383 case OMPC_if:
2384 case OMPC_num_threads:
2385 case OMPC_safelen:
2386 case OMPC_collapse:
2387 case OMPC_schedule:
2388 case OMPC_private:
2389 case OMPC_firstprivate:
2390 case OMPC_lastprivate:
2391 case OMPC_shared:
2392 case OMPC_reduction:
2393 case OMPC_linear:
2394 case OMPC_aligned:
2395 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002396 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002397 case OMPC_default:
2398 case OMPC_proc_bind:
2399 case OMPC_threadprivate:
2400 case OMPC_unknown:
2401 llvm_unreachable("Clause is not allowed.");
2402 }
2403 return Res;
2404}
2405
2406OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2407 SourceLocation EndLoc) {
2408 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2409}
2410
Alexey Bataev236070f2014-06-20 11:19:47 +00002411OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2412 SourceLocation EndLoc) {
2413 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2414}
2415
Alexey Bataevc5e02582014-06-16 07:08:35 +00002416OMPClause *Sema::ActOnOpenMPVarListClause(
2417 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2418 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2419 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2420 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002421 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002422 switch (Kind) {
2423 case OMPC_private:
2424 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2425 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002426 case OMPC_firstprivate:
2427 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2428 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002429 case OMPC_lastprivate:
2430 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2431 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002432 case OMPC_shared:
2433 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2434 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002435 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002436 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2437 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002438 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002439 case OMPC_linear:
2440 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2441 ColonLoc, EndLoc);
2442 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002443 case OMPC_aligned:
2444 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2445 ColonLoc, EndLoc);
2446 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002447 case OMPC_copyin:
2448 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2449 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002450 case OMPC_copyprivate:
2451 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2452 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002453 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002454 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002455 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002456 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002457 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002458 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002459 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002460 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002461 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002462 case OMPC_threadprivate:
2463 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002464 llvm_unreachable("Clause is not allowed.");
2465 }
2466 return Res;
2467}
2468
2469OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2470 SourceLocation StartLoc,
2471 SourceLocation LParenLoc,
2472 SourceLocation EndLoc) {
2473 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002474 for (auto &RefExpr : VarList) {
2475 assert(RefExpr && "NULL expr in OpenMP private clause.");
2476 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002477 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002478 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002479 continue;
2480 }
2481
Alexey Bataeved09d242014-05-28 05:53:51 +00002482 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002483 // OpenMP [2.1, C/C++]
2484 // A list item is a variable name.
2485 // OpenMP [2.9.3.3, Restrictions, p.1]
2486 // A variable that is part of another variable (as an array or
2487 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002488 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002489 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002490 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002491 continue;
2492 }
2493 Decl *D = DE->getDecl();
2494 VarDecl *VD = cast<VarDecl>(D);
2495
2496 QualType Type = VD->getType();
2497 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2498 // It will be analyzed later.
2499 Vars.push_back(DE);
2500 continue;
2501 }
2502
2503 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2504 // A variable that appears in a private clause must not have an incomplete
2505 // type or a reference type.
2506 if (RequireCompleteType(ELoc, Type,
2507 diag::err_omp_private_incomplete_type)) {
2508 continue;
2509 }
2510 if (Type->isReferenceType()) {
2511 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002512 << getOpenMPClauseName(OMPC_private) << Type;
2513 bool IsDecl =
2514 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2515 Diag(VD->getLocation(),
2516 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2517 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002518 continue;
2519 }
2520
2521 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2522 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002523 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002524 // class type.
2525 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002526 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2527 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002528 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002529 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2530 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2531 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002532 // FIXME This code must be replaced by actual constructing/destructing of
2533 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002534 if (RD) {
2535 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2536 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002537 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002538 if (!CD ||
2539 CheckConstructorAccess(ELoc, CD,
2540 InitializedEntity::InitializeTemporary(Type),
2541 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002542 CD->isDeleted()) {
2543 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002544 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002545 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2546 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002547 Diag(VD->getLocation(),
2548 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2549 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002550 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2551 continue;
2552 }
2553 MarkFunctionReferenced(ELoc, CD);
2554 DiagnoseUseOfDecl(CD, ELoc);
2555
2556 CXXDestructorDecl *DD = RD->getDestructor();
2557 if (DD) {
2558 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2559 DD->isDeleted()) {
2560 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002561 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002562 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2563 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002564 Diag(VD->getLocation(),
2565 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2566 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002567 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2568 continue;
2569 }
2570 MarkFunctionReferenced(ELoc, DD);
2571 DiagnoseUseOfDecl(DD, ELoc);
2572 }
2573 }
2574
Alexey Bataev758e55e2013-09-06 18:03:48 +00002575 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2576 // in a Construct]
2577 // Variables with the predetermined data-sharing attributes may not be
2578 // listed in data-sharing attributes clauses, except for the cases
2579 // listed below. For these exceptions only, listing a predetermined
2580 // variable in a data-sharing attribute clause is allowed and overrides
2581 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002582 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002583 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002584 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2585 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002586 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002587 continue;
2588 }
2589
2590 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002591 Vars.push_back(DE);
2592 }
2593
Alexey Bataeved09d242014-05-28 05:53:51 +00002594 if (Vars.empty())
2595 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002596
2597 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2598}
2599
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002600OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2601 SourceLocation StartLoc,
2602 SourceLocation LParenLoc,
2603 SourceLocation EndLoc) {
2604 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002605 bool IsImplicitClause =
2606 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
2607 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
2608
Alexey Bataeved09d242014-05-28 05:53:51 +00002609 for (auto &RefExpr : VarList) {
2610 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2611 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002612 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002613 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002614 continue;
2615 }
2616
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002617 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
2618 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002619 // OpenMP [2.1, C/C++]
2620 // A list item is a variable name.
2621 // OpenMP [2.9.3.3, Restrictions, p.1]
2622 // A variable that is part of another variable (as an array or
2623 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002624 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002625 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002626 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002627 continue;
2628 }
2629 Decl *D = DE->getDecl();
2630 VarDecl *VD = cast<VarDecl>(D);
2631
2632 QualType Type = VD->getType();
2633 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2634 // It will be analyzed later.
2635 Vars.push_back(DE);
2636 continue;
2637 }
2638
2639 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2640 // A variable that appears in a private clause must not have an incomplete
2641 // type or a reference type.
2642 if (RequireCompleteType(ELoc, Type,
2643 diag::err_omp_firstprivate_incomplete_type)) {
2644 continue;
2645 }
2646 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002647 if (IsImplicitClause) {
2648 Diag(ImplicitClauseLoc,
2649 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
2650 << Type;
2651 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2652 } else {
2653 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2654 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2655 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002656 bool IsDecl =
2657 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2658 Diag(VD->getLocation(),
2659 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2660 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002661 continue;
2662 }
2663
2664 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2665 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002666 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002667 // class type.
2668 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002669 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2670 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2671 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002672 // FIXME This code must be replaced by actual constructing/destructing of
2673 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002674 if (RD) {
2675 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2676 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002677 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002678 if (!CD ||
2679 CheckConstructorAccess(ELoc, CD,
2680 InitializedEntity::InitializeTemporary(Type),
2681 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002682 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002683 if (IsImplicitClause) {
2684 Diag(ImplicitClauseLoc,
2685 diag::err_omp_task_predetermined_firstprivate_required_method)
2686 << 0;
2687 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2688 } else {
2689 Diag(ELoc, diag::err_omp_required_method)
2690 << getOpenMPClauseName(OMPC_firstprivate) << 1;
2691 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002692 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2693 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002694 Diag(VD->getLocation(),
2695 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2696 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002697 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2698 continue;
2699 }
2700 MarkFunctionReferenced(ELoc, CD);
2701 DiagnoseUseOfDecl(CD, ELoc);
2702
2703 CXXDestructorDecl *DD = RD->getDestructor();
2704 if (DD) {
2705 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2706 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002707 if (IsImplicitClause) {
2708 Diag(ImplicitClauseLoc,
2709 diag::err_omp_task_predetermined_firstprivate_required_method)
2710 << 1;
2711 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2712 } else {
2713 Diag(ELoc, diag::err_omp_required_method)
2714 << getOpenMPClauseName(OMPC_firstprivate) << 4;
2715 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002716 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2717 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002718 Diag(VD->getLocation(),
2719 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2720 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002721 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2722 continue;
2723 }
2724 MarkFunctionReferenced(ELoc, DD);
2725 DiagnoseUseOfDecl(DD, ELoc);
2726 }
2727 }
2728
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002729 // If an implicit firstprivate variable found it was checked already.
2730 if (!IsImplicitClause) {
2731 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002732 Type = Type.getNonReferenceType().getCanonicalType();
2733 bool IsConstant = Type.isConstant(Context);
2734 Type = Context.getBaseElementType(Type);
2735 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2736 // A list item that specifies a given variable may not appear in more
2737 // than one clause on the same directive, except that a variable may be
2738 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002739 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002740 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002741 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002742 << getOpenMPClauseName(DVar.CKind)
2743 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002744 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002745 continue;
2746 }
2747
2748 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2749 // in a Construct]
2750 // Variables with the predetermined data-sharing attributes may not be
2751 // listed in data-sharing attributes clauses, except for the cases
2752 // listed below. For these exceptions only, listing a predetermined
2753 // variable in a data-sharing attribute clause is allowed and overrides
2754 // the variable's predetermined data-sharing attributes.
2755 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2756 // in a Construct, C/C++, p.2]
2757 // Variables with const-qualified type having no mutable member may be
2758 // listed in a firstprivate clause, even if they are static data members.
2759 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2760 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2761 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002762 << getOpenMPClauseName(DVar.CKind)
2763 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002764 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002765 continue;
2766 }
2767
Alexey Bataevf29276e2014-06-18 04:14:57 +00002768 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002769 // OpenMP [2.9.3.4, Restrictions, p.2]
2770 // A list item that is private within a parallel region must not appear
2771 // in a firstprivate clause on a worksharing construct if any of the
2772 // worksharing regions arising from the worksharing construct ever bind
2773 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002774 if (isOpenMPWorksharingDirective(CurrDir) &&
2775 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002776 DVar = DSAStack->getImplicitDSA(VD, true);
2777 if (DVar.CKind != OMPC_shared &&
2778 (isOpenMPParallelDirective(DVar.DKind) ||
2779 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002780 Diag(ELoc, diag::err_omp_required_access)
2781 << getOpenMPClauseName(OMPC_firstprivate)
2782 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002783 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002784 continue;
2785 }
2786 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002787 // OpenMP [2.9.3.4, Restrictions, p.3]
2788 // A list item that appears in a reduction clause of a parallel construct
2789 // must not appear in a firstprivate clause on a worksharing or task
2790 // construct if any of the worksharing or task regions arising from the
2791 // worksharing or task construct ever bind to any of the parallel regions
2792 // arising from the parallel construct.
2793 // OpenMP [2.9.3.4, Restrictions, p.4]
2794 // A list item that appears in a reduction clause in worksharing
2795 // construct must not appear in a firstprivate clause in a task construct
2796 // encountered during execution of any of the worksharing regions arising
2797 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002798 if (CurrDir == OMPD_task) {
2799 DVar =
2800 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
2801 [](OpenMPDirectiveKind K) -> bool {
2802 return isOpenMPParallelDirective(K) ||
2803 isOpenMPWorksharingDirective(K);
2804 },
2805 false);
2806 if (DVar.CKind == OMPC_reduction &&
2807 (isOpenMPParallelDirective(DVar.DKind) ||
2808 isOpenMPWorksharingDirective(DVar.DKind))) {
2809 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
2810 << getOpenMPDirectiveName(DVar.DKind);
2811 ReportOriginalDSA(*this, DSAStack, VD, DVar);
2812 continue;
2813 }
2814 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002815 }
2816
2817 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2818 Vars.push_back(DE);
2819 }
2820
Alexey Bataeved09d242014-05-28 05:53:51 +00002821 if (Vars.empty())
2822 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002823
2824 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2825 Vars);
2826}
2827
Alexander Musman1bb328c2014-06-04 13:06:39 +00002828OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2829 SourceLocation StartLoc,
2830 SourceLocation LParenLoc,
2831 SourceLocation EndLoc) {
2832 SmallVector<Expr *, 8> Vars;
2833 for (auto &RefExpr : VarList) {
2834 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2835 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2836 // It will be analyzed later.
2837 Vars.push_back(RefExpr);
2838 continue;
2839 }
2840
2841 SourceLocation ELoc = RefExpr->getExprLoc();
2842 // OpenMP [2.1, C/C++]
2843 // A list item is a variable name.
2844 // OpenMP [2.14.3.5, Restrictions, p.1]
2845 // A variable that is part of another variable (as an array or structure
2846 // element) cannot appear in a lastprivate clause.
2847 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2848 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2849 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2850 continue;
2851 }
2852 Decl *D = DE->getDecl();
2853 VarDecl *VD = cast<VarDecl>(D);
2854
2855 QualType Type = VD->getType();
2856 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2857 // It will be analyzed later.
2858 Vars.push_back(DE);
2859 continue;
2860 }
2861
2862 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2863 // A variable that appears in a lastprivate clause must not have an
2864 // incomplete type or a reference type.
2865 if (RequireCompleteType(ELoc, Type,
2866 diag::err_omp_lastprivate_incomplete_type)) {
2867 continue;
2868 }
2869 if (Type->isReferenceType()) {
2870 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2871 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2872 bool IsDecl =
2873 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2874 Diag(VD->getLocation(),
2875 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2876 << VD;
2877 continue;
2878 }
2879
2880 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2881 // in a Construct]
2882 // Variables with the predetermined data-sharing attributes may not be
2883 // listed in data-sharing attributes clauses, except for the cases
2884 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002885 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002886 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2887 DVar.CKind != OMPC_firstprivate &&
2888 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2889 Diag(ELoc, diag::err_omp_wrong_dsa)
2890 << getOpenMPClauseName(DVar.CKind)
2891 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002892 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002893 continue;
2894 }
2895
Alexey Bataevf29276e2014-06-18 04:14:57 +00002896 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2897 // OpenMP [2.14.3.5, Restrictions, p.2]
2898 // A list item that is private within a parallel region, or that appears in
2899 // the reduction clause of a parallel construct, must not appear in a
2900 // lastprivate clause on a worksharing construct if any of the corresponding
2901 // worksharing regions ever binds to any of the corresponding parallel
2902 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002903 if (isOpenMPWorksharingDirective(CurrDir) &&
2904 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002905 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002906 if (DVar.CKind != OMPC_shared) {
2907 Diag(ELoc, diag::err_omp_required_access)
2908 << getOpenMPClauseName(OMPC_lastprivate)
2909 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002910 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002911 continue;
2912 }
2913 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002914 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002915 // A variable of class type (or array thereof) that appears in a
2916 // lastprivate clause requires an accessible, unambiguous default
2917 // constructor for the class type, unless the list item is also specified
2918 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002919 // A variable of class type (or array thereof) that appears in a
2920 // lastprivate clause requires an accessible, unambiguous copy assignment
2921 // operator for the class type.
2922 while (Type.getNonReferenceType()->isArrayType())
2923 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2924 ->getElementType();
2925 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2926 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2927 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002928 // FIXME This code must be replaced by actual copying and destructing of the
2929 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002930 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002931 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2932 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002933 if (MD) {
2934 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2935 MD->isDeleted()) {
2936 Diag(ELoc, diag::err_omp_required_method)
2937 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2938 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2939 VarDecl::DeclarationOnly;
2940 Diag(VD->getLocation(),
2941 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2942 << VD;
2943 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2944 continue;
2945 }
2946 MarkFunctionReferenced(ELoc, MD);
2947 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002948 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002949
2950 CXXDestructorDecl *DD = RD->getDestructor();
2951 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002952 PartialDiagnostic PD =
2953 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002954 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2955 DD->isDeleted()) {
2956 Diag(ELoc, diag::err_omp_required_method)
2957 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2958 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2959 VarDecl::DeclarationOnly;
2960 Diag(VD->getLocation(),
2961 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2962 << VD;
2963 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2964 continue;
2965 }
2966 MarkFunctionReferenced(ELoc, DD);
2967 DiagnoseUseOfDecl(DD, ELoc);
2968 }
2969 }
2970
Alexey Bataevf29276e2014-06-18 04:14:57 +00002971 if (DVar.CKind != OMPC_firstprivate)
2972 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002973 Vars.push_back(DE);
2974 }
2975
2976 if (Vars.empty())
2977 return nullptr;
2978
2979 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2980 Vars);
2981}
2982
Alexey Bataev758e55e2013-09-06 18:03:48 +00002983OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2984 SourceLocation StartLoc,
2985 SourceLocation LParenLoc,
2986 SourceLocation EndLoc) {
2987 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002988 for (auto &RefExpr : VarList) {
2989 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2990 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002991 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002992 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002993 continue;
2994 }
2995
Alexey Bataeved09d242014-05-28 05:53:51 +00002996 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002997 // OpenMP [2.1, C/C++]
2998 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002999 // OpenMP [2.14.3.2, Restrictions, p.1]
3000 // A variable that is part of another variable (as an array or structure
3001 // element) cannot appear in a shared unless it is a static data member
3002 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003003 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003004 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003005 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003006 continue;
3007 }
3008 Decl *D = DE->getDecl();
3009 VarDecl *VD = cast<VarDecl>(D);
3010
3011 QualType Type = VD->getType();
3012 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3013 // It will be analyzed later.
3014 Vars.push_back(DE);
3015 continue;
3016 }
3017
3018 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3019 // in a Construct]
3020 // Variables with the predetermined data-sharing attributes may not be
3021 // listed in data-sharing attributes clauses, except for the cases
3022 // listed below. For these exceptions only, listing a predetermined
3023 // variable in a data-sharing attribute clause is allowed and overrides
3024 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003025 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003026 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3027 DVar.RefExpr) {
3028 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3029 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003030 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003031 continue;
3032 }
3033
3034 DSAStack->addDSA(VD, DE, OMPC_shared);
3035 Vars.push_back(DE);
3036 }
3037
Alexey Bataeved09d242014-05-28 05:53:51 +00003038 if (Vars.empty())
3039 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003040
3041 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3042}
3043
Alexey Bataevc5e02582014-06-16 07:08:35 +00003044namespace {
3045class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3046 DSAStackTy *Stack;
3047
3048public:
3049 bool VisitDeclRefExpr(DeclRefExpr *E) {
3050 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003051 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003052 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3053 return false;
3054 if (DVar.CKind != OMPC_unknown)
3055 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003056 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003057 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003058 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003059 return true;
3060 return false;
3061 }
3062 return false;
3063 }
3064 bool VisitStmt(Stmt *S) {
3065 for (auto Child : S->children()) {
3066 if (Child && Visit(Child))
3067 return true;
3068 }
3069 return false;
3070 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003071 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003072};
Alexey Bataev23b69422014-06-18 07:08:49 +00003073} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003074
3075OMPClause *Sema::ActOnOpenMPReductionClause(
3076 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3077 SourceLocation ColonLoc, SourceLocation EndLoc,
3078 CXXScopeSpec &ReductionIdScopeSpec,
3079 const DeclarationNameInfo &ReductionId) {
3080 // TODO: Allow scope specification search when 'declare reduction' is
3081 // supported.
3082 assert(ReductionIdScopeSpec.isEmpty() &&
3083 "No support for scoped reduction identifiers yet.");
3084
3085 auto DN = ReductionId.getName();
3086 auto OOK = DN.getCXXOverloadedOperator();
3087 BinaryOperatorKind BOK = BO_Comma;
3088
3089 // OpenMP [2.14.3.6, reduction clause]
3090 // C
3091 // reduction-identifier is either an identifier or one of the following
3092 // operators: +, -, *, &, |, ^, && and ||
3093 // C++
3094 // reduction-identifier is either an id-expression or one of the following
3095 // operators: +, -, *, &, |, ^, && and ||
3096 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3097 switch (OOK) {
3098 case OO_Plus:
3099 case OO_Minus:
3100 BOK = BO_AddAssign;
3101 break;
3102 case OO_Star:
3103 BOK = BO_MulAssign;
3104 break;
3105 case OO_Amp:
3106 BOK = BO_AndAssign;
3107 break;
3108 case OO_Pipe:
3109 BOK = BO_OrAssign;
3110 break;
3111 case OO_Caret:
3112 BOK = BO_XorAssign;
3113 break;
3114 case OO_AmpAmp:
3115 BOK = BO_LAnd;
3116 break;
3117 case OO_PipePipe:
3118 BOK = BO_LOr;
3119 break;
3120 default:
3121 if (auto II = DN.getAsIdentifierInfo()) {
3122 if (II->isStr("max"))
3123 BOK = BO_GT;
3124 else if (II->isStr("min"))
3125 BOK = BO_LT;
3126 }
3127 break;
3128 }
3129 SourceRange ReductionIdRange;
3130 if (ReductionIdScopeSpec.isValid()) {
3131 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3132 }
3133 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3134 if (BOK == BO_Comma) {
3135 // Not allowed reduction identifier is found.
3136 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3137 << ReductionIdRange;
3138 return nullptr;
3139 }
3140
3141 SmallVector<Expr *, 8> Vars;
3142 for (auto RefExpr : VarList) {
3143 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3144 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3145 // It will be analyzed later.
3146 Vars.push_back(RefExpr);
3147 continue;
3148 }
3149
3150 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3151 RefExpr->isInstantiationDependent() ||
3152 RefExpr->containsUnexpandedParameterPack()) {
3153 // It will be analyzed later.
3154 Vars.push_back(RefExpr);
3155 continue;
3156 }
3157
3158 auto ELoc = RefExpr->getExprLoc();
3159 auto ERange = RefExpr->getSourceRange();
3160 // OpenMP [2.1, C/C++]
3161 // A list item is a variable or array section, subject to the restrictions
3162 // specified in Section 2.4 on page 42 and in each of the sections
3163 // describing clauses and directives for which a list appears.
3164 // OpenMP [2.14.3.3, Restrictions, p.1]
3165 // A variable that is part of another variable (as an array or
3166 // structure element) cannot appear in a private clause.
3167 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3168 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3169 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3170 continue;
3171 }
3172 auto D = DE->getDecl();
3173 auto VD = cast<VarDecl>(D);
3174 auto Type = VD->getType();
3175 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3176 // A variable that appears in a private clause must not have an incomplete
3177 // type or a reference type.
3178 if (RequireCompleteType(ELoc, Type,
3179 diag::err_omp_reduction_incomplete_type))
3180 continue;
3181 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3182 // Arrays may not appear in a reduction clause.
3183 if (Type.getNonReferenceType()->isArrayType()) {
3184 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3185 bool IsDecl =
3186 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3187 Diag(VD->getLocation(),
3188 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3189 << VD;
3190 continue;
3191 }
3192 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3193 // A list item that appears in a reduction clause must not be
3194 // const-qualified.
3195 if (Type.getNonReferenceType().isConstant(Context)) {
3196 Diag(ELoc, diag::err_omp_const_variable)
3197 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3198 bool IsDecl =
3199 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3200 Diag(VD->getLocation(),
3201 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3202 << VD;
3203 continue;
3204 }
3205 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3206 // If a list-item is a reference type then it must bind to the same object
3207 // for all threads of the team.
3208 VarDecl *VDDef = VD->getDefinition();
3209 if (Type->isReferenceType() && VDDef) {
3210 DSARefChecker Check(DSAStack);
3211 if (Check.Visit(VDDef->getInit())) {
3212 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3213 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3214 continue;
3215 }
3216 }
3217 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3218 // The type of a list item that appears in a reduction clause must be valid
3219 // for the reduction-identifier. For a max or min reduction in C, the type
3220 // of the list item must be an allowed arithmetic data type: char, int,
3221 // float, double, or _Bool, possibly modified with long, short, signed, or
3222 // unsigned. For a max or min reduction in C++, the type of the list item
3223 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3224 // double, or bool, possibly modified with long, short, signed, or unsigned.
3225 if ((BOK == BO_GT || BOK == BO_LT) &&
3226 !(Type->isScalarType() ||
3227 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3228 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3229 << getLangOpts().CPlusPlus;
3230 bool IsDecl =
3231 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3232 Diag(VD->getLocation(),
3233 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3234 << VD;
3235 continue;
3236 }
3237 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3238 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3239 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3240 bool IsDecl =
3241 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3242 Diag(VD->getLocation(),
3243 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3244 << VD;
3245 continue;
3246 }
3247 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3248 getDiagnostics().setSuppressAllDiagnostics(true);
3249 ExprResult ReductionOp =
3250 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3251 RefExpr, RefExpr);
3252 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3253 if (ReductionOp.isInvalid()) {
3254 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003255 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003256 bool IsDecl =
3257 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3258 Diag(VD->getLocation(),
3259 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3260 << VD;
3261 continue;
3262 }
3263
3264 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3265 // in a Construct]
3266 // Variables with the predetermined data-sharing attributes may not be
3267 // listed in data-sharing attributes clauses, except for the cases
3268 // listed below. For these exceptions only, listing a predetermined
3269 // variable in a data-sharing attribute clause is allowed and overrides
3270 // the variable's predetermined data-sharing attributes.
3271 // OpenMP [2.14.3.6, Restrictions, p.3]
3272 // Any number of reduction clauses can be specified on the directive,
3273 // but a list item can appear only once in the reduction clauses for that
3274 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003275 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003276 if (DVar.CKind == OMPC_reduction) {
3277 Diag(ELoc, diag::err_omp_once_referenced)
3278 << getOpenMPClauseName(OMPC_reduction);
3279 if (DVar.RefExpr) {
3280 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3281 }
3282 } else if (DVar.CKind != OMPC_unknown) {
3283 Diag(ELoc, diag::err_omp_wrong_dsa)
3284 << getOpenMPClauseName(DVar.CKind)
3285 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003286 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003287 continue;
3288 }
3289
3290 // OpenMP [2.14.3.6, Restrictions, p.1]
3291 // A list item that appears in a reduction clause of a worksharing
3292 // construct must be shared in the parallel regions to which any of the
3293 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003294 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003295 if (isOpenMPWorksharingDirective(CurrDir) &&
3296 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003297 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003298 if (DVar.CKind != OMPC_shared) {
3299 Diag(ELoc, diag::err_omp_required_access)
3300 << getOpenMPClauseName(OMPC_reduction)
3301 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003302 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003303 continue;
3304 }
3305 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003306
3307 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3308 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3309 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003310 // FIXME This code must be replaced by actual constructing/destructing of
3311 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003312 if (RD) {
3313 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3314 PartialDiagnostic PD =
3315 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003316 if (!CD ||
3317 CheckConstructorAccess(ELoc, CD,
3318 InitializedEntity::InitializeTemporary(Type),
3319 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003320 CD->isDeleted()) {
3321 Diag(ELoc, diag::err_omp_required_method)
3322 << getOpenMPClauseName(OMPC_reduction) << 0;
3323 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3324 VarDecl::DeclarationOnly;
3325 Diag(VD->getLocation(),
3326 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3327 << VD;
3328 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3329 continue;
3330 }
3331 MarkFunctionReferenced(ELoc, CD);
3332 DiagnoseUseOfDecl(CD, ELoc);
3333
3334 CXXDestructorDecl *DD = RD->getDestructor();
3335 if (DD) {
3336 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3337 DD->isDeleted()) {
3338 Diag(ELoc, diag::err_omp_required_method)
3339 << getOpenMPClauseName(OMPC_reduction) << 4;
3340 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3341 VarDecl::DeclarationOnly;
3342 Diag(VD->getLocation(),
3343 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3344 << VD;
3345 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3346 continue;
3347 }
3348 MarkFunctionReferenced(ELoc, DD);
3349 DiagnoseUseOfDecl(DD, ELoc);
3350 }
3351 }
3352
3353 DSAStack->addDSA(VD, DE, OMPC_reduction);
3354 Vars.push_back(DE);
3355 }
3356
3357 if (Vars.empty())
3358 return nullptr;
3359
3360 return OMPReductionClause::Create(
3361 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3362 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3363}
3364
Alexander Musman8dba6642014-04-22 13:09:42 +00003365OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3366 SourceLocation StartLoc,
3367 SourceLocation LParenLoc,
3368 SourceLocation ColonLoc,
3369 SourceLocation EndLoc) {
3370 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003371 for (auto &RefExpr : VarList) {
3372 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3373 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003374 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003375 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003376 continue;
3377 }
3378
3379 // OpenMP [2.14.3.7, linear clause]
3380 // A list item that appears in a linear clause is subject to the private
3381 // clause semantics described in Section 2.14.3.3 on page 159 except as
3382 // noted. In addition, the value of the new list item on each iteration
3383 // of the associated loop(s) corresponds to the value of the original
3384 // list item before entering the construct plus the logical number of
3385 // the iteration times linear-step.
3386
Alexey Bataeved09d242014-05-28 05:53:51 +00003387 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003388 // OpenMP [2.1, C/C++]
3389 // A list item is a variable name.
3390 // OpenMP [2.14.3.3, Restrictions, p.1]
3391 // A variable that is part of another variable (as an array or
3392 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003393 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003394 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003395 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003396 continue;
3397 }
3398
3399 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3400
3401 // OpenMP [2.14.3.7, linear clause]
3402 // A list-item cannot appear in more than one linear clause.
3403 // A list-item that appears in a linear clause cannot appear in any
3404 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003405 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003406 if (DVar.RefExpr) {
3407 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3408 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003409 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003410 continue;
3411 }
3412
3413 QualType QType = VD->getType();
3414 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3415 // It will be analyzed later.
3416 Vars.push_back(DE);
3417 continue;
3418 }
3419
3420 // A variable must not have an incomplete type or a reference type.
3421 if (RequireCompleteType(ELoc, QType,
3422 diag::err_omp_linear_incomplete_type)) {
3423 continue;
3424 }
3425 if (QType->isReferenceType()) {
3426 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3427 << getOpenMPClauseName(OMPC_linear) << QType;
3428 bool IsDecl =
3429 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3430 Diag(VD->getLocation(),
3431 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3432 << VD;
3433 continue;
3434 }
3435
3436 // A list item must not be const-qualified.
3437 if (QType.isConstant(Context)) {
3438 Diag(ELoc, diag::err_omp_const_variable)
3439 << getOpenMPClauseName(OMPC_linear);
3440 bool IsDecl =
3441 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3442 Diag(VD->getLocation(),
3443 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3444 << VD;
3445 continue;
3446 }
3447
3448 // A list item must be of integral or pointer type.
3449 QType = QType.getUnqualifiedType().getCanonicalType();
3450 const Type *Ty = QType.getTypePtrOrNull();
3451 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3452 !Ty->isPointerType())) {
3453 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3454 bool IsDecl =
3455 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3456 Diag(VD->getLocation(),
3457 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3458 << VD;
3459 continue;
3460 }
3461
3462 DSAStack->addDSA(VD, DE, OMPC_linear);
3463 Vars.push_back(DE);
3464 }
3465
3466 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003467 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003468
3469 Expr *StepExpr = Step;
3470 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3471 !Step->isInstantiationDependent() &&
3472 !Step->containsUnexpandedParameterPack()) {
3473 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003474 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003475 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003476 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003477 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003478
3479 // Warn about zero linear step (it would be probably better specified as
3480 // making corresponding variables 'const').
3481 llvm::APSInt Result;
3482 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3483 !Result.isNegative() && !Result.isStrictlyPositive())
3484 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3485 << (Vars.size() > 1);
3486 }
3487
3488 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3489 Vars, StepExpr);
3490}
3491
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003492OMPClause *Sema::ActOnOpenMPAlignedClause(
3493 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3494 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3495
3496 SmallVector<Expr *, 8> Vars;
3497 for (auto &RefExpr : VarList) {
3498 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3499 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3500 // It will be analyzed later.
3501 Vars.push_back(RefExpr);
3502 continue;
3503 }
3504
3505 SourceLocation ELoc = RefExpr->getExprLoc();
3506 // OpenMP [2.1, C/C++]
3507 // A list item is a variable name.
3508 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3509 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3510 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3511 continue;
3512 }
3513
3514 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3515
3516 // OpenMP [2.8.1, simd construct, Restrictions]
3517 // The type of list items appearing in the aligned clause must be
3518 // array, pointer, reference to array, or reference to pointer.
3519 QualType QType = DE->getType()
3520 .getNonReferenceType()
3521 .getUnqualifiedType()
3522 .getCanonicalType();
3523 const Type *Ty = QType.getTypePtrOrNull();
3524 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3525 !Ty->isPointerType())) {
3526 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3527 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3528 bool IsDecl =
3529 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3530 Diag(VD->getLocation(),
3531 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3532 << VD;
3533 continue;
3534 }
3535
3536 // OpenMP [2.8.1, simd construct, Restrictions]
3537 // A list-item cannot appear in more than one aligned clause.
3538 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3539 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3540 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3541 << getOpenMPClauseName(OMPC_aligned);
3542 continue;
3543 }
3544
3545 Vars.push_back(DE);
3546 }
3547
3548 // OpenMP [2.8.1, simd construct, Description]
3549 // The parameter of the aligned clause, alignment, must be a constant
3550 // positive integer expression.
3551 // If no optional parameter is specified, implementation-defined default
3552 // alignments for SIMD instructions on the target platforms are assumed.
3553 if (Alignment != nullptr) {
3554 ExprResult AlignResult =
3555 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3556 if (AlignResult.isInvalid())
3557 return nullptr;
3558 Alignment = AlignResult.get();
3559 }
3560 if (Vars.empty())
3561 return nullptr;
3562
3563 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3564 EndLoc, Vars, Alignment);
3565}
3566
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003567OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3568 SourceLocation StartLoc,
3569 SourceLocation LParenLoc,
3570 SourceLocation EndLoc) {
3571 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003572 for (auto &RefExpr : VarList) {
3573 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3574 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003575 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003576 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003577 continue;
3578 }
3579
Alexey Bataeved09d242014-05-28 05:53:51 +00003580 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003581 // OpenMP [2.1, C/C++]
3582 // A list item is a variable name.
3583 // OpenMP [2.14.4.1, Restrictions, p.1]
3584 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003585 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003586 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003587 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003588 continue;
3589 }
3590
3591 Decl *D = DE->getDecl();
3592 VarDecl *VD = cast<VarDecl>(D);
3593
3594 QualType Type = VD->getType();
3595 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3596 // It will be analyzed later.
3597 Vars.push_back(DE);
3598 continue;
3599 }
3600
3601 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3602 // A list item that appears in a copyin clause must be threadprivate.
3603 if (!DSAStack->isThreadPrivate(VD)) {
3604 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003605 << getOpenMPClauseName(OMPC_copyin)
3606 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003607 continue;
3608 }
3609
3610 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3611 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003612 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003613 // operator for the class type.
3614 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003615 CXXRecordDecl *RD =
3616 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003617 // FIXME This code must be replaced by actual assignment of the
3618 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003619 if (RD) {
3620 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3621 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003622 if (MD) {
3623 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3624 MD->isDeleted()) {
3625 Diag(ELoc, diag::err_omp_required_method)
3626 << getOpenMPClauseName(OMPC_copyin) << 2;
3627 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3628 VarDecl::DeclarationOnly;
3629 Diag(VD->getLocation(),
3630 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3631 << VD;
3632 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3633 continue;
3634 }
3635 MarkFunctionReferenced(ELoc, MD);
3636 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003637 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003638 }
3639
3640 DSAStack->addDSA(VD, DE, OMPC_copyin);
3641 Vars.push_back(DE);
3642 }
3643
Alexey Bataeved09d242014-05-28 05:53:51 +00003644 if (Vars.empty())
3645 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003646
3647 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3648}
3649
Alexey Bataevbae9a792014-06-27 10:37:06 +00003650OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
3651 SourceLocation StartLoc,
3652 SourceLocation LParenLoc,
3653 SourceLocation EndLoc) {
3654 SmallVector<Expr *, 8> Vars;
3655 for (auto &RefExpr : VarList) {
3656 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
3657 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3658 // It will be analyzed later.
3659 Vars.push_back(RefExpr);
3660 continue;
3661 }
3662
3663 SourceLocation ELoc = RefExpr->getExprLoc();
3664 // OpenMP [2.1, C/C++]
3665 // A list item is a variable name.
3666 // OpenMP [2.14.4.1, Restrictions, p.1]
3667 // A list item that appears in a copyin clause must be threadprivate.
3668 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3669 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3670 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3671 continue;
3672 }
3673
3674 Decl *D = DE->getDecl();
3675 VarDecl *VD = cast<VarDecl>(D);
3676
3677 QualType Type = VD->getType();
3678 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3679 // It will be analyzed later.
3680 Vars.push_back(DE);
3681 continue;
3682 }
3683
3684 // OpenMP [2.14.4.2, Restrictions, p.2]
3685 // A list item that appears in a copyprivate clause may not appear in a
3686 // private or firstprivate clause on the single construct.
3687 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003688 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003689 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
3690 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
3691 Diag(ELoc, diag::err_omp_wrong_dsa)
3692 << getOpenMPClauseName(DVar.CKind)
3693 << getOpenMPClauseName(OMPC_copyprivate);
3694 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3695 continue;
3696 }
3697
3698 // OpenMP [2.11.4.2, Restrictions, p.1]
3699 // All list items that appear in a copyprivate clause must be either
3700 // threadprivate or private in the enclosing context.
3701 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003702 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003703 if (DVar.CKind == OMPC_shared) {
3704 Diag(ELoc, diag::err_omp_required_access)
3705 << getOpenMPClauseName(OMPC_copyprivate)
3706 << "threadprivate or private in the enclosing context";
3707 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3708 continue;
3709 }
3710 }
3711 }
3712
3713 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3714 // A variable of class type (or array thereof) that appears in a
3715 // copyin clause requires an accessible, unambiguous copy assignment
3716 // operator for the class type.
3717 Type = Context.getBaseElementType(Type);
3718 CXXRecordDecl *RD =
3719 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
3720 // FIXME This code must be replaced by actual assignment of the
3721 // threadprivate variable.
3722 if (RD) {
3723 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3724 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
3725 if (MD) {
3726 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3727 MD->isDeleted()) {
3728 Diag(ELoc, diag::err_omp_required_method)
3729 << getOpenMPClauseName(OMPC_copyprivate) << 2;
3730 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3731 VarDecl::DeclarationOnly;
3732 Diag(VD->getLocation(),
3733 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3734 << VD;
3735 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3736 continue;
3737 }
3738 MarkFunctionReferenced(ELoc, MD);
3739 DiagnoseUseOfDecl(MD, ELoc);
3740 }
3741 }
3742
3743 // No need to mark vars as copyprivate, they are already threadprivate or
3744 // implicitly private.
3745 Vars.push_back(DE);
3746 }
3747
3748 if (Vars.empty())
3749 return nullptr;
3750
3751 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3752}
3753
Alexey Bataev758e55e2013-09-06 18:03:48 +00003754#undef DSAStack