blob: 85a9ccffd8c05570e700041c599c7bfe3ba72c53 [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 }
Alexander Musman80c22892014-07-17 08:54:58 +00001003 case OMPD_master: {
1004 Sema::CapturedParamNameType Params[] = {
1005 std::make_pair(StringRef(), QualType()) // __context with shared vars
1006 };
1007 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1008 Params);
1009 break;
1010 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001011 case OMPD_parallel_for: {
1012 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1013 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1014 Sema::CapturedParamNameType Params[] = {
1015 std::make_pair(".global_tid.", KmpInt32PtrTy),
1016 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1017 std::make_pair(StringRef(), QualType()) // __context with shared vars
1018 };
1019 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1020 Params);
1021 break;
1022 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001023 case OMPD_parallel_sections: {
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 Bataev9c2e8ee2014-07-11 11:25:16 +00001031 case OMPD_task: {
1032 Sema::CapturedParamNameType Params[] = {
1033 std::make_pair(StringRef(), QualType()) // __context with shared vars
1034 };
1035 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1036 Params);
1037 break;
1038 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001039 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001040 llvm_unreachable("OpenMP Directive is not allowed");
1041 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001042 llvm_unreachable("Unknown OpenMP directive");
1043 }
1044}
1045
Alexey Bataev549210e2014-06-24 04:39:47 +00001046bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1047 OpenMPDirectiveKind CurrentRegion,
1048 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001049 // Allowed nesting of constructs
1050 // +------------------+-----------------+------------------------------------+
1051 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1052 // +------------------+-----------------+------------------------------------+
1053 // | parallel | parallel | * |
1054 // | parallel | for | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001055 // | parallel | master | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001056 // | parallel | simd | * |
1057 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001058 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001059 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001060 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001061 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001062 // | parallel | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001063 // +------------------+-----------------+------------------------------------+
1064 // | for | parallel | * |
1065 // | for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001066 // | for | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001067 // | for | simd | * |
1068 // | for | sections | + |
1069 // | for | section | + |
1070 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001071 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001072 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001073 // | for | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001074 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001075 // | master | parallel | * |
1076 // | master | for | + |
1077 // | master | master | * |
1078 // | master | simd | * |
1079 // | master | sections | + |
1080 // | master | section | + |
1081 // | master | single | + |
1082 // | master | parallel for | * |
1083 // | master |parallel sections| * |
1084 // | master | task | * |
1085 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001086 // | simd | parallel | |
1087 // | simd | for | |
Alexander Musman80c22892014-07-17 08:54:58 +00001088 // | simd | master | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001089 // | simd | simd | |
1090 // | simd | sections | |
1091 // | simd | section | |
1092 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001093 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001094 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001095 // | simd | task | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001096 // +------------------+-----------------+------------------------------------+
1097 // | sections | parallel | * |
1098 // | sections | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001099 // | sections | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001100 // | sections | simd | * |
1101 // | sections | sections | + |
1102 // | sections | section | * |
1103 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001104 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001105 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001106 // | sections | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001107 // +------------------+-----------------+------------------------------------+
1108 // | section | parallel | * |
1109 // | section | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001110 // | section | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001111 // | section | simd | * |
1112 // | section | sections | + |
1113 // | section | section | + |
1114 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001115 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001116 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001117 // | section | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001118 // +------------------+-----------------+------------------------------------+
1119 // | single | parallel | * |
1120 // | single | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001121 // | single | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001122 // | single | simd | * |
1123 // | single | sections | + |
1124 // | single | section | + |
1125 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001126 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001127 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001128 // | single | task | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001129 // +------------------+-----------------+------------------------------------+
1130 // | parallel for | parallel | * |
1131 // | parallel for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001132 // | parallel for | master | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001133 // | parallel for | simd | * |
1134 // | parallel for | sections | + |
1135 // | parallel for | section | + |
1136 // | parallel for | single | + |
1137 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001138 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001139 // | parallel for | task | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001140 // +------------------+-----------------+------------------------------------+
1141 // | parallel sections| parallel | * |
1142 // | parallel sections| for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001143 // | parallel sections| master | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001144 // | parallel sections| simd | * |
1145 // | parallel sections| sections | + |
1146 // | parallel sections| section | * |
1147 // | parallel sections| single | + |
1148 // | parallel sections| parallel for | * |
1149 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001150 // | parallel sections| task | * |
1151 // +------------------+-----------------+------------------------------------+
1152 // | task | parallel | * |
1153 // | task | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001154 // | task | master | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001155 // | task | simd | * |
1156 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001157 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001158 // | task | single | + |
1159 // | task | parallel for | * |
1160 // | task |parallel sections| * |
1161 // | task | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001162 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001163 if (Stack->getCurScope()) {
1164 auto ParentRegion = Stack->getParentDirective();
1165 bool NestingProhibited = false;
1166 bool CloseNesting = true;
1167 bool ShouldBeInParallelRegion = false;
1168 if (isOpenMPSimdDirective(ParentRegion)) {
1169 // OpenMP [2.16, Nesting of Regions]
1170 // OpenMP constructs may not be nested inside a simd region.
1171 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1172 return true;
1173 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001174 if (CurrentRegion == OMPD_section) {
1175 // OpenMP [2.7.2, sections Construct, Restrictions]
1176 // Orphaned section directives are prohibited. That is, the section
1177 // directives must appear within the sections construct and must not be
1178 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001179 if (ParentRegion != OMPD_sections &&
1180 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001181 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1182 << (ParentRegion != OMPD_unknown)
1183 << getOpenMPDirectiveName(ParentRegion);
1184 return true;
1185 }
1186 return false;
1187 }
Alexander Musman80c22892014-07-17 08:54:58 +00001188 if (CurrentRegion == OMPD_master) {
1189 // OpenMP [2.16, Nesting of Regions]
1190 // A master region may not be closely nested inside a worksharing,
1191 // atomic (TODO), or explicit task region.
1192 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1193 ParentRegion == OMPD_task;
1194 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1195 !isOpenMPParallelDirective(CurrentRegion) &&
1196 !isOpenMPSimdDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001197 // OpenMP [2.16, Nesting of Regions]
1198 // A worksharing region may not be closely nested inside a worksharing,
1199 // explicit task, critical, ordered, atomic, or master region.
1200 // TODO
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001201 NestingProhibited = (isOpenMPWorksharingDirective(ParentRegion) &&
1202 !isOpenMPSimdDirective(ParentRegion)) ||
Alexander Musman80c22892014-07-17 08:54:58 +00001203 ParentRegion == OMPD_task ||
1204 ParentRegion == OMPD_master;
Alexey Bataev549210e2014-06-24 04:39:47 +00001205 ShouldBeInParallelRegion = true;
1206 }
1207 if (NestingProhibited) {
1208 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev41b97322014-07-02 03:04:53 +00001209 << CloseNesting << getOpenMPDirectiveName(ParentRegion)
1210 << ShouldBeInParallelRegion << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001211 return true;
1212 }
1213 }
1214 return false;
1215}
1216
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001217StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1218 ArrayRef<OMPClause *> Clauses,
1219 Stmt *AStmt,
1220 SourceLocation StartLoc,
1221 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001222 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1223
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001224 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +00001225 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
1226 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001227
1228 // Check default data sharing attributes for referenced variables.
1229 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1230 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1231 if (DSAChecker.isErrorFound())
1232 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001233 // Generate list of implicitly defined firstprivate variables.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001234 auto &VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001235 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1236 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1237
1238 bool ErrorFound = false;
1239 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001240 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1241 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1242 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001243 ClausesWithImplicit.push_back(Implicit);
1244 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +00001245 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001246 } else
1247 ErrorFound = true;
1248 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001249
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001250 switch (Kind) {
1251 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001252 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1253 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001254 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001255 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001256 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1257 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001258 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001259 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001260 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1261 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001262 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001263 case OMPD_sections:
1264 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1265 EndLoc);
1266 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001267 case OMPD_section:
1268 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001269 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001270 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1271 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001272 case OMPD_single:
1273 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1274 EndLoc);
1275 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001276 case OMPD_master:
1277 assert(ClausesWithImplicit.empty() &&
1278 "No clauses are allowed for 'omp master' directive");
1279 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1280 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001281 case OMPD_parallel_for:
1282 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1283 EndLoc, VarsWithInheritedDSA);
1284 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001285 case OMPD_parallel_sections:
1286 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1287 StartLoc, EndLoc);
1288 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001289 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001290 Res =
1291 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1292 break;
1293 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001294 llvm_unreachable("OpenMP Directive is not allowed");
1295 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001296 llvm_unreachable("Unknown OpenMP directive");
1297 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001298
Alexey Bataev4acb8592014-07-07 13:01:15 +00001299 for (auto P : VarsWithInheritedDSA) {
1300 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1301 << P.first << P.second->getSourceRange();
1302 }
1303 if (!VarsWithInheritedDSA.empty())
1304 return StmtError();
1305
Alexey Bataeved09d242014-05-28 05:53:51 +00001306 if (ErrorFound)
1307 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001308 return Res;
1309}
1310
1311StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1312 Stmt *AStmt,
1313 SourceLocation StartLoc,
1314 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001315 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1316 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1317 // 1.2.2 OpenMP Language Terminology
1318 // Structured block - An executable statement with a single entry at the
1319 // top and a single exit at the bottom.
1320 // The point of exit cannot be a branch out of the structured block.
1321 // longjmp() and throw() must not violate the entry/exit criteria.
1322 CS->getCapturedDecl()->setNothrow();
1323
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001324 getCurFunction()->setHasBranchProtectedScope();
1325
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001326 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1327 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001328}
1329
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001330namespace {
1331/// \brief Helper class for checking canonical form of the OpenMP loops and
1332/// extracting iteration space of each loop in the loop nest, that will be used
1333/// for IR generation.
1334class OpenMPIterationSpaceChecker {
1335 /// \brief Reference to Sema.
1336 Sema &SemaRef;
1337 /// \brief A location for diagnostics (when there is no some better location).
1338 SourceLocation DefaultLoc;
1339 /// \brief A location for diagnostics (when increment is not compatible).
1340 SourceLocation ConditionLoc;
1341 /// \brief A source location for referring to condition later.
1342 SourceRange ConditionSrcRange;
1343 /// \brief Loop variable.
1344 VarDecl *Var;
1345 /// \brief Lower bound (initializer for the var).
1346 Expr *LB;
1347 /// \brief Upper bound.
1348 Expr *UB;
1349 /// \brief Loop step (increment).
1350 Expr *Step;
1351 /// \brief This flag is true when condition is one of:
1352 /// Var < UB
1353 /// Var <= UB
1354 /// UB > Var
1355 /// UB >= Var
1356 bool TestIsLessOp;
1357 /// \brief This flag is true when condition is strict ( < or > ).
1358 bool TestIsStrictOp;
1359 /// \brief This flag is true when step is subtracted on each iteration.
1360 bool SubtractStep;
1361
1362public:
1363 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1364 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1365 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1366 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1367 SubtractStep(false) {}
1368 /// \brief Check init-expr for canonical loop form and save loop counter
1369 /// variable - #Var and its initialization value - #LB.
1370 bool CheckInit(Stmt *S);
1371 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1372 /// for less/greater and for strict/non-strict comparison.
1373 bool CheckCond(Expr *S);
1374 /// \brief Check incr-expr for canonical loop form and return true if it
1375 /// does not conform, otherwise save loop step (#Step).
1376 bool CheckInc(Expr *S);
1377 /// \brief Return the loop counter variable.
1378 VarDecl *GetLoopVar() const { return Var; }
1379 /// \brief Return true if any expression is dependent.
1380 bool Dependent() const;
1381
1382private:
1383 /// \brief Check the right-hand side of an assignment in the increment
1384 /// expression.
1385 bool CheckIncRHS(Expr *RHS);
1386 /// \brief Helper to set loop counter variable and its initializer.
1387 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1388 /// \brief Helper to set upper bound.
1389 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1390 const SourceLocation &SL);
1391 /// \brief Helper to set loop increment.
1392 bool SetStep(Expr *NewStep, bool Subtract);
1393};
1394
1395bool OpenMPIterationSpaceChecker::Dependent() const {
1396 if (!Var) {
1397 assert(!LB && !UB && !Step);
1398 return false;
1399 }
1400 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1401 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1402}
1403
1404bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1405 // State consistency checking to ensure correct usage.
1406 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1407 !TestIsLessOp && !TestIsStrictOp);
1408 if (!NewVar || !NewLB)
1409 return true;
1410 Var = NewVar;
1411 LB = NewLB;
1412 return false;
1413}
1414
1415bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1416 const SourceRange &SR,
1417 const SourceLocation &SL) {
1418 // State consistency checking to ensure correct usage.
1419 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1420 !TestIsLessOp && !TestIsStrictOp);
1421 if (!NewUB)
1422 return true;
1423 UB = NewUB;
1424 TestIsLessOp = LessOp;
1425 TestIsStrictOp = StrictOp;
1426 ConditionSrcRange = SR;
1427 ConditionLoc = SL;
1428 return false;
1429}
1430
1431bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1432 // State consistency checking to ensure correct usage.
1433 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1434 if (!NewStep)
1435 return true;
1436 if (!NewStep->isValueDependent()) {
1437 // Check that the step is integer expression.
1438 SourceLocation StepLoc = NewStep->getLocStart();
1439 ExprResult Val =
1440 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1441 if (Val.isInvalid())
1442 return true;
1443 NewStep = Val.get();
1444
1445 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1446 // If test-expr is of form var relational-op b and relational-op is < or
1447 // <= then incr-expr must cause var to increase on each iteration of the
1448 // loop. If test-expr is of form var relational-op b and relational-op is
1449 // > or >= then incr-expr must cause var to decrease on each iteration of
1450 // the loop.
1451 // If test-expr is of form b relational-op var and relational-op is < or
1452 // <= then incr-expr must cause var to decrease on each iteration of the
1453 // loop. If test-expr is of form b relational-op var and relational-op is
1454 // > or >= then incr-expr must cause var to increase on each iteration of
1455 // the loop.
1456 llvm::APSInt Result;
1457 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1458 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1459 bool IsConstNeg =
1460 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1461 bool IsConstZero = IsConstant && !Result.getBoolValue();
1462 if (UB && (IsConstZero ||
1463 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1464 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1465 SemaRef.Diag(NewStep->getExprLoc(),
1466 diag::err_omp_loop_incr_not_compatible)
1467 << Var << TestIsLessOp << NewStep->getSourceRange();
1468 SemaRef.Diag(ConditionLoc,
1469 diag::note_omp_loop_cond_requres_compatible_incr)
1470 << TestIsLessOp << ConditionSrcRange;
1471 return true;
1472 }
1473 }
1474
1475 Step = NewStep;
1476 SubtractStep = Subtract;
1477 return false;
1478}
1479
1480bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1481 // Check init-expr for canonical loop form and save loop counter
1482 // variable - #Var and its initialization value - #LB.
1483 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1484 // var = lb
1485 // integer-type var = lb
1486 // random-access-iterator-type var = lb
1487 // pointer-type var = lb
1488 //
1489 if (!S) {
1490 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1491 return true;
1492 }
1493 if (Expr *E = dyn_cast<Expr>(S))
1494 S = E->IgnoreParens();
1495 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1496 if (BO->getOpcode() == BO_Assign)
1497 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1498 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1499 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1500 if (DS->isSingleDecl()) {
1501 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1502 if (Var->hasInit()) {
1503 // Accept non-canonical init form here but emit ext. warning.
1504 if (Var->getInitStyle() != VarDecl::CInit)
1505 SemaRef.Diag(S->getLocStart(),
1506 diag::ext_omp_loop_not_canonical_init)
1507 << S->getSourceRange();
1508 return SetVarAndLB(Var, Var->getInit());
1509 }
1510 }
1511 }
1512 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1513 if (CE->getOperator() == OO_Equal)
1514 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1515 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1516
1517 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1518 << S->getSourceRange();
1519 return true;
1520}
1521
Alexey Bataev23b69422014-06-18 07:08:49 +00001522/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001523/// variable (which may be the loop variable) if possible.
1524static const VarDecl *GetInitVarDecl(const Expr *E) {
1525 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001526 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001527 E = E->IgnoreParenImpCasts();
1528 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1529 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1530 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1531 CE->getArg(0) != nullptr)
1532 E = CE->getArg(0)->IgnoreParenImpCasts();
1533 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1534 if (!DRE)
1535 return nullptr;
1536 return dyn_cast<VarDecl>(DRE->getDecl());
1537}
1538
1539bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1540 // Check test-expr for canonical form, save upper-bound UB, flags for
1541 // less/greater and for strict/non-strict comparison.
1542 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1543 // var relational-op b
1544 // b relational-op var
1545 //
1546 if (!S) {
1547 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1548 return true;
1549 }
1550 S = S->IgnoreParenImpCasts();
1551 SourceLocation CondLoc = S->getLocStart();
1552 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1553 if (BO->isRelationalOp()) {
1554 if (GetInitVarDecl(BO->getLHS()) == Var)
1555 return SetUB(BO->getRHS(),
1556 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1557 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1558 BO->getSourceRange(), BO->getOperatorLoc());
1559 if (GetInitVarDecl(BO->getRHS()) == Var)
1560 return SetUB(BO->getLHS(),
1561 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1562 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1563 BO->getSourceRange(), BO->getOperatorLoc());
1564 }
1565 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1566 if (CE->getNumArgs() == 2) {
1567 auto Op = CE->getOperator();
1568 switch (Op) {
1569 case OO_Greater:
1570 case OO_GreaterEqual:
1571 case OO_Less:
1572 case OO_LessEqual:
1573 if (GetInitVarDecl(CE->getArg(0)) == Var)
1574 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1575 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1576 CE->getOperatorLoc());
1577 if (GetInitVarDecl(CE->getArg(1)) == Var)
1578 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1579 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1580 CE->getOperatorLoc());
1581 break;
1582 default:
1583 break;
1584 }
1585 }
1586 }
1587 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1588 << S->getSourceRange() << Var;
1589 return true;
1590}
1591
1592bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1593 // RHS of canonical loop form increment can be:
1594 // var + incr
1595 // incr + var
1596 // var - incr
1597 //
1598 RHS = RHS->IgnoreParenImpCasts();
1599 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1600 if (BO->isAdditiveOp()) {
1601 bool IsAdd = BO->getOpcode() == BO_Add;
1602 if (GetInitVarDecl(BO->getLHS()) == Var)
1603 return SetStep(BO->getRHS(), !IsAdd);
1604 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1605 return SetStep(BO->getLHS(), false);
1606 }
1607 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1608 bool IsAdd = CE->getOperator() == OO_Plus;
1609 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1610 if (GetInitVarDecl(CE->getArg(0)) == Var)
1611 return SetStep(CE->getArg(1), !IsAdd);
1612 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1613 return SetStep(CE->getArg(0), false);
1614 }
1615 }
1616 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1617 << RHS->getSourceRange() << Var;
1618 return true;
1619}
1620
1621bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1622 // Check incr-expr for canonical loop form and return true if it
1623 // does not conform.
1624 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1625 // ++var
1626 // var++
1627 // --var
1628 // var--
1629 // var += incr
1630 // var -= incr
1631 // var = var + incr
1632 // var = incr + var
1633 // var = var - incr
1634 //
1635 if (!S) {
1636 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1637 return true;
1638 }
1639 S = S->IgnoreParens();
1640 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1641 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1642 return SetStep(
1643 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1644 (UO->isDecrementOp() ? -1 : 1)).get(),
1645 false);
1646 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1647 switch (BO->getOpcode()) {
1648 case BO_AddAssign:
1649 case BO_SubAssign:
1650 if (GetInitVarDecl(BO->getLHS()) == Var)
1651 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1652 break;
1653 case BO_Assign:
1654 if (GetInitVarDecl(BO->getLHS()) == Var)
1655 return CheckIncRHS(BO->getRHS());
1656 break;
1657 default:
1658 break;
1659 }
1660 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1661 switch (CE->getOperator()) {
1662 case OO_PlusPlus:
1663 case OO_MinusMinus:
1664 if (GetInitVarDecl(CE->getArg(0)) == Var)
1665 return SetStep(
1666 SemaRef.ActOnIntegerConstant(
1667 CE->getLocStart(),
1668 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1669 false);
1670 break;
1671 case OO_PlusEqual:
1672 case OO_MinusEqual:
1673 if (GetInitVarDecl(CE->getArg(0)) == Var)
1674 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1675 break;
1676 case OO_Equal:
1677 if (GetInitVarDecl(CE->getArg(0)) == Var)
1678 return CheckIncRHS(CE->getArg(1));
1679 break;
1680 default:
1681 break;
1682 }
1683 }
1684 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1685 << S->getSourceRange() << Var;
1686 return true;
1687}
Alexey Bataev23b69422014-06-18 07:08:49 +00001688} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001689
1690/// \brief Called on a for stmt to check and extract its iteration space
1691/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00001692static bool CheckOpenMPIterationSpace(
1693 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
1694 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
1695 Expr *NestedLoopCountExpr,
1696 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001697 // OpenMP [2.6, Canonical Loop Form]
1698 // for (init-expr; test-expr; incr-expr) structured-block
1699 auto For = dyn_cast_or_null<ForStmt>(S);
1700 if (!For) {
1701 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001702 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1703 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1704 << CurrentNestedLoopCount;
1705 if (NestedLoopCount > 1)
1706 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1707 diag::note_omp_collapse_expr)
1708 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001709 return true;
1710 }
1711 assert(For->getBody());
1712
1713 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1714
1715 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001716 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001717 if (ISC.CheckInit(Init)) {
1718 return true;
1719 }
1720
1721 bool HasErrors = false;
1722
1723 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001724 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001725
1726 // OpenMP [2.6, Canonical Loop Form]
1727 // Var is one of the following:
1728 // A variable of signed or unsigned integer type.
1729 // For C++, a variable of a random access iterator type.
1730 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001731 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001732 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1733 !VarType->isPointerType() &&
1734 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1735 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1736 << SemaRef.getLangOpts().CPlusPlus;
1737 HasErrors = true;
1738 }
1739
Alexey Bataev4acb8592014-07-07 13:01:15 +00001740 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
1741 // Construct
1742 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1743 // parallel for construct is (are) private.
1744 // The loop iteration variable in the associated for-loop of a simd construct
1745 // with just one associated for-loop is linear with a constant-linear-step
1746 // that is the increment of the associated for-loop.
1747 // Exclude loop var from the list of variables with implicitly defined data
1748 // sharing attributes.
1749 while (VarsWithImplicitDSA.count(Var) > 0)
1750 VarsWithImplicitDSA.erase(Var);
1751
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001752 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1753 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001754 // The loop iteration variable in the associated for-loop of a simd construct
1755 // with just one associated for-loop may be listed in a linear clause with a
1756 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001757 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1758 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001759 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001760 auto PredeterminedCKind =
1761 isOpenMPSimdDirective(DKind)
1762 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
1763 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001764 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001765 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00001766 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1767 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001768 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001769 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00001770 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
1771 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001772 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001773 HasErrors = true;
1774 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001775 // Make the loop iteration variable private (for worksharing constructs),
1776 // linear (for simd directives with the only one associated loop) or
1777 // lastprivate (for simd directives with several collapsed loops).
1778 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001779 }
1780
Alexey Bataev7ff55242014-06-19 09:13:45 +00001781 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001782
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001783 // Check test-expr.
1784 HasErrors |= ISC.CheckCond(For->getCond());
1785
1786 // Check incr-expr.
1787 HasErrors |= ISC.CheckInc(For->getInc());
1788
1789 if (ISC.Dependent())
1790 return HasErrors;
1791
1792 // FIXME: Build loop's iteration space representation.
1793 return HasErrors;
1794}
1795
1796/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1797/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1798/// to get the first for loop.
1799static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1800 if (IgnoreCaptured)
1801 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1802 S = CapS->getCapturedStmt();
1803 // OpenMP [2.8.1, simd construct, Restrictions]
1804 // All loops associated with the construct must be perfectly nested; that is,
1805 // there must be no intervening code nor any OpenMP directive between any two
1806 // loops.
1807 while (true) {
1808 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1809 S = AS->getSubStmt();
1810 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1811 if (CS->size() != 1)
1812 break;
1813 S = CS->body_back();
1814 } else
1815 break;
1816 }
1817 return S;
1818}
1819
1820/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001821/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1822/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001823static unsigned
1824CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
1825 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
1826 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001827 unsigned NestedLoopCount = 1;
1828 if (NestedLoopCountExpr) {
1829 // Found 'collapse' clause - calculate collapse number.
1830 llvm::APSInt Result;
1831 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1832 NestedLoopCount = Result.getLimitedValue();
1833 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001834 // This is helper routine for loop directives (e.g., 'for', 'simd',
1835 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001836 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1837 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001838 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00001839 NestedLoopCount, NestedLoopCountExpr,
1840 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001841 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001842 // Move on to the next nested for loop, or to the loop body.
1843 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1844 }
1845
1846 // FIXME: Build resulting iteration space for IR generation (collapsing
1847 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001848 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001849}
1850
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001851static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001852 auto CollapseFilter = [](const OMPClause *C) -> bool {
1853 return C->getClauseKind() == OMPC_collapse;
1854 };
1855 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1856 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001857 if (I)
1858 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1859 return nullptr;
1860}
1861
Alexey Bataev4acb8592014-07-07 13:01:15 +00001862StmtResult Sema::ActOnOpenMPSimdDirective(
1863 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1864 SourceLocation EndLoc,
1865 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001866 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001867 unsigned NestedLoopCount =
1868 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
1869 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001870 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001871 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001872
1873 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001874 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1875 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001876}
1877
Alexey Bataev4acb8592014-07-07 13:01:15 +00001878StmtResult Sema::ActOnOpenMPForDirective(
1879 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1880 SourceLocation EndLoc,
1881 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001882 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001883 unsigned NestedLoopCount =
1884 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
1885 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001886 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001887 return StmtError();
1888
1889 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001890 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1891 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001892}
1893
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001894StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1895 Stmt *AStmt,
1896 SourceLocation StartLoc,
1897 SourceLocation EndLoc) {
1898 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1899 auto BaseStmt = AStmt;
1900 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1901 BaseStmt = CS->getCapturedStmt();
1902 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1903 auto S = C->children();
1904 if (!S)
1905 return StmtError();
1906 // All associated statements must be '#pragma omp section' except for
1907 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001908 for (++S; S; ++S) {
1909 auto SectionStmt = *S;
1910 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1911 if (SectionStmt)
1912 Diag(SectionStmt->getLocStart(),
1913 diag::err_omp_sections_substmt_not_section);
1914 return StmtError();
1915 }
1916 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001917 } else {
1918 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1919 return StmtError();
1920 }
1921
1922 getCurFunction()->setHasBranchProtectedScope();
1923
1924 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1925 AStmt);
1926}
1927
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001928StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1929 SourceLocation StartLoc,
1930 SourceLocation EndLoc) {
1931 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1932
1933 getCurFunction()->setHasBranchProtectedScope();
1934
1935 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1936}
1937
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001938StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
1939 Stmt *AStmt,
1940 SourceLocation StartLoc,
1941 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00001942 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1943
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001944 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00001945
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001946 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1947}
1948
Alexander Musman80c22892014-07-17 08:54:58 +00001949StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
1950 SourceLocation StartLoc,
1951 SourceLocation EndLoc) {
1952 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1953
1954 getCurFunction()->setHasBranchProtectedScope();
1955
1956 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
1957}
1958
Alexey Bataev4acb8592014-07-07 13:01:15 +00001959StmtResult Sema::ActOnOpenMPParallelForDirective(
1960 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1961 SourceLocation EndLoc,
1962 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
1963 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1964 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1965 // 1.2.2 OpenMP Language Terminology
1966 // Structured block - An executable statement with a single entry at the
1967 // top and a single exit at the bottom.
1968 // The point of exit cannot be a branch out of the structured block.
1969 // longjmp() and throw() must not violate the entry/exit criteria.
1970 CS->getCapturedDecl()->setNothrow();
1971
1972 // In presence of clause 'collapse', it will define the nested loops number.
1973 unsigned NestedLoopCount =
1974 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
1975 *this, *DSAStack, VarsWithImplicitDSA);
1976 if (NestedLoopCount == 0)
1977 return StmtError();
1978
1979 getCurFunction()->setHasBranchProtectedScope();
1980 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
1981 NestedLoopCount, Clauses, AStmt);
1982}
1983
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001984StmtResult
1985Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
1986 Stmt *AStmt, SourceLocation StartLoc,
1987 SourceLocation EndLoc) {
1988 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1989 auto BaseStmt = AStmt;
1990 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1991 BaseStmt = CS->getCapturedStmt();
1992 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1993 auto S = C->children();
1994 if (!S)
1995 return StmtError();
1996 // All associated statements must be '#pragma omp section' except for
1997 // the first one.
1998 for (++S; S; ++S) {
1999 auto SectionStmt = *S;
2000 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2001 if (SectionStmt)
2002 Diag(SectionStmt->getLocStart(),
2003 diag::err_omp_parallel_sections_substmt_not_section);
2004 return StmtError();
2005 }
2006 }
2007 } else {
2008 Diag(AStmt->getLocStart(),
2009 diag::err_omp_parallel_sections_not_compound_stmt);
2010 return StmtError();
2011 }
2012
2013 getCurFunction()->setHasBranchProtectedScope();
2014
2015 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2016 Clauses, AStmt);
2017}
2018
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002019StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2020 Stmt *AStmt, SourceLocation StartLoc,
2021 SourceLocation EndLoc) {
2022 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2023 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2024 // 1.2.2 OpenMP Language Terminology
2025 // Structured block - An executable statement with a single entry at the
2026 // top and a single exit at the bottom.
2027 // The point of exit cannot be a branch out of the structured block.
2028 // longjmp() and throw() must not violate the entry/exit criteria.
2029 CS->getCapturedDecl()->setNothrow();
2030
2031 getCurFunction()->setHasBranchProtectedScope();
2032
2033 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2034}
2035
Alexey Bataeved09d242014-05-28 05:53:51 +00002036OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002037 SourceLocation StartLoc,
2038 SourceLocation LParenLoc,
2039 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002040 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002041 switch (Kind) {
2042 case OMPC_if:
2043 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2044 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002045 case OMPC_final:
2046 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2047 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002048 case OMPC_num_threads:
2049 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2050 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002051 case OMPC_safelen:
2052 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2053 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002054 case OMPC_collapse:
2055 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2056 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002057 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002058 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002059 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002060 case OMPC_private:
2061 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002062 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002063 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002064 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002065 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002066 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002067 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002068 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002069 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002070 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002071 case OMPC_untied:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002072 case OMPC_threadprivate:
2073 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002074 llvm_unreachable("Clause is not allowed.");
2075 }
2076 return Res;
2077}
2078
Alexey Bataeved09d242014-05-28 05:53:51 +00002079OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002080 SourceLocation LParenLoc,
2081 SourceLocation EndLoc) {
2082 Expr *ValExpr = Condition;
2083 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2084 !Condition->isInstantiationDependent() &&
2085 !Condition->containsUnexpandedParameterPack()) {
2086 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002087 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002088 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002089 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002090
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002091 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002092 }
2093
2094 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2095}
2096
Alexey Bataev3778b602014-07-17 07:32:53 +00002097OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2098 SourceLocation StartLoc,
2099 SourceLocation LParenLoc,
2100 SourceLocation EndLoc) {
2101 Expr *ValExpr = Condition;
2102 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2103 !Condition->isInstantiationDependent() &&
2104 !Condition->containsUnexpandedParameterPack()) {
2105 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2106 Condition->getExprLoc(), Condition);
2107 if (Val.isInvalid())
2108 return nullptr;
2109
2110 ValExpr = Val.get();
2111 }
2112
2113 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2114}
2115
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002116ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2117 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002118 if (!Op)
2119 return ExprError();
2120
2121 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2122 public:
2123 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002124 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002125 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2126 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002127 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2128 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002129 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2130 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002131 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2132 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002133 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2134 QualType T,
2135 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002136 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2137 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002138 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2139 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002140 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002141 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002142 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002143 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2144 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002145 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2146 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002147 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2148 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002149 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002150 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002151 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002152 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2153 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002154 llvm_unreachable("conversion functions are permitted");
2155 }
2156 } ConvertDiagnoser;
2157 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2158}
2159
2160OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2161 SourceLocation StartLoc,
2162 SourceLocation LParenLoc,
2163 SourceLocation EndLoc) {
2164 Expr *ValExpr = NumThreads;
2165 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2166 !NumThreads->isInstantiationDependent() &&
2167 !NumThreads->containsUnexpandedParameterPack()) {
2168 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2169 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002170 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002171 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002172 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002173
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002174 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002175
2176 // OpenMP [2.5, Restrictions]
2177 // The num_threads expression must evaluate to a positive integer value.
2178 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002179 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2180 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002181 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2182 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002183 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002184 }
2185 }
2186
Alexey Bataeved09d242014-05-28 05:53:51 +00002187 return new (Context)
2188 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002189}
2190
Alexey Bataev62c87d22014-03-21 04:51:18 +00002191ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2192 OpenMPClauseKind CKind) {
2193 if (!E)
2194 return ExprError();
2195 if (E->isValueDependent() || E->isTypeDependent() ||
2196 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002197 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002198 llvm::APSInt Result;
2199 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2200 if (ICE.isInvalid())
2201 return ExprError();
2202 if (!Result.isStrictlyPositive()) {
2203 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2204 << getOpenMPClauseName(CKind) << E->getSourceRange();
2205 return ExprError();
2206 }
2207 return ICE;
2208}
2209
2210OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2211 SourceLocation LParenLoc,
2212 SourceLocation EndLoc) {
2213 // OpenMP [2.8.1, simd construct, Description]
2214 // The parameter of the safelen clause must be a constant
2215 // positive integer expression.
2216 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2217 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002218 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002219 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002220 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002221}
2222
Alexander Musman64d33f12014-06-04 07:53:32 +00002223OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2224 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002225 SourceLocation LParenLoc,
2226 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002227 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002228 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002229 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002230 // The parameter of the collapse clause must be a constant
2231 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002232 ExprResult NumForLoopsResult =
2233 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2234 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002235 return nullptr;
2236 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002237 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002238}
2239
Alexey Bataeved09d242014-05-28 05:53:51 +00002240OMPClause *Sema::ActOnOpenMPSimpleClause(
2241 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2242 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002243 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002244 switch (Kind) {
2245 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002246 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002247 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2248 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002249 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002250 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002251 Res = ActOnOpenMPProcBindClause(
2252 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2253 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002254 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002255 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002256 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002257 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002258 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002259 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002260 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002261 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002262 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002263 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002264 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002265 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002266 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002267 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002268 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002269 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002270 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002271 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002272 case OMPC_untied:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002273 case OMPC_threadprivate:
2274 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002275 llvm_unreachable("Clause is not allowed.");
2276 }
2277 return Res;
2278}
2279
2280OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2281 SourceLocation KindKwLoc,
2282 SourceLocation StartLoc,
2283 SourceLocation LParenLoc,
2284 SourceLocation EndLoc) {
2285 if (Kind == OMPC_DEFAULT_unknown) {
2286 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002287 static_assert(OMPC_DEFAULT_unknown > 0,
2288 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002289 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002290 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002291 Values += "'";
2292 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2293 Values += "'";
2294 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002295 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002296 Values += " or ";
2297 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002298 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002299 break;
2300 default:
2301 Values += Sep;
2302 break;
2303 }
2304 }
2305 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002306 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002307 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002308 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002309 switch (Kind) {
2310 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002311 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002312 break;
2313 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002314 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002315 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002316 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002317 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002318 break;
2319 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002320 return new (Context)
2321 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002322}
2323
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002324OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2325 SourceLocation KindKwLoc,
2326 SourceLocation StartLoc,
2327 SourceLocation LParenLoc,
2328 SourceLocation EndLoc) {
2329 if (Kind == OMPC_PROC_BIND_unknown) {
2330 std::string Values;
2331 std::string Sep(", ");
2332 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2333 Values += "'";
2334 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2335 Values += "'";
2336 switch (i) {
2337 case OMPC_PROC_BIND_unknown - 2:
2338 Values += " or ";
2339 break;
2340 case OMPC_PROC_BIND_unknown - 1:
2341 break;
2342 default:
2343 Values += Sep;
2344 break;
2345 }
2346 }
2347 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002348 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002349 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002350 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002351 return new (Context)
2352 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002353}
2354
Alexey Bataev56dafe82014-06-20 07:16:17 +00002355OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2356 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2357 SourceLocation StartLoc, SourceLocation LParenLoc,
2358 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2359 SourceLocation EndLoc) {
2360 OMPClause *Res = nullptr;
2361 switch (Kind) {
2362 case OMPC_schedule:
2363 Res = ActOnOpenMPScheduleClause(
2364 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2365 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2366 break;
2367 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002368 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002369 case OMPC_num_threads:
2370 case OMPC_safelen:
2371 case OMPC_collapse:
2372 case OMPC_default:
2373 case OMPC_proc_bind:
2374 case OMPC_private:
2375 case OMPC_firstprivate:
2376 case OMPC_lastprivate:
2377 case OMPC_shared:
2378 case OMPC_reduction:
2379 case OMPC_linear:
2380 case OMPC_aligned:
2381 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002382 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002383 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002384 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002385 case OMPC_untied:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002386 case OMPC_threadprivate:
2387 case OMPC_unknown:
2388 llvm_unreachable("Clause is not allowed.");
2389 }
2390 return Res;
2391}
2392
2393OMPClause *Sema::ActOnOpenMPScheduleClause(
2394 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2395 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2396 SourceLocation EndLoc) {
2397 if (Kind == OMPC_SCHEDULE_unknown) {
2398 std::string Values;
2399 std::string Sep(", ");
2400 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2401 Values += "'";
2402 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2403 Values += "'";
2404 switch (i) {
2405 case OMPC_SCHEDULE_unknown - 2:
2406 Values += " or ";
2407 break;
2408 case OMPC_SCHEDULE_unknown - 1:
2409 break;
2410 default:
2411 Values += Sep;
2412 break;
2413 }
2414 }
2415 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2416 << Values << getOpenMPClauseName(OMPC_schedule);
2417 return nullptr;
2418 }
2419 Expr *ValExpr = ChunkSize;
2420 if (ChunkSize) {
2421 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2422 !ChunkSize->isInstantiationDependent() &&
2423 !ChunkSize->containsUnexpandedParameterPack()) {
2424 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2425 ExprResult Val =
2426 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2427 if (Val.isInvalid())
2428 return nullptr;
2429
2430 ValExpr = Val.get();
2431
2432 // OpenMP [2.7.1, Restrictions]
2433 // chunk_size must be a loop invariant integer expression with a positive
2434 // value.
2435 llvm::APSInt Result;
2436 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2437 Result.isSigned() && !Result.isStrictlyPositive()) {
2438 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2439 << "schedule" << ChunkSize->getSourceRange();
2440 return nullptr;
2441 }
2442 }
2443 }
2444
2445 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2446 EndLoc, Kind, ValExpr);
2447}
2448
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002449OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2450 SourceLocation StartLoc,
2451 SourceLocation EndLoc) {
2452 OMPClause *Res = nullptr;
2453 switch (Kind) {
2454 case OMPC_ordered:
2455 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2456 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002457 case OMPC_nowait:
2458 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2459 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002460 case OMPC_untied:
2461 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
2462 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002463 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002464 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002465 case OMPC_num_threads:
2466 case OMPC_safelen:
2467 case OMPC_collapse:
2468 case OMPC_schedule:
2469 case OMPC_private:
2470 case OMPC_firstprivate:
2471 case OMPC_lastprivate:
2472 case OMPC_shared:
2473 case OMPC_reduction:
2474 case OMPC_linear:
2475 case OMPC_aligned:
2476 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002477 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002478 case OMPC_default:
2479 case OMPC_proc_bind:
2480 case OMPC_threadprivate:
2481 case OMPC_unknown:
2482 llvm_unreachable("Clause is not allowed.");
2483 }
2484 return Res;
2485}
2486
2487OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2488 SourceLocation EndLoc) {
2489 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2490}
2491
Alexey Bataev236070f2014-06-20 11:19:47 +00002492OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2493 SourceLocation EndLoc) {
2494 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2495}
2496
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002497OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
2498 SourceLocation EndLoc) {
2499 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
2500}
2501
Alexey Bataevc5e02582014-06-16 07:08:35 +00002502OMPClause *Sema::ActOnOpenMPVarListClause(
2503 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2504 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2505 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2506 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002507 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002508 switch (Kind) {
2509 case OMPC_private:
2510 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2511 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002512 case OMPC_firstprivate:
2513 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2514 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002515 case OMPC_lastprivate:
2516 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2517 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002518 case OMPC_shared:
2519 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2520 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002521 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002522 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2523 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002524 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002525 case OMPC_linear:
2526 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2527 ColonLoc, EndLoc);
2528 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002529 case OMPC_aligned:
2530 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2531 ColonLoc, EndLoc);
2532 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002533 case OMPC_copyin:
2534 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2535 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002536 case OMPC_copyprivate:
2537 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2538 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002539 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002540 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002541 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002542 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002543 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002544 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002545 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002546 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002547 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002548 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002549 case OMPC_untied:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002550 case OMPC_threadprivate:
2551 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002552 llvm_unreachable("Clause is not allowed.");
2553 }
2554 return Res;
2555}
2556
2557OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2558 SourceLocation StartLoc,
2559 SourceLocation LParenLoc,
2560 SourceLocation EndLoc) {
2561 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002562 for (auto &RefExpr : VarList) {
2563 assert(RefExpr && "NULL expr in OpenMP private clause.");
2564 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002565 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002566 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002567 continue;
2568 }
2569
Alexey Bataeved09d242014-05-28 05:53:51 +00002570 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002571 // OpenMP [2.1, C/C++]
2572 // A list item is a variable name.
2573 // OpenMP [2.9.3.3, Restrictions, p.1]
2574 // A variable that is part of another variable (as an array or
2575 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002576 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002577 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002578 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002579 continue;
2580 }
2581 Decl *D = DE->getDecl();
2582 VarDecl *VD = cast<VarDecl>(D);
2583
2584 QualType Type = VD->getType();
2585 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2586 // It will be analyzed later.
2587 Vars.push_back(DE);
2588 continue;
2589 }
2590
2591 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2592 // A variable that appears in a private clause must not have an incomplete
2593 // type or a reference type.
2594 if (RequireCompleteType(ELoc, Type,
2595 diag::err_omp_private_incomplete_type)) {
2596 continue;
2597 }
2598 if (Type->isReferenceType()) {
2599 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002600 << getOpenMPClauseName(OMPC_private) << Type;
2601 bool IsDecl =
2602 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2603 Diag(VD->getLocation(),
2604 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2605 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002606 continue;
2607 }
2608
2609 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2610 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002611 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002612 // class type.
2613 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002614 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2615 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002616 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002617 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2618 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2619 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002620 // FIXME This code must be replaced by actual constructing/destructing of
2621 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002622 if (RD) {
2623 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2624 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002625 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002626 if (!CD ||
2627 CheckConstructorAccess(ELoc, CD,
2628 InitializedEntity::InitializeTemporary(Type),
2629 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002630 CD->isDeleted()) {
2631 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002632 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002633 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2634 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002635 Diag(VD->getLocation(),
2636 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2637 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002638 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2639 continue;
2640 }
2641 MarkFunctionReferenced(ELoc, CD);
2642 DiagnoseUseOfDecl(CD, ELoc);
2643
2644 CXXDestructorDecl *DD = RD->getDestructor();
2645 if (DD) {
2646 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2647 DD->isDeleted()) {
2648 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002649 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002650 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2651 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002652 Diag(VD->getLocation(),
2653 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2654 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002655 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2656 continue;
2657 }
2658 MarkFunctionReferenced(ELoc, DD);
2659 DiagnoseUseOfDecl(DD, ELoc);
2660 }
2661 }
2662
Alexey Bataev758e55e2013-09-06 18:03:48 +00002663 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2664 // in a Construct]
2665 // Variables with the predetermined data-sharing attributes may not be
2666 // listed in data-sharing attributes clauses, except for the cases
2667 // listed below. For these exceptions only, listing a predetermined
2668 // variable in a data-sharing attribute clause is allowed and overrides
2669 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002670 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002671 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002672 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2673 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002674 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002675 continue;
2676 }
2677
2678 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002679 Vars.push_back(DE);
2680 }
2681
Alexey Bataeved09d242014-05-28 05:53:51 +00002682 if (Vars.empty())
2683 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002684
2685 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2686}
2687
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002688OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2689 SourceLocation StartLoc,
2690 SourceLocation LParenLoc,
2691 SourceLocation EndLoc) {
2692 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002693 bool IsImplicitClause =
2694 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
2695 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
2696
Alexey Bataeved09d242014-05-28 05:53:51 +00002697 for (auto &RefExpr : VarList) {
2698 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2699 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002700 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002701 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002702 continue;
2703 }
2704
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002705 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
2706 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002707 // OpenMP [2.1, C/C++]
2708 // A list item is a variable name.
2709 // OpenMP [2.9.3.3, Restrictions, p.1]
2710 // A variable that is part of another variable (as an array or
2711 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002712 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002713 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002714 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002715 continue;
2716 }
2717 Decl *D = DE->getDecl();
2718 VarDecl *VD = cast<VarDecl>(D);
2719
2720 QualType Type = VD->getType();
2721 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2722 // It will be analyzed later.
2723 Vars.push_back(DE);
2724 continue;
2725 }
2726
2727 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2728 // A variable that appears in a private clause must not have an incomplete
2729 // type or a reference type.
2730 if (RequireCompleteType(ELoc, Type,
2731 diag::err_omp_firstprivate_incomplete_type)) {
2732 continue;
2733 }
2734 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002735 if (IsImplicitClause) {
2736 Diag(ImplicitClauseLoc,
2737 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
2738 << Type;
2739 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2740 } else {
2741 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2742 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2743 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002744 bool IsDecl =
2745 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2746 Diag(VD->getLocation(),
2747 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2748 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002749 continue;
2750 }
2751
2752 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2753 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002754 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002755 // class type.
2756 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002757 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2758 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2759 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002760 // FIXME This code must be replaced by actual constructing/destructing of
2761 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002762 if (RD) {
2763 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2764 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002765 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002766 if (!CD ||
2767 CheckConstructorAccess(ELoc, CD,
2768 InitializedEntity::InitializeTemporary(Type),
2769 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002770 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002771 if (IsImplicitClause) {
2772 Diag(ImplicitClauseLoc,
2773 diag::err_omp_task_predetermined_firstprivate_required_method)
2774 << 0;
2775 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2776 } else {
2777 Diag(ELoc, diag::err_omp_required_method)
2778 << getOpenMPClauseName(OMPC_firstprivate) << 1;
2779 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002780 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2781 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002782 Diag(VD->getLocation(),
2783 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2784 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002785 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2786 continue;
2787 }
2788 MarkFunctionReferenced(ELoc, CD);
2789 DiagnoseUseOfDecl(CD, ELoc);
2790
2791 CXXDestructorDecl *DD = RD->getDestructor();
2792 if (DD) {
2793 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2794 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002795 if (IsImplicitClause) {
2796 Diag(ImplicitClauseLoc,
2797 diag::err_omp_task_predetermined_firstprivate_required_method)
2798 << 1;
2799 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2800 } else {
2801 Diag(ELoc, diag::err_omp_required_method)
2802 << getOpenMPClauseName(OMPC_firstprivate) << 4;
2803 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002804 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2805 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002806 Diag(VD->getLocation(),
2807 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2808 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002809 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2810 continue;
2811 }
2812 MarkFunctionReferenced(ELoc, DD);
2813 DiagnoseUseOfDecl(DD, ELoc);
2814 }
2815 }
2816
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002817 // If an implicit firstprivate variable found it was checked already.
2818 if (!IsImplicitClause) {
2819 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002820 Type = Type.getNonReferenceType().getCanonicalType();
2821 bool IsConstant = Type.isConstant(Context);
2822 Type = Context.getBaseElementType(Type);
2823 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2824 // A list item that specifies a given variable may not appear in more
2825 // than one clause on the same directive, except that a variable may be
2826 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002827 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002828 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002829 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002830 << getOpenMPClauseName(DVar.CKind)
2831 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002832 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002833 continue;
2834 }
2835
2836 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2837 // in a Construct]
2838 // Variables with the predetermined data-sharing attributes may not be
2839 // listed in data-sharing attributes clauses, except for the cases
2840 // listed below. For these exceptions only, listing a predetermined
2841 // variable in a data-sharing attribute clause is allowed and overrides
2842 // the variable's predetermined data-sharing attributes.
2843 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2844 // in a Construct, C/C++, p.2]
2845 // Variables with const-qualified type having no mutable member may be
2846 // listed in a firstprivate clause, even if they are static data members.
2847 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2848 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2849 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002850 << getOpenMPClauseName(DVar.CKind)
2851 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002852 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002853 continue;
2854 }
2855
Alexey Bataevf29276e2014-06-18 04:14:57 +00002856 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002857 // OpenMP [2.9.3.4, Restrictions, p.2]
2858 // A list item that is private within a parallel region must not appear
2859 // in a firstprivate clause on a worksharing construct if any of the
2860 // worksharing regions arising from the worksharing construct ever bind
2861 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002862 if (isOpenMPWorksharingDirective(CurrDir) &&
2863 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002864 DVar = DSAStack->getImplicitDSA(VD, true);
2865 if (DVar.CKind != OMPC_shared &&
2866 (isOpenMPParallelDirective(DVar.DKind) ||
2867 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002868 Diag(ELoc, diag::err_omp_required_access)
2869 << getOpenMPClauseName(OMPC_firstprivate)
2870 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002871 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002872 continue;
2873 }
2874 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002875 // OpenMP [2.9.3.4, Restrictions, p.3]
2876 // A list item that appears in a reduction clause of a parallel construct
2877 // must not appear in a firstprivate clause on a worksharing or task
2878 // construct if any of the worksharing or task regions arising from the
2879 // worksharing or task construct ever bind to any of the parallel regions
2880 // arising from the parallel construct.
2881 // OpenMP [2.9.3.4, Restrictions, p.4]
2882 // A list item that appears in a reduction clause in worksharing
2883 // construct must not appear in a firstprivate clause in a task construct
2884 // encountered during execution of any of the worksharing regions arising
2885 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002886 if (CurrDir == OMPD_task) {
2887 DVar =
2888 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
2889 [](OpenMPDirectiveKind K) -> bool {
2890 return isOpenMPParallelDirective(K) ||
2891 isOpenMPWorksharingDirective(K);
2892 },
2893 false);
2894 if (DVar.CKind == OMPC_reduction &&
2895 (isOpenMPParallelDirective(DVar.DKind) ||
2896 isOpenMPWorksharingDirective(DVar.DKind))) {
2897 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
2898 << getOpenMPDirectiveName(DVar.DKind);
2899 ReportOriginalDSA(*this, DSAStack, VD, DVar);
2900 continue;
2901 }
2902 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002903 }
2904
2905 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2906 Vars.push_back(DE);
2907 }
2908
Alexey Bataeved09d242014-05-28 05:53:51 +00002909 if (Vars.empty())
2910 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002911
2912 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2913 Vars);
2914}
2915
Alexander Musman1bb328c2014-06-04 13:06:39 +00002916OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2917 SourceLocation StartLoc,
2918 SourceLocation LParenLoc,
2919 SourceLocation EndLoc) {
2920 SmallVector<Expr *, 8> Vars;
2921 for (auto &RefExpr : VarList) {
2922 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2923 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2924 // It will be analyzed later.
2925 Vars.push_back(RefExpr);
2926 continue;
2927 }
2928
2929 SourceLocation ELoc = RefExpr->getExprLoc();
2930 // OpenMP [2.1, C/C++]
2931 // A list item is a variable name.
2932 // OpenMP [2.14.3.5, Restrictions, p.1]
2933 // A variable that is part of another variable (as an array or structure
2934 // element) cannot appear in a lastprivate clause.
2935 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2936 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2937 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2938 continue;
2939 }
2940 Decl *D = DE->getDecl();
2941 VarDecl *VD = cast<VarDecl>(D);
2942
2943 QualType Type = VD->getType();
2944 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2945 // It will be analyzed later.
2946 Vars.push_back(DE);
2947 continue;
2948 }
2949
2950 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2951 // A variable that appears in a lastprivate clause must not have an
2952 // incomplete type or a reference type.
2953 if (RequireCompleteType(ELoc, Type,
2954 diag::err_omp_lastprivate_incomplete_type)) {
2955 continue;
2956 }
2957 if (Type->isReferenceType()) {
2958 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2959 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2960 bool IsDecl =
2961 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2962 Diag(VD->getLocation(),
2963 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2964 << VD;
2965 continue;
2966 }
2967
2968 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2969 // in a Construct]
2970 // Variables with the predetermined data-sharing attributes may not be
2971 // listed in data-sharing attributes clauses, except for the cases
2972 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002973 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002974 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2975 DVar.CKind != OMPC_firstprivate &&
2976 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2977 Diag(ELoc, diag::err_omp_wrong_dsa)
2978 << getOpenMPClauseName(DVar.CKind)
2979 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002980 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002981 continue;
2982 }
2983
Alexey Bataevf29276e2014-06-18 04:14:57 +00002984 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2985 // OpenMP [2.14.3.5, Restrictions, p.2]
2986 // A list item that is private within a parallel region, or that appears in
2987 // the reduction clause of a parallel construct, must not appear in a
2988 // lastprivate clause on a worksharing construct if any of the corresponding
2989 // worksharing regions ever binds to any of the corresponding parallel
2990 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002991 if (isOpenMPWorksharingDirective(CurrDir) &&
2992 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002993 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002994 if (DVar.CKind != OMPC_shared) {
2995 Diag(ELoc, diag::err_omp_required_access)
2996 << getOpenMPClauseName(OMPC_lastprivate)
2997 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002998 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002999 continue;
3000 }
3001 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003002 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003003 // A variable of class type (or array thereof) that appears in a
3004 // lastprivate clause requires an accessible, unambiguous default
3005 // constructor for the class type, unless the list item is also specified
3006 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003007 // A variable of class type (or array thereof) that appears in a
3008 // lastprivate clause requires an accessible, unambiguous copy assignment
3009 // operator for the class type.
3010 while (Type.getNonReferenceType()->isArrayType())
3011 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3012 ->getElementType();
3013 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3014 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3015 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003016 // FIXME This code must be replaced by actual copying and destructing of the
3017 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003018 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003019 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3020 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003021 if (MD) {
3022 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3023 MD->isDeleted()) {
3024 Diag(ELoc, diag::err_omp_required_method)
3025 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3026 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3027 VarDecl::DeclarationOnly;
3028 Diag(VD->getLocation(),
3029 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3030 << VD;
3031 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3032 continue;
3033 }
3034 MarkFunctionReferenced(ELoc, MD);
3035 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003036 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003037
3038 CXXDestructorDecl *DD = RD->getDestructor();
3039 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003040 PartialDiagnostic PD =
3041 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003042 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3043 DD->isDeleted()) {
3044 Diag(ELoc, diag::err_omp_required_method)
3045 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3046 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3047 VarDecl::DeclarationOnly;
3048 Diag(VD->getLocation(),
3049 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3050 << VD;
3051 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3052 continue;
3053 }
3054 MarkFunctionReferenced(ELoc, DD);
3055 DiagnoseUseOfDecl(DD, ELoc);
3056 }
3057 }
3058
Alexey Bataevf29276e2014-06-18 04:14:57 +00003059 if (DVar.CKind != OMPC_firstprivate)
3060 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003061 Vars.push_back(DE);
3062 }
3063
3064 if (Vars.empty())
3065 return nullptr;
3066
3067 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3068 Vars);
3069}
3070
Alexey Bataev758e55e2013-09-06 18:03:48 +00003071OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3072 SourceLocation StartLoc,
3073 SourceLocation LParenLoc,
3074 SourceLocation EndLoc) {
3075 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003076 for (auto &RefExpr : VarList) {
3077 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3078 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003079 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003080 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003081 continue;
3082 }
3083
Alexey Bataeved09d242014-05-28 05:53:51 +00003084 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003085 // OpenMP [2.1, C/C++]
3086 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003087 // OpenMP [2.14.3.2, Restrictions, p.1]
3088 // A variable that is part of another variable (as an array or structure
3089 // element) cannot appear in a shared unless it is a static data member
3090 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003091 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003092 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003093 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003094 continue;
3095 }
3096 Decl *D = DE->getDecl();
3097 VarDecl *VD = cast<VarDecl>(D);
3098
3099 QualType Type = VD->getType();
3100 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3101 // It will be analyzed later.
3102 Vars.push_back(DE);
3103 continue;
3104 }
3105
3106 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3107 // in a Construct]
3108 // Variables with the predetermined data-sharing attributes may not be
3109 // listed in data-sharing attributes clauses, except for the cases
3110 // listed below. For these exceptions only, listing a predetermined
3111 // variable in a data-sharing attribute clause is allowed and overrides
3112 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003113 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003114 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3115 DVar.RefExpr) {
3116 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3117 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003118 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003119 continue;
3120 }
3121
3122 DSAStack->addDSA(VD, DE, OMPC_shared);
3123 Vars.push_back(DE);
3124 }
3125
Alexey Bataeved09d242014-05-28 05:53:51 +00003126 if (Vars.empty())
3127 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003128
3129 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3130}
3131
Alexey Bataevc5e02582014-06-16 07:08:35 +00003132namespace {
3133class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3134 DSAStackTy *Stack;
3135
3136public:
3137 bool VisitDeclRefExpr(DeclRefExpr *E) {
3138 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003139 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003140 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3141 return false;
3142 if (DVar.CKind != OMPC_unknown)
3143 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003144 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003145 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003146 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003147 return true;
3148 return false;
3149 }
3150 return false;
3151 }
3152 bool VisitStmt(Stmt *S) {
3153 for (auto Child : S->children()) {
3154 if (Child && Visit(Child))
3155 return true;
3156 }
3157 return false;
3158 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003159 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003160};
Alexey Bataev23b69422014-06-18 07:08:49 +00003161} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003162
3163OMPClause *Sema::ActOnOpenMPReductionClause(
3164 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3165 SourceLocation ColonLoc, SourceLocation EndLoc,
3166 CXXScopeSpec &ReductionIdScopeSpec,
3167 const DeclarationNameInfo &ReductionId) {
3168 // TODO: Allow scope specification search when 'declare reduction' is
3169 // supported.
3170 assert(ReductionIdScopeSpec.isEmpty() &&
3171 "No support for scoped reduction identifiers yet.");
3172
3173 auto DN = ReductionId.getName();
3174 auto OOK = DN.getCXXOverloadedOperator();
3175 BinaryOperatorKind BOK = BO_Comma;
3176
3177 // OpenMP [2.14.3.6, reduction clause]
3178 // C
3179 // reduction-identifier is either an identifier or one of the following
3180 // operators: +, -, *, &, |, ^, && and ||
3181 // C++
3182 // reduction-identifier is either an id-expression or one of the following
3183 // operators: +, -, *, &, |, ^, && and ||
3184 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3185 switch (OOK) {
3186 case OO_Plus:
3187 case OO_Minus:
3188 BOK = BO_AddAssign;
3189 break;
3190 case OO_Star:
3191 BOK = BO_MulAssign;
3192 break;
3193 case OO_Amp:
3194 BOK = BO_AndAssign;
3195 break;
3196 case OO_Pipe:
3197 BOK = BO_OrAssign;
3198 break;
3199 case OO_Caret:
3200 BOK = BO_XorAssign;
3201 break;
3202 case OO_AmpAmp:
3203 BOK = BO_LAnd;
3204 break;
3205 case OO_PipePipe:
3206 BOK = BO_LOr;
3207 break;
3208 default:
3209 if (auto II = DN.getAsIdentifierInfo()) {
3210 if (II->isStr("max"))
3211 BOK = BO_GT;
3212 else if (II->isStr("min"))
3213 BOK = BO_LT;
3214 }
3215 break;
3216 }
3217 SourceRange ReductionIdRange;
3218 if (ReductionIdScopeSpec.isValid()) {
3219 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3220 }
3221 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3222 if (BOK == BO_Comma) {
3223 // Not allowed reduction identifier is found.
3224 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3225 << ReductionIdRange;
3226 return nullptr;
3227 }
3228
3229 SmallVector<Expr *, 8> Vars;
3230 for (auto RefExpr : VarList) {
3231 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3232 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3233 // It will be analyzed later.
3234 Vars.push_back(RefExpr);
3235 continue;
3236 }
3237
3238 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3239 RefExpr->isInstantiationDependent() ||
3240 RefExpr->containsUnexpandedParameterPack()) {
3241 // It will be analyzed later.
3242 Vars.push_back(RefExpr);
3243 continue;
3244 }
3245
3246 auto ELoc = RefExpr->getExprLoc();
3247 auto ERange = RefExpr->getSourceRange();
3248 // OpenMP [2.1, C/C++]
3249 // A list item is a variable or array section, subject to the restrictions
3250 // specified in Section 2.4 on page 42 and in each of the sections
3251 // describing clauses and directives for which a list appears.
3252 // OpenMP [2.14.3.3, Restrictions, p.1]
3253 // A variable that is part of another variable (as an array or
3254 // structure element) cannot appear in a private clause.
3255 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3256 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3257 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3258 continue;
3259 }
3260 auto D = DE->getDecl();
3261 auto VD = cast<VarDecl>(D);
3262 auto Type = VD->getType();
3263 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3264 // A variable that appears in a private clause must not have an incomplete
3265 // type or a reference type.
3266 if (RequireCompleteType(ELoc, Type,
3267 diag::err_omp_reduction_incomplete_type))
3268 continue;
3269 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3270 // Arrays may not appear in a reduction clause.
3271 if (Type.getNonReferenceType()->isArrayType()) {
3272 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3273 bool IsDecl =
3274 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3275 Diag(VD->getLocation(),
3276 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3277 << VD;
3278 continue;
3279 }
3280 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3281 // A list item that appears in a reduction clause must not be
3282 // const-qualified.
3283 if (Type.getNonReferenceType().isConstant(Context)) {
3284 Diag(ELoc, diag::err_omp_const_variable)
3285 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3286 bool IsDecl =
3287 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3288 Diag(VD->getLocation(),
3289 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3290 << VD;
3291 continue;
3292 }
3293 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3294 // If a list-item is a reference type then it must bind to the same object
3295 // for all threads of the team.
3296 VarDecl *VDDef = VD->getDefinition();
3297 if (Type->isReferenceType() && VDDef) {
3298 DSARefChecker Check(DSAStack);
3299 if (Check.Visit(VDDef->getInit())) {
3300 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3301 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3302 continue;
3303 }
3304 }
3305 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3306 // The type of a list item that appears in a reduction clause must be valid
3307 // for the reduction-identifier. For a max or min reduction in C, the type
3308 // of the list item must be an allowed arithmetic data type: char, int,
3309 // float, double, or _Bool, possibly modified with long, short, signed, or
3310 // unsigned. For a max or min reduction in C++, the type of the list item
3311 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3312 // double, or bool, possibly modified with long, short, signed, or unsigned.
3313 if ((BOK == BO_GT || BOK == BO_LT) &&
3314 !(Type->isScalarType() ||
3315 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3316 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3317 << getLangOpts().CPlusPlus;
3318 bool IsDecl =
3319 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3320 Diag(VD->getLocation(),
3321 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3322 << VD;
3323 continue;
3324 }
3325 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3326 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3327 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3328 bool IsDecl =
3329 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3330 Diag(VD->getLocation(),
3331 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3332 << VD;
3333 continue;
3334 }
3335 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3336 getDiagnostics().setSuppressAllDiagnostics(true);
3337 ExprResult ReductionOp =
3338 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3339 RefExpr, RefExpr);
3340 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3341 if (ReductionOp.isInvalid()) {
3342 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003343 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003344 bool IsDecl =
3345 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3346 Diag(VD->getLocation(),
3347 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3348 << VD;
3349 continue;
3350 }
3351
3352 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3353 // in a Construct]
3354 // Variables with the predetermined data-sharing attributes may not be
3355 // listed in data-sharing attributes clauses, except for the cases
3356 // listed below. For these exceptions only, listing a predetermined
3357 // variable in a data-sharing attribute clause is allowed and overrides
3358 // the variable's predetermined data-sharing attributes.
3359 // OpenMP [2.14.3.6, Restrictions, p.3]
3360 // Any number of reduction clauses can be specified on the directive,
3361 // but a list item can appear only once in the reduction clauses for that
3362 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003363 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003364 if (DVar.CKind == OMPC_reduction) {
3365 Diag(ELoc, diag::err_omp_once_referenced)
3366 << getOpenMPClauseName(OMPC_reduction);
3367 if (DVar.RefExpr) {
3368 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3369 }
3370 } else if (DVar.CKind != OMPC_unknown) {
3371 Diag(ELoc, diag::err_omp_wrong_dsa)
3372 << getOpenMPClauseName(DVar.CKind)
3373 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003374 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003375 continue;
3376 }
3377
3378 // OpenMP [2.14.3.6, Restrictions, p.1]
3379 // A list item that appears in a reduction clause of a worksharing
3380 // construct must be shared in the parallel regions to which any of the
3381 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003382 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003383 if (isOpenMPWorksharingDirective(CurrDir) &&
3384 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003385 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003386 if (DVar.CKind != OMPC_shared) {
3387 Diag(ELoc, diag::err_omp_required_access)
3388 << getOpenMPClauseName(OMPC_reduction)
3389 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003390 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003391 continue;
3392 }
3393 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003394
3395 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3396 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3397 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003398 // FIXME This code must be replaced by actual constructing/destructing of
3399 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003400 if (RD) {
3401 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3402 PartialDiagnostic PD =
3403 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003404 if (!CD ||
3405 CheckConstructorAccess(ELoc, CD,
3406 InitializedEntity::InitializeTemporary(Type),
3407 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003408 CD->isDeleted()) {
3409 Diag(ELoc, diag::err_omp_required_method)
3410 << getOpenMPClauseName(OMPC_reduction) << 0;
3411 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3412 VarDecl::DeclarationOnly;
3413 Diag(VD->getLocation(),
3414 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3415 << VD;
3416 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3417 continue;
3418 }
3419 MarkFunctionReferenced(ELoc, CD);
3420 DiagnoseUseOfDecl(CD, ELoc);
3421
3422 CXXDestructorDecl *DD = RD->getDestructor();
3423 if (DD) {
3424 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3425 DD->isDeleted()) {
3426 Diag(ELoc, diag::err_omp_required_method)
3427 << getOpenMPClauseName(OMPC_reduction) << 4;
3428 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3429 VarDecl::DeclarationOnly;
3430 Diag(VD->getLocation(),
3431 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3432 << VD;
3433 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3434 continue;
3435 }
3436 MarkFunctionReferenced(ELoc, DD);
3437 DiagnoseUseOfDecl(DD, ELoc);
3438 }
3439 }
3440
3441 DSAStack->addDSA(VD, DE, OMPC_reduction);
3442 Vars.push_back(DE);
3443 }
3444
3445 if (Vars.empty())
3446 return nullptr;
3447
3448 return OMPReductionClause::Create(
3449 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3450 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3451}
3452
Alexander Musman8dba6642014-04-22 13:09:42 +00003453OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3454 SourceLocation StartLoc,
3455 SourceLocation LParenLoc,
3456 SourceLocation ColonLoc,
3457 SourceLocation EndLoc) {
3458 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003459 for (auto &RefExpr : VarList) {
3460 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3461 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003462 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003463 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003464 continue;
3465 }
3466
3467 // OpenMP [2.14.3.7, linear clause]
3468 // A list item that appears in a linear clause is subject to the private
3469 // clause semantics described in Section 2.14.3.3 on page 159 except as
3470 // noted. In addition, the value of the new list item on each iteration
3471 // of the associated loop(s) corresponds to the value of the original
3472 // list item before entering the construct plus the logical number of
3473 // the iteration times linear-step.
3474
Alexey Bataeved09d242014-05-28 05:53:51 +00003475 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003476 // OpenMP [2.1, C/C++]
3477 // A list item is a variable name.
3478 // OpenMP [2.14.3.3, Restrictions, p.1]
3479 // A variable that is part of another variable (as an array or
3480 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003481 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003482 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003483 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003484 continue;
3485 }
3486
3487 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3488
3489 // OpenMP [2.14.3.7, linear clause]
3490 // A list-item cannot appear in more than one linear clause.
3491 // A list-item that appears in a linear clause cannot appear in any
3492 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003493 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003494 if (DVar.RefExpr) {
3495 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3496 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003497 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003498 continue;
3499 }
3500
3501 QualType QType = VD->getType();
3502 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3503 // It will be analyzed later.
3504 Vars.push_back(DE);
3505 continue;
3506 }
3507
3508 // A variable must not have an incomplete type or a reference type.
3509 if (RequireCompleteType(ELoc, QType,
3510 diag::err_omp_linear_incomplete_type)) {
3511 continue;
3512 }
3513 if (QType->isReferenceType()) {
3514 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3515 << getOpenMPClauseName(OMPC_linear) << QType;
3516 bool IsDecl =
3517 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3518 Diag(VD->getLocation(),
3519 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3520 << VD;
3521 continue;
3522 }
3523
3524 // A list item must not be const-qualified.
3525 if (QType.isConstant(Context)) {
3526 Diag(ELoc, diag::err_omp_const_variable)
3527 << getOpenMPClauseName(OMPC_linear);
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 // A list item must be of integral or pointer type.
3537 QType = QType.getUnqualifiedType().getCanonicalType();
3538 const Type *Ty = QType.getTypePtrOrNull();
3539 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3540 !Ty->isPointerType())) {
3541 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3542 bool IsDecl =
3543 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3544 Diag(VD->getLocation(),
3545 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3546 << VD;
3547 continue;
3548 }
3549
3550 DSAStack->addDSA(VD, DE, OMPC_linear);
3551 Vars.push_back(DE);
3552 }
3553
3554 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003555 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003556
3557 Expr *StepExpr = Step;
3558 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3559 !Step->isInstantiationDependent() &&
3560 !Step->containsUnexpandedParameterPack()) {
3561 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003562 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003563 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003564 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003565 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003566
3567 // Warn about zero linear step (it would be probably better specified as
3568 // making corresponding variables 'const').
3569 llvm::APSInt Result;
3570 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3571 !Result.isNegative() && !Result.isStrictlyPositive())
3572 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3573 << (Vars.size() > 1);
3574 }
3575
3576 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3577 Vars, StepExpr);
3578}
3579
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003580OMPClause *Sema::ActOnOpenMPAlignedClause(
3581 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3582 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3583
3584 SmallVector<Expr *, 8> Vars;
3585 for (auto &RefExpr : VarList) {
3586 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3587 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3588 // It will be analyzed later.
3589 Vars.push_back(RefExpr);
3590 continue;
3591 }
3592
3593 SourceLocation ELoc = RefExpr->getExprLoc();
3594 // OpenMP [2.1, C/C++]
3595 // A list item is a variable name.
3596 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3597 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3598 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3599 continue;
3600 }
3601
3602 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3603
3604 // OpenMP [2.8.1, simd construct, Restrictions]
3605 // The type of list items appearing in the aligned clause must be
3606 // array, pointer, reference to array, or reference to pointer.
3607 QualType QType = DE->getType()
3608 .getNonReferenceType()
3609 .getUnqualifiedType()
3610 .getCanonicalType();
3611 const Type *Ty = QType.getTypePtrOrNull();
3612 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3613 !Ty->isPointerType())) {
3614 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3615 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3616 bool IsDecl =
3617 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3618 Diag(VD->getLocation(),
3619 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3620 << VD;
3621 continue;
3622 }
3623
3624 // OpenMP [2.8.1, simd construct, Restrictions]
3625 // A list-item cannot appear in more than one aligned clause.
3626 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3627 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3628 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3629 << getOpenMPClauseName(OMPC_aligned);
3630 continue;
3631 }
3632
3633 Vars.push_back(DE);
3634 }
3635
3636 // OpenMP [2.8.1, simd construct, Description]
3637 // The parameter of the aligned clause, alignment, must be a constant
3638 // positive integer expression.
3639 // If no optional parameter is specified, implementation-defined default
3640 // alignments for SIMD instructions on the target platforms are assumed.
3641 if (Alignment != nullptr) {
3642 ExprResult AlignResult =
3643 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3644 if (AlignResult.isInvalid())
3645 return nullptr;
3646 Alignment = AlignResult.get();
3647 }
3648 if (Vars.empty())
3649 return nullptr;
3650
3651 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3652 EndLoc, Vars, Alignment);
3653}
3654
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003655OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3656 SourceLocation StartLoc,
3657 SourceLocation LParenLoc,
3658 SourceLocation EndLoc) {
3659 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003660 for (auto &RefExpr : VarList) {
3661 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3662 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003663 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003664 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003665 continue;
3666 }
3667
Alexey Bataeved09d242014-05-28 05:53:51 +00003668 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003669 // OpenMP [2.1, C/C++]
3670 // A list item is a variable name.
3671 // OpenMP [2.14.4.1, Restrictions, p.1]
3672 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003673 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003674 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003675 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003676 continue;
3677 }
3678
3679 Decl *D = DE->getDecl();
3680 VarDecl *VD = cast<VarDecl>(D);
3681
3682 QualType Type = VD->getType();
3683 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3684 // It will be analyzed later.
3685 Vars.push_back(DE);
3686 continue;
3687 }
3688
3689 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3690 // A list item that appears in a copyin clause must be threadprivate.
3691 if (!DSAStack->isThreadPrivate(VD)) {
3692 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003693 << getOpenMPClauseName(OMPC_copyin)
3694 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003695 continue;
3696 }
3697
3698 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3699 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003700 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003701 // operator for the class type.
3702 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003703 CXXRecordDecl *RD =
3704 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003705 // FIXME This code must be replaced by actual assignment of the
3706 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003707 if (RD) {
3708 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3709 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003710 if (MD) {
3711 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3712 MD->isDeleted()) {
3713 Diag(ELoc, diag::err_omp_required_method)
3714 << getOpenMPClauseName(OMPC_copyin) << 2;
3715 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3716 VarDecl::DeclarationOnly;
3717 Diag(VD->getLocation(),
3718 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3719 << VD;
3720 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3721 continue;
3722 }
3723 MarkFunctionReferenced(ELoc, MD);
3724 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003725 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003726 }
3727
3728 DSAStack->addDSA(VD, DE, OMPC_copyin);
3729 Vars.push_back(DE);
3730 }
3731
Alexey Bataeved09d242014-05-28 05:53:51 +00003732 if (Vars.empty())
3733 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003734
3735 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3736}
3737
Alexey Bataevbae9a792014-06-27 10:37:06 +00003738OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
3739 SourceLocation StartLoc,
3740 SourceLocation LParenLoc,
3741 SourceLocation EndLoc) {
3742 SmallVector<Expr *, 8> Vars;
3743 for (auto &RefExpr : VarList) {
3744 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
3745 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3746 // It will be analyzed later.
3747 Vars.push_back(RefExpr);
3748 continue;
3749 }
3750
3751 SourceLocation ELoc = RefExpr->getExprLoc();
3752 // OpenMP [2.1, C/C++]
3753 // A list item is a variable name.
3754 // OpenMP [2.14.4.1, Restrictions, p.1]
3755 // A list item that appears in a copyin clause must be threadprivate.
3756 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3757 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3758 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3759 continue;
3760 }
3761
3762 Decl *D = DE->getDecl();
3763 VarDecl *VD = cast<VarDecl>(D);
3764
3765 QualType Type = VD->getType();
3766 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3767 // It will be analyzed later.
3768 Vars.push_back(DE);
3769 continue;
3770 }
3771
3772 // OpenMP [2.14.4.2, Restrictions, p.2]
3773 // A list item that appears in a copyprivate clause may not appear in a
3774 // private or firstprivate clause on the single construct.
3775 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003776 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003777 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
3778 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
3779 Diag(ELoc, diag::err_omp_wrong_dsa)
3780 << getOpenMPClauseName(DVar.CKind)
3781 << getOpenMPClauseName(OMPC_copyprivate);
3782 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3783 continue;
3784 }
3785
3786 // OpenMP [2.11.4.2, Restrictions, p.1]
3787 // All list items that appear in a copyprivate clause must be either
3788 // threadprivate or private in the enclosing context.
3789 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003790 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003791 if (DVar.CKind == OMPC_shared) {
3792 Diag(ELoc, diag::err_omp_required_access)
3793 << getOpenMPClauseName(OMPC_copyprivate)
3794 << "threadprivate or private in the enclosing context";
3795 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3796 continue;
3797 }
3798 }
3799 }
3800
3801 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3802 // A variable of class type (or array thereof) that appears in a
3803 // copyin clause requires an accessible, unambiguous copy assignment
3804 // operator for the class type.
3805 Type = Context.getBaseElementType(Type);
3806 CXXRecordDecl *RD =
3807 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
3808 // FIXME This code must be replaced by actual assignment of the
3809 // threadprivate variable.
3810 if (RD) {
3811 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3812 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
3813 if (MD) {
3814 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3815 MD->isDeleted()) {
3816 Diag(ELoc, diag::err_omp_required_method)
3817 << getOpenMPClauseName(OMPC_copyprivate) << 2;
3818 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3819 VarDecl::DeclarationOnly;
3820 Diag(VD->getLocation(),
3821 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3822 << VD;
3823 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3824 continue;
3825 }
3826 MarkFunctionReferenced(ELoc, MD);
3827 DiagnoseUseOfDecl(MD, ELoc);
3828 }
3829 }
3830
3831 // No need to mark vars as copyprivate, they are already threadprivate or
3832 // implicitly private.
3833 Vars.push_back(DE);
3834 }
3835
3836 if (Vars.empty())
3837 return nullptr;
3838
3839 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3840}
3841
Alexey Bataev758e55e2013-09-06 18:03:48 +00003842#undef DSAStack