blob: 7ec0d2b944a8aa270163300e8fc1efc7a4724c26 [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 Bataev9fb6e642014-07-22 06:45:04 +000094 bool OrderedRegion;
Alexey Bataevf98b00c2014-07-23 02:27:21 +000095 SourceLocation AtomicClauseLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +000096 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000097 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000098 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +000099 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000100 ConstructLoc(Loc), OrderedRegion(false), AtomicClauseLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000101 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000102 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000104 ConstructLoc(), OrderedRegion(false), AtomicClauseLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000105 };
106
107 typedef SmallVector<SharingMapTy, 64> StackTy;
108
109 /// \brief Stack of used declaration and their data-sharing attributes.
110 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000111 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000112
113 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
114
115 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000116
117 /// \brief Checks if the variable is a local for OpenMP region.
118 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000119
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000121 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122
123 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000124 Scope *CurScope, SourceLocation Loc) {
125 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
126 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 }
128
129 void pop() {
130 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
131 Stack.pop_back();
132 }
133
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000134 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000135 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000136 /// for diagnostics.
137 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
138
Alexey Bataev758e55e2013-09-06 18:03:48 +0000139 /// \brief Adds explicit data sharing attribute to the specified declaration.
140 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
141
Alexey Bataev758e55e2013-09-06 18:03:48 +0000142 /// \brief Returns data sharing attributes from top of the stack for the
143 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000144 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000145 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000146 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000147 /// \brief Checks if the specified variables has data-sharing attributes which
148 /// match specified \a CPred predicate in any directive which matches \a DPred
149 /// predicate.
150 template <class ClausesPredicate, class DirectivesPredicate>
151 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000152 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000153 /// \brief Checks if the specified variables has data-sharing attributes which
154 /// match specified \a CPred predicate in any innermost directive which
155 /// matches \a DPred predicate.
156 template <class ClausesPredicate, class DirectivesPredicate>
157 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000158 DirectivesPredicate DPred,
159 bool FromParent);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000160 /// \brief Finds a directive which matches specified \a DPred predicate.
161 template <class NamedDirectivesPredicate>
162 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000163
Alexey Bataev758e55e2013-09-06 18:03:48 +0000164 /// \brief Returns currently analyzed directive.
165 OpenMPDirectiveKind getCurrentDirective() const {
166 return Stack.back().Directive;
167 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000168 /// \brief Returns parent directive.
169 OpenMPDirectiveKind getParentDirective() const {
170 if (Stack.size() > 2)
171 return Stack[Stack.size() - 2].Directive;
172 return OMPD_unknown;
173 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000174
175 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000176 void setDefaultDSANone(SourceLocation Loc) {
177 Stack.back().DefaultAttr = DSA_none;
178 Stack.back().DefaultAttrLoc = Loc;
179 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000181 void setDefaultDSAShared(SourceLocation Loc) {
182 Stack.back().DefaultAttr = DSA_shared;
183 Stack.back().DefaultAttrLoc = Loc;
184 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000185
186 DefaultDataSharingAttributes getDefaultDSA() const {
187 return Stack.back().DefaultAttr;
188 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000189 SourceLocation getDefaultDSALocation() const {
190 return Stack.back().DefaultAttrLoc;
191 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000192
Alexey Bataevf29276e2014-06-18 04:14:57 +0000193 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000194 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000195 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000196 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000197 }
198
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000199 /// \brief Marks current region as ordered (it has an 'ordered' clause).
200 void setOrderedRegion(bool IsOrdered = true) {
201 Stack.back().OrderedRegion = IsOrdered;
202 }
203 /// \brief Returns true, if parent region is ordered (has associated
204 /// 'ordered' clause), false - otherwise.
205 bool isParentOrderedRegion() const {
206 if (Stack.size() > 2)
207 return Stack[Stack.size() - 2].OrderedRegion;
208 return false;
209 }
210
Alexey Bataevf98b00c2014-07-23 02:27:21 +0000211 /// \brief Checks if the 'atomic' construct has explicitly specified 'read',
212 /// 'update', 'write' or 'capture' clause.
213 bool hasAtomicClause() const {
214 return Stack.back().AtomicClauseLoc.isValid();
215 }
216 /// \brief Gets location of explicitly specified clause for 'atomic'
217 /// construct.
218 SourceLocation getAtomicClauseLoc() const {
219 return Stack.back().AtomicClauseLoc;
220 }
221 /// \brief Sets location of explicitly specified clause for 'atomic'
222 /// directive.
223 void setAtomicClauseLoc(SourceLocation Loc) {
224 Stack.back().AtomicClauseLoc = Loc;
225 }
226
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000227 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000228 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000229 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000230};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000231bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
232 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
233 DKind == OMPD_unknown;
234}
Alexey Bataeved09d242014-05-28 05:53:51 +0000235} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000236
237DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
238 VarDecl *D) {
239 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000240 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000241 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
242 // in a region but not in construct]
243 // File-scope or namespace-scope variables referenced in called routines
244 // in the region are shared unless they appear in a threadprivate
245 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000246 if (!D->isFunctionOrMethodVarDecl())
247 DVar.CKind = OMPC_shared;
248
249 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
250 // in a region but not in construct]
251 // Variables with static storage duration that are declared in called
252 // routines in the region are shared.
253 if (D->hasGlobalStorage())
254 DVar.CKind = OMPC_shared;
255
Alexey Bataev758e55e2013-09-06 18:03:48 +0000256 return DVar;
257 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000258
Alexey Bataev758e55e2013-09-06 18:03:48 +0000259 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000260 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
261 // in a Construct, C/C++, predetermined, p.1]
262 // Variables with automatic storage duration that are declared in a scope
263 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000264 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
265 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
266 DVar.CKind = OMPC_private;
267 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000268 }
269
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270 // Explicitly specified attributes and local variables with predetermined
271 // attributes.
272 if (Iter->SharingMap.count(D)) {
273 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
274 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000275 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000276 return DVar;
277 }
278
279 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
280 // in a Construct, C/C++, implicitly determined, p.1]
281 // In a parallel or task construct, the data-sharing attributes of these
282 // variables are determined by the default clause, if present.
283 switch (Iter->DefaultAttr) {
284 case DSA_shared:
285 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000286 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000287 return DVar;
288 case DSA_none:
289 return DVar;
290 case DSA_unspecified:
291 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
292 // in a Construct, implicitly determined, p.2]
293 // In a parallel construct, if no default clause is present, these
294 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000295 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000296 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000297 DVar.CKind = OMPC_shared;
298 return DVar;
299 }
300
301 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
302 // in a Construct, implicitly determined, p.4]
303 // In a task construct, if no default clause is present, a variable that in
304 // the enclosing context is determined to be shared by all implicit tasks
305 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306 if (DVar.DKind == OMPD_task) {
307 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000308 for (StackTy::reverse_iterator I = std::next(Iter),
309 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000310 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000311 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
312 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000313 // in a Construct, implicitly determined, p.6]
314 // In a task construct, if no default clause is present, a variable
315 // whose data-sharing attribute is not determined by the rules above is
316 // firstprivate.
317 DVarTemp = getDSA(I, D);
318 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000319 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000320 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000321 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322 return DVar;
323 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000324 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000325 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000326 }
327 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000328 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000329 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000330 return DVar;
331 }
332 }
333 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
334 // in a Construct, implicitly determined, p.3]
335 // For constructs other than task, if no default clause is present, these
336 // variables inherit their data-sharing attributes from the enclosing
337 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000338 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000339}
340
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000341DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
342 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
343 auto It = Stack.back().AlignedMap.find(D);
344 if (It == Stack.back().AlignedMap.end()) {
345 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
346 Stack.back().AlignedMap[D] = NewDE;
347 return nullptr;
348 } else {
349 assert(It->second && "Unexpected nullptr expr in the aligned map");
350 return It->second;
351 }
352 return nullptr;
353}
354
Alexey Bataev758e55e2013-09-06 18:03:48 +0000355void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
356 if (A == OMPC_threadprivate) {
357 Stack[0].SharingMap[D].Attributes = A;
358 Stack[0].SharingMap[D].RefExpr = E;
359 } else {
360 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
361 Stack.back().SharingMap[D].Attributes = A;
362 Stack.back().SharingMap[D].RefExpr = E;
363 }
364}
365
Alexey Bataeved09d242014-05-28 05:53:51 +0000366bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000367 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000368 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000369 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000370 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000371 ++I;
372 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000373 if (I == E)
374 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000375 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000376 Scope *CurScope = getCurScope();
377 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000378 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000379 }
380 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000382 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383}
384
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000385DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386 DSAVarData DVar;
387
388 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
389 // in a Construct, C/C++, predetermined, p.1]
390 // Variables appearing in threadprivate directives are threadprivate.
391 if (D->getTLSKind() != VarDecl::TLS_None) {
392 DVar.CKind = OMPC_threadprivate;
393 return DVar;
394 }
395 if (Stack[0].SharingMap.count(D)) {
396 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
397 DVar.CKind = OMPC_threadprivate;
398 return DVar;
399 }
400
401 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
402 // in a Construct, C/C++, predetermined, p.1]
403 // Variables with automatic storage duration that are declared in a scope
404 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000405 OpenMPDirectiveKind Kind =
406 FromParent ? getParentDirective() : getCurrentDirective();
407 auto StartI = std::next(Stack.rbegin());
408 auto EndI = std::prev(Stack.rend());
409 if (FromParent && StartI != EndI) {
410 StartI = std::next(StartI);
411 }
412 if (!isParallelOrTaskRegion(Kind)) {
413 if (isOpenMPLocal(D, StartI) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000414 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000415 DVar.CKind = OMPC_private;
416 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000417 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000418 }
419
420 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
421 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000422 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000424 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000425 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000426 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
427 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000428 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
429 return DVar;
430
Alexey Bataev758e55e2013-09-06 18:03:48 +0000431 DVar.CKind = OMPC_shared;
432 return DVar;
433 }
434
435 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000436 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000437 while (Type->isArrayType()) {
438 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
439 Type = ElemType.getNonReferenceType().getCanonicalType();
440 }
441 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
442 // in a Construct, C/C++, predetermined, p.6]
443 // Variables with const qualified type having no mutable member are
444 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000445 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000446 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000448 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000449 // Variables with const-qualified type having no mutable member may be
450 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000451 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
452 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000453 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
454 return DVar;
455
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456 DVar.CKind = OMPC_shared;
457 return DVar;
458 }
459
460 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
461 // in a Construct, C/C++, predetermined, p.7]
462 // Variables with static storage duration that are declared in a scope
463 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000464 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 DVar.CKind = OMPC_shared;
466 return DVar;
467 }
468
469 // Explicitly specified attributes and local variables with predetermined
470 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000471 auto I = std::prev(StartI);
472 if (I->SharingMap.count(D)) {
473 DVar.RefExpr = I->SharingMap[D].RefExpr;
474 DVar.CKind = I->SharingMap[D].Attributes;
475 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 }
477
478 return DVar;
479}
480
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000481DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
482 auto StartI = Stack.rbegin();
483 auto EndI = std::prev(Stack.rend());
484 if (FromParent && StartI != EndI) {
485 StartI = std::next(StartI);
486 }
487 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488}
489
Alexey Bataevf29276e2014-06-18 04:14:57 +0000490template <class ClausesPredicate, class DirectivesPredicate>
491DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000492 DirectivesPredicate DPred,
493 bool FromParent) {
494 auto StartI = std::next(Stack.rbegin());
495 auto EndI = std::prev(Stack.rend());
496 if (FromParent && StartI != EndI) {
497 StartI = std::next(StartI);
498 }
499 for (auto I = StartI, EE = EndI; I != EE; ++I) {
500 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000501 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000502 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000503 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000504 return DVar;
505 }
506 return DSAVarData();
507}
508
Alexey Bataevf29276e2014-06-18 04:14:57 +0000509template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000510DSAStackTy::DSAVarData
511DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
512 DirectivesPredicate DPred, bool FromParent) {
513 auto StartI = std::next(Stack.rbegin());
514 auto EndI = std::prev(Stack.rend());
515 if (FromParent && StartI != EndI) {
516 StartI = std::next(StartI);
517 }
518 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000519 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000520 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000521 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000522 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000523 return DVar;
524 return DSAVarData();
525 }
526 return DSAVarData();
527}
528
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000529template <class NamedDirectivesPredicate>
530bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
531 auto StartI = std::next(Stack.rbegin());
532 auto EndI = std::prev(Stack.rend());
533 if (FromParent && StartI != EndI) {
534 StartI = std::next(StartI);
535 }
536 for (auto I = StartI, EE = EndI; I != EE; ++I) {
537 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
538 return true;
539 }
540 return false;
541}
542
Alexey Bataev758e55e2013-09-06 18:03:48 +0000543void Sema::InitDataSharingAttributesStack() {
544 VarDataSharingAttributesStack = new DSAStackTy(*this);
545}
546
547#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
548
Alexey Bataeved09d242014-05-28 05:53:51 +0000549void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000550
551void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
552 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000553 Scope *CurScope, SourceLocation Loc) {
554 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000555 PushExpressionEvaluationContext(PotentiallyEvaluated);
556}
557
558void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000559 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
560 // A variable of class type (or array thereof) that appears in a lastprivate
561 // clause requires an accessible, unambiguous default constructor for the
562 // class type, unless the list item is also specified in a firstprivate
563 // clause.
564 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
565 for (auto C : D->clauses()) {
566 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
567 for (auto VarRef : Clause->varlists()) {
568 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
569 continue;
570 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000571 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000572 if (DVar.CKind == OMPC_lastprivate) {
573 SourceLocation ELoc = VarRef->getExprLoc();
574 auto Type = VarRef->getType();
575 if (Type->isArrayType())
576 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
577 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000578 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
579 // FIXME This code must be replaced by actual constructing of the
580 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000581 if (RD) {
582 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
583 PartialDiagnostic PD =
584 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
585 if (!CD ||
586 CheckConstructorAccess(
587 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
588 CD->getAccess(), PD) == AR_inaccessible ||
589 CD->isDeleted()) {
590 Diag(ELoc, diag::err_omp_required_method)
591 << getOpenMPClauseName(OMPC_lastprivate) << 0;
592 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
593 VarDecl::DeclarationOnly;
594 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
595 : diag::note_defined_here)
596 << VD;
597 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
598 continue;
599 }
600 MarkFunctionReferenced(ELoc, CD);
601 DiagnoseUseOfDecl(CD, ELoc);
602 }
603 }
604 }
605 }
606 }
607 }
608
Alexey Bataev758e55e2013-09-06 18:03:48 +0000609 DSAStack->pop();
610 DiscardCleanupsInEvaluationContext();
611 PopExpressionEvaluationContext();
612}
613
Alexey Bataeva769e072013-03-22 06:34:35 +0000614namespace {
615
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000616class VarDeclFilterCCC : public CorrectionCandidateCallback {
617private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000618 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000619
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000620public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000621 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000622 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000623 NamedDecl *ND = Candidate.getCorrectionDecl();
624 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
625 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000626 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
627 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000628 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000629 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000630 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000631};
Alexey Bataeved09d242014-05-28 05:53:51 +0000632} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000633
634ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
635 CXXScopeSpec &ScopeSpec,
636 const DeclarationNameInfo &Id) {
637 LookupResult Lookup(*this, Id, LookupOrdinaryName);
638 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
639
640 if (Lookup.isAmbiguous())
641 return ExprError();
642
643 VarDecl *VD;
644 if (!Lookup.isSingleResult()) {
645 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000646 if (TypoCorrection Corrected =
647 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
648 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000649 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000650 PDiag(Lookup.empty()
651 ? diag::err_undeclared_var_use_suggest
652 : diag::err_omp_expected_var_arg_suggest)
653 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000654 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000655 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000656 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
657 : diag::err_omp_expected_var_arg)
658 << Id.getName();
659 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000660 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000661 } else {
662 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000663 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000664 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
665 return ExprError();
666 }
667 }
668 Lookup.suppressDiagnostics();
669
670 // OpenMP [2.9.2, Syntax, C/C++]
671 // Variables must be file-scope, namespace-scope, or static block-scope.
672 if (!VD->hasGlobalStorage()) {
673 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000674 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
675 bool IsDecl =
676 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000677 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000678 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
679 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000680 return ExprError();
681 }
682
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000683 VarDecl *CanonicalVD = VD->getCanonicalDecl();
684 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000685 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
686 // A threadprivate directive for file-scope variables must appear outside
687 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000688 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
689 !getCurLexicalContext()->isTranslationUnit()) {
690 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000691 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
692 bool IsDecl =
693 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
694 Diag(VD->getLocation(),
695 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
696 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000697 return ExprError();
698 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000699 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
700 // A threadprivate directive for static class member variables must appear
701 // in the class definition, in the same scope in which the member
702 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000703 if (CanonicalVD->isStaticDataMember() &&
704 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
705 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000706 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
707 bool IsDecl =
708 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
709 Diag(VD->getLocation(),
710 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
711 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000712 return ExprError();
713 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000714 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
715 // A threadprivate directive for namespace-scope variables must appear
716 // outside any definition or declaration other than the namespace
717 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000718 if (CanonicalVD->getDeclContext()->isNamespace() &&
719 (!getCurLexicalContext()->isFileContext() ||
720 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
721 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000722 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
723 bool IsDecl =
724 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
725 Diag(VD->getLocation(),
726 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
727 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000728 return ExprError();
729 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000730 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
731 // A threadprivate directive for static block-scope variables must appear
732 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000733 if (CanonicalVD->isStaticLocal() && CurScope &&
734 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000735 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000736 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
737 bool IsDecl =
738 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
739 Diag(VD->getLocation(),
740 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
741 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000742 return ExprError();
743 }
744
745 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
746 // A threadprivate directive must lexically precede all references to any
747 // of the variables in its list.
748 if (VD->isUsed()) {
749 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000750 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000751 return ExprError();
752 }
753
754 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000755 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000756 return DE;
757}
758
Alexey Bataeved09d242014-05-28 05:53:51 +0000759Sema::DeclGroupPtrTy
760Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
761 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000762 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000763 CurContext->addDecl(D);
764 return DeclGroupPtrTy::make(DeclGroupRef(D));
765 }
766 return DeclGroupPtrTy();
767}
768
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000769namespace {
770class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
771 Sema &SemaRef;
772
773public:
774 bool VisitDeclRefExpr(const DeclRefExpr *E) {
775 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
776 if (VD->hasLocalStorage()) {
777 SemaRef.Diag(E->getLocStart(),
778 diag::err_omp_local_var_in_threadprivate_init)
779 << E->getSourceRange();
780 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
781 << VD << VD->getSourceRange();
782 return true;
783 }
784 }
785 return false;
786 }
787 bool VisitStmt(const Stmt *S) {
788 for (auto Child : S->children()) {
789 if (Child && Visit(Child))
790 return true;
791 }
792 return false;
793 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000794 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000795};
796} // namespace
797
Alexey Bataeved09d242014-05-28 05:53:51 +0000798OMPThreadPrivateDecl *
799Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000800 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000801 for (auto &RefExpr : VarList) {
802 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000803 VarDecl *VD = cast<VarDecl>(DE->getDecl());
804 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000805
806 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
807 // A threadprivate variable must not have an incomplete type.
808 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000809 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000810 continue;
811 }
812
813 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
814 // A threadprivate variable must not have a reference type.
815 if (VD->getType()->isReferenceType()) {
816 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000817 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
818 bool IsDecl =
819 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
820 Diag(VD->getLocation(),
821 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
822 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000823 continue;
824 }
825
Richard Smithfd3834f2013-04-13 02:43:54 +0000826 // Check if this is a TLS variable.
827 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000828 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000829 bool IsDecl =
830 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
831 Diag(VD->getLocation(),
832 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
833 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000834 continue;
835 }
836
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000837 // Check if initial value of threadprivate variable reference variable with
838 // local storage (it is not supported by runtime).
839 if (auto Init = VD->getAnyInitializer()) {
840 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000841 if (Checker.Visit(Init))
842 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000843 }
844
Alexey Bataeved09d242014-05-28 05:53:51 +0000845 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000846 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000847 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000848 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000849 if (!Vars.empty()) {
850 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
851 Vars);
852 D->setAccess(AS_public);
853 }
854 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000855}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000856
Alexey Bataev7ff55242014-06-19 09:13:45 +0000857static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
858 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
859 bool IsLoopIterVar = false) {
860 if (DVar.RefExpr) {
861 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
862 << getOpenMPClauseName(DVar.CKind);
863 return;
864 }
865 enum {
866 PDSA_StaticMemberShared,
867 PDSA_StaticLocalVarShared,
868 PDSA_LoopIterVarPrivate,
869 PDSA_LoopIterVarLinear,
870 PDSA_LoopIterVarLastprivate,
871 PDSA_ConstVarShared,
872 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000873 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000874 PDSA_LocalVarPrivate,
875 PDSA_Implicit
876 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000877 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000878 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000879 if (IsLoopIterVar) {
880 if (DVar.CKind == OMPC_private)
881 Reason = PDSA_LoopIterVarPrivate;
882 else if (DVar.CKind == OMPC_lastprivate)
883 Reason = PDSA_LoopIterVarLastprivate;
884 else
885 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000886 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
887 Reason = PDSA_TaskVarFirstprivate;
888 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000889 } else if (VD->isStaticLocal())
890 Reason = PDSA_StaticLocalVarShared;
891 else if (VD->isStaticDataMember())
892 Reason = PDSA_StaticMemberShared;
893 else if (VD->isFileVarDecl())
894 Reason = PDSA_GlobalVarShared;
895 else if (VD->getType().isConstant(SemaRef.getASTContext()))
896 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000897 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000898 ReportHint = true;
899 Reason = PDSA_LocalVarPrivate;
900 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000901 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000902 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000903 << Reason << ReportHint
904 << getOpenMPDirectiveName(Stack->getCurrentDirective());
905 } else if (DVar.ImplicitDSALoc.isValid()) {
906 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
907 << getOpenMPClauseName(DVar.CKind);
908 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000909}
910
Alexey Bataev758e55e2013-09-06 18:03:48 +0000911namespace {
912class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
913 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000914 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000915 bool ErrorFound;
916 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000917 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000918 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000919
Alexey Bataev758e55e2013-09-06 18:03:48 +0000920public:
921 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000922 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000923 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000924 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
925 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000926
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000927 auto DVar = Stack->getTopDSA(VD, false);
928 // Check if the variable has explicit DSA set and stop analysis if it so.
929 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000930
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000931 auto ELoc = E->getExprLoc();
932 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000933 // The default(none) clause requires that each variable that is referenced
934 // in the construct, and does not have a predetermined data-sharing
935 // attribute, must have its data-sharing attribute explicitly determined
936 // by being listed in a data-sharing attribute clause.
937 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000938 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000939 VarsWithInheritedDSA.count(VD) == 0) {
940 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000941 return;
942 }
943
944 // OpenMP [2.9.3.6, Restrictions, p.2]
945 // A list item that appears in a reduction clause of the innermost
946 // enclosing worksharing or parallel construct may not be accessed in an
947 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000948 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000949 [](OpenMPDirectiveKind K) -> bool {
950 return isOpenMPParallelDirective(K) ||
951 isOpenMPWorksharingDirective(K);
952 },
953 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000954 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
955 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000956 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
957 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000958 return;
959 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000960
961 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000962 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000963 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000964 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000965 }
966 }
967 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000968 for (auto *C : S->clauses()) {
969 // Skip analysis of arguments of implicitly defined firstprivate clause
970 // for task directives.
971 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
972 for (auto *CC : C->children()) {
973 if (CC)
974 Visit(CC);
975 }
976 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000977 }
978 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000979 for (auto *C : S->children()) {
980 if (C && !isa<OMPExecutableDirective>(C))
981 Visit(C);
982 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000983 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000984
985 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000986 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000987 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
988 return VarsWithInheritedDSA;
989 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000990
Alexey Bataev7ff55242014-06-19 09:13:45 +0000991 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
992 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000993};
Alexey Bataeved09d242014-05-28 05:53:51 +0000994} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000995
Alexey Bataevbae9a792014-06-27 10:37:06 +0000996void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000997 switch (DKind) {
998 case OMPD_parallel: {
999 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1000 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001001 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001002 std::make_pair(".global_tid.", KmpInt32PtrTy),
1003 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1004 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001005 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001006 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1007 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001008 break;
1009 }
1010 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001011 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001012 std::make_pair(StringRef(), QualType()) // __context with shared vars
1013 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001014 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1015 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001016 break;
1017 }
1018 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001019 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001020 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001021 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001022 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1023 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001024 break;
1025 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001026 case OMPD_sections: {
1027 Sema::CapturedParamNameType Params[] = {
1028 std::make_pair(StringRef(), QualType()) // __context with shared vars
1029 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001030 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1031 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001032 break;
1033 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001034 case OMPD_section: {
1035 Sema::CapturedParamNameType Params[] = {
1036 std::make_pair(StringRef(), QualType()) // __context with shared vars
1037 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001038 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1039 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001040 break;
1041 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001042 case OMPD_single: {
1043 Sema::CapturedParamNameType Params[] = {
1044 std::make_pair(StringRef(), QualType()) // __context with shared vars
1045 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001046 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1047 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001048 break;
1049 }
Alexander Musman80c22892014-07-17 08:54:58 +00001050 case OMPD_master: {
1051 Sema::CapturedParamNameType Params[] = {
1052 std::make_pair(StringRef(), QualType()) // __context with shared vars
1053 };
1054 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1055 Params);
1056 break;
1057 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001058 case OMPD_critical: {
1059 Sema::CapturedParamNameType Params[] = {
1060 std::make_pair(StringRef(), QualType()) // __context with shared vars
1061 };
1062 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1063 Params);
1064 break;
1065 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001066 case OMPD_parallel_for: {
1067 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1068 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1069 Sema::CapturedParamNameType Params[] = {
1070 std::make_pair(".global_tid.", KmpInt32PtrTy),
1071 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1072 std::make_pair(StringRef(), QualType()) // __context with shared vars
1073 };
1074 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1075 Params);
1076 break;
1077 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001078 case OMPD_parallel_sections: {
1079 Sema::CapturedParamNameType Params[] = {
1080 std::make_pair(StringRef(), QualType()) // __context with shared vars
1081 };
1082 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1083 Params);
1084 break;
1085 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001086 case OMPD_task: {
1087 Sema::CapturedParamNameType Params[] = {
1088 std::make_pair(StringRef(), QualType()) // __context with shared vars
1089 };
1090 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1091 Params);
1092 break;
1093 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001094 case OMPD_taskyield: {
1095 Sema::CapturedParamNameType Params[] = {
1096 std::make_pair(StringRef(), QualType()) // __context with shared vars
1097 };
1098 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1099 Params);
1100 break;
1101 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001102 case OMPD_barrier: {
1103 Sema::CapturedParamNameType Params[] = {
1104 std::make_pair(StringRef(), QualType()) // __context with shared vars
1105 };
1106 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1107 Params);
1108 break;
1109 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001110 case OMPD_taskwait: {
1111 Sema::CapturedParamNameType Params[] = {
1112 std::make_pair(StringRef(), QualType()) // __context with shared vars
1113 };
1114 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1115 Params);
1116 break;
1117 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001118 case OMPD_flush: {
1119 Sema::CapturedParamNameType Params[] = {
1120 std::make_pair(StringRef(), QualType()) // __context with shared vars
1121 };
1122 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1123 Params);
1124 break;
1125 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001126 case OMPD_ordered: {
1127 Sema::CapturedParamNameType Params[] = {
1128 std::make_pair(StringRef(), QualType()) // __context with shared vars
1129 };
1130 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1131 Params);
1132 break;
1133 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001134 case OMPD_atomic: {
1135 Sema::CapturedParamNameType Params[] = {
1136 std::make_pair(StringRef(), QualType()) // __context with shared vars
1137 };
1138 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1139 Params);
1140 break;
1141 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001142 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001143 llvm_unreachable("OpenMP Directive is not allowed");
1144 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001145 llvm_unreachable("Unknown OpenMP directive");
1146 }
1147}
1148
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001149static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1150 OpenMPDirectiveKind CurrentRegion,
1151 const DeclarationNameInfo &CurrentName,
1152 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001153 // Allowed nesting of constructs
1154 // +------------------+-----------------+------------------------------------+
1155 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1156 // +------------------+-----------------+------------------------------------+
1157 // | parallel | parallel | * |
1158 // | parallel | for | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001159 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001160 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001161 // | parallel | simd | * |
1162 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001163 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001164 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001165 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001166 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001167 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001168 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001169 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001170 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001171 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001172 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001173 // | parallel | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001174 // +------------------+-----------------+------------------------------------+
1175 // | for | parallel | * |
1176 // | for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001177 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001178 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001179 // | for | simd | * |
1180 // | for | sections | + |
1181 // | for | section | + |
1182 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001183 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001184 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001185 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001186 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001187 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001188 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001189 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001190 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001191 // | for | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001192 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001193 // | master | parallel | * |
1194 // | master | for | + |
1195 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001196 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001197 // | master | simd | * |
1198 // | master | sections | + |
1199 // | master | section | + |
1200 // | master | single | + |
1201 // | master | parallel for | * |
1202 // | master |parallel sections| * |
1203 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001204 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001205 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001206 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001207 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001208 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001209 // | master | atomic | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001210 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001211 // | critical | parallel | * |
1212 // | critical | for | + |
1213 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001214 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001215 // | critical | simd | * |
1216 // | critical | sections | + |
1217 // | critical | section | + |
1218 // | critical | single | + |
1219 // | critical | parallel for | * |
1220 // | critical |parallel sections| * |
1221 // | critical | task | * |
1222 // | critical | taskyield | * |
1223 // | critical | barrier | + |
1224 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001225 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001226 // | critical | atomic | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001227 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001228 // | simd | parallel | |
1229 // | simd | for | |
Alexander Musman80c22892014-07-17 08:54:58 +00001230 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001231 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001232 // | simd | simd | |
1233 // | simd | sections | |
1234 // | simd | section | |
1235 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001236 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001237 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001238 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001239 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001240 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001241 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001242 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001243 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001244 // | simd | atomic | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001245 // +------------------+-----------------+------------------------------------+
1246 // | sections | parallel | * |
1247 // | sections | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001248 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001249 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001250 // | sections | simd | * |
1251 // | sections | sections | + |
1252 // | sections | section | * |
1253 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001254 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001255 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001256 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001257 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001258 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001259 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001260 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001261 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001262 // | sections | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001263 // +------------------+-----------------+------------------------------------+
1264 // | section | parallel | * |
1265 // | section | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001266 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001267 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001268 // | section | simd | * |
1269 // | section | sections | + |
1270 // | section | section | + |
1271 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001272 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001273 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001274 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001275 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001276 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001277 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001278 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001279 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001280 // | section | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001281 // +------------------+-----------------+------------------------------------+
1282 // | single | parallel | * |
1283 // | single | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001284 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001285 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001286 // | single | simd | * |
1287 // | single | sections | + |
1288 // | single | section | + |
1289 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001290 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001291 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001292 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001293 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001294 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001295 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001296 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001297 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001298 // | single | atomic | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001299 // +------------------+-----------------+------------------------------------+
1300 // | parallel for | parallel | * |
1301 // | parallel for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001302 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001303 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001304 // | parallel for | simd | * |
1305 // | parallel for | sections | + |
1306 // | parallel for | section | + |
1307 // | parallel for | single | + |
1308 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001309 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001310 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001311 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001312 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001313 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001314 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001315 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001316 // | parallel for | atomic | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001317 // +------------------+-----------------+------------------------------------+
1318 // | parallel sections| parallel | * |
1319 // | parallel sections| for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001320 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001321 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001322 // | parallel sections| simd | * |
1323 // | parallel sections| sections | + |
1324 // | parallel sections| section | * |
1325 // | parallel sections| single | + |
1326 // | parallel sections| parallel for | * |
1327 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001328 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001329 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001330 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001331 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001332 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001333 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001334 // | parallel sections| atomic | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001335 // +------------------+-----------------+------------------------------------+
1336 // | task | parallel | * |
1337 // | task | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001338 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001339 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001340 // | task | simd | * |
1341 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001342 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001343 // | task | single | + |
1344 // | task | parallel for | * |
1345 // | task |parallel sections| * |
1346 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001347 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001348 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001349 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001350 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001351 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001352 // | task | atomic | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001353 // +------------------+-----------------+------------------------------------+
1354 // | ordered | parallel | * |
1355 // | ordered | for | + |
1356 // | ordered | master | * |
1357 // | ordered | critical | * |
1358 // | ordered | simd | * |
1359 // | ordered | sections | + |
1360 // | ordered | section | + |
1361 // | ordered | single | + |
1362 // | ordered | parallel for | * |
1363 // | ordered |parallel sections| * |
1364 // | ordered | task | * |
1365 // | ordered | taskyield | * |
1366 // | ordered | barrier | + |
1367 // | ordered | taskwait | * |
1368 // | ordered | flush | * |
1369 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001370 // | ordered | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001371 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001372 if (Stack->getCurScope()) {
1373 auto ParentRegion = Stack->getParentDirective();
1374 bool NestingProhibited = false;
1375 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001376 enum {
1377 NoRecommend,
1378 ShouldBeInParallelRegion,
1379 ShouldBeInOrderedRegion
1380 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001381 if (isOpenMPSimdDirective(ParentRegion)) {
1382 // OpenMP [2.16, Nesting of Regions]
1383 // OpenMP constructs may not be nested inside a simd region.
1384 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1385 return true;
1386 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001387 if (ParentRegion == OMPD_atomic) {
1388 // OpenMP [2.16, Nesting of Regions]
1389 // OpenMP constructs may not be nested inside an atomic region.
1390 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1391 return true;
1392 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001393 if (CurrentRegion == OMPD_section) {
1394 // OpenMP [2.7.2, sections Construct, Restrictions]
1395 // Orphaned section directives are prohibited. That is, the section
1396 // directives must appear within the sections construct and must not be
1397 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001398 if (ParentRegion != OMPD_sections &&
1399 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001400 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1401 << (ParentRegion != OMPD_unknown)
1402 << getOpenMPDirectiveName(ParentRegion);
1403 return true;
1404 }
1405 return false;
1406 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001407 // Allow some constructs to be orphaned (they could be used in functions,
1408 // called from OpenMP regions with the required preconditions).
1409 if (ParentRegion == OMPD_unknown)
1410 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001411 if (CurrentRegion == OMPD_master) {
1412 // OpenMP [2.16, Nesting of Regions]
1413 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001414 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001415 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1416 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001417 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1418 // OpenMP [2.16, Nesting of Regions]
1419 // A critical region may not be nested (closely or otherwise) inside a
1420 // critical region with the same name. Note that this restriction is not
1421 // sufficient to prevent deadlock.
1422 SourceLocation PreviousCriticalLoc;
1423 bool DeadLock =
1424 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1425 OpenMPDirectiveKind K,
1426 const DeclarationNameInfo &DNI,
1427 SourceLocation Loc)
1428 ->bool {
1429 if (K == OMPD_critical &&
1430 DNI.getName() == CurrentName.getName()) {
1431 PreviousCriticalLoc = Loc;
1432 return true;
1433 } else
1434 return false;
1435 },
1436 false /* skip top directive */);
1437 if (DeadLock) {
1438 SemaRef.Diag(StartLoc,
1439 diag::err_omp_prohibited_region_critical_same_name)
1440 << CurrentName.getName();
1441 if (PreviousCriticalLoc.isValid())
1442 SemaRef.Diag(PreviousCriticalLoc,
1443 diag::note_omp_previous_critical_region);
1444 return true;
1445 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001446 } else if (CurrentRegion == OMPD_barrier) {
1447 // OpenMP [2.16, Nesting of Regions]
1448 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001449 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001450 NestingProhibited =
1451 isOpenMPWorksharingDirective(ParentRegion) ||
1452 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1453 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001454 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1455 !isOpenMPParallelDirective(CurrentRegion) &&
1456 !isOpenMPSimdDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001457 // OpenMP [2.16, Nesting of Regions]
1458 // A worksharing region may not be closely nested inside a worksharing,
1459 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001460 NestingProhibited =
1461 (isOpenMPWorksharingDirective(ParentRegion) &&
1462 !isOpenMPSimdDirective(ParentRegion)) ||
1463 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1464 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1465 Recommend = ShouldBeInParallelRegion;
1466 } else if (CurrentRegion == OMPD_ordered) {
1467 // OpenMP [2.16, Nesting of Regions]
1468 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001469 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001470 // An ordered region must be closely nested inside a loop region (or
1471 // parallel loop region) with an ordered clause.
1472 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001473 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001474 !Stack->isParentOrderedRegion();
1475 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001476 }
1477 if (NestingProhibited) {
1478 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001479 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1480 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001481 return true;
1482 }
1483 }
1484 return false;
1485}
1486
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001487StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001488 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001489 ArrayRef<OMPClause *> Clauses,
1490 Stmt *AStmt,
1491 SourceLocation StartLoc,
1492 SourceLocation EndLoc) {
1493 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001494 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001495 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001496
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001497 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001498 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001499 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001500 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001501 if (AStmt) {
1502 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1503
1504 // Check default data sharing attributes for referenced variables.
1505 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1506 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1507 if (DSAChecker.isErrorFound())
1508 return StmtError();
1509 // Generate list of implicitly defined firstprivate variables.
1510 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001511
1512 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1513 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1514 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1515 SourceLocation(), SourceLocation())) {
1516 ClausesWithImplicit.push_back(Implicit);
1517 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1518 DSAChecker.getImplicitFirstprivate().size();
1519 } else
1520 ErrorFound = true;
1521 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001522 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001523
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001524 switch (Kind) {
1525 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001526 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1527 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001528 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001529 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001530 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1531 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001532 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001533 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001534 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1535 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001536 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001537 case OMPD_sections:
1538 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1539 EndLoc);
1540 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001541 case OMPD_section:
1542 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001543 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001544 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1545 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001546 case OMPD_single:
1547 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1548 EndLoc);
1549 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001550 case OMPD_master:
1551 assert(ClausesWithImplicit.empty() &&
1552 "No clauses are allowed for 'omp master' directive");
1553 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1554 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001555 case OMPD_critical:
1556 assert(ClausesWithImplicit.empty() &&
1557 "No clauses are allowed for 'omp critical' directive");
1558 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1559 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001560 case OMPD_parallel_for:
1561 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1562 EndLoc, VarsWithInheritedDSA);
1563 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001564 case OMPD_parallel_sections:
1565 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1566 StartLoc, EndLoc);
1567 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001568 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001569 Res =
1570 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1571 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001572 case OMPD_taskyield:
1573 assert(ClausesWithImplicit.empty() &&
1574 "No clauses are allowed for 'omp taskyield' directive");
1575 assert(AStmt == nullptr &&
1576 "No associated statement allowed for 'omp taskyield' directive");
1577 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1578 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001579 case OMPD_barrier:
1580 assert(ClausesWithImplicit.empty() &&
1581 "No clauses are allowed for 'omp barrier' directive");
1582 assert(AStmt == nullptr &&
1583 "No associated statement allowed for 'omp barrier' directive");
1584 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1585 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001586 case OMPD_taskwait:
1587 assert(ClausesWithImplicit.empty() &&
1588 "No clauses are allowed for 'omp taskwait' directive");
1589 assert(AStmt == nullptr &&
1590 "No associated statement allowed for 'omp taskwait' directive");
1591 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1592 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001593 case OMPD_flush:
1594 assert(AStmt == nullptr &&
1595 "No associated statement allowed for 'omp flush' directive");
1596 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1597 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001598 case OMPD_ordered:
1599 assert(ClausesWithImplicit.empty() &&
1600 "No clauses are allowed for 'omp ordered' directive");
1601 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1602 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001603 case OMPD_atomic:
1604 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1605 EndLoc);
1606 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001607 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001608 llvm_unreachable("OpenMP Directive is not allowed");
1609 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001610 llvm_unreachable("Unknown OpenMP directive");
1611 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001612
Alexey Bataev4acb8592014-07-07 13:01:15 +00001613 for (auto P : VarsWithInheritedDSA) {
1614 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1615 << P.first << P.second->getSourceRange();
1616 }
1617 if (!VarsWithInheritedDSA.empty())
1618 return StmtError();
1619
Alexey Bataeved09d242014-05-28 05:53:51 +00001620 if (ErrorFound)
1621 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001622 return Res;
1623}
1624
1625StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1626 Stmt *AStmt,
1627 SourceLocation StartLoc,
1628 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001629 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1630 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1631 // 1.2.2 OpenMP Language Terminology
1632 // Structured block - An executable statement with a single entry at the
1633 // top and a single exit at the bottom.
1634 // The point of exit cannot be a branch out of the structured block.
1635 // longjmp() and throw() must not violate the entry/exit criteria.
1636 CS->getCapturedDecl()->setNothrow();
1637
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001638 getCurFunction()->setHasBranchProtectedScope();
1639
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001640 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1641 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001642}
1643
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001644namespace {
1645/// \brief Helper class for checking canonical form of the OpenMP loops and
1646/// extracting iteration space of each loop in the loop nest, that will be used
1647/// for IR generation.
1648class OpenMPIterationSpaceChecker {
1649 /// \brief Reference to Sema.
1650 Sema &SemaRef;
1651 /// \brief A location for diagnostics (when there is no some better location).
1652 SourceLocation DefaultLoc;
1653 /// \brief A location for diagnostics (when increment is not compatible).
1654 SourceLocation ConditionLoc;
1655 /// \brief A source location for referring to condition later.
1656 SourceRange ConditionSrcRange;
1657 /// \brief Loop variable.
1658 VarDecl *Var;
1659 /// \brief Lower bound (initializer for the var).
1660 Expr *LB;
1661 /// \brief Upper bound.
1662 Expr *UB;
1663 /// \brief Loop step (increment).
1664 Expr *Step;
1665 /// \brief This flag is true when condition is one of:
1666 /// Var < UB
1667 /// Var <= UB
1668 /// UB > Var
1669 /// UB >= Var
1670 bool TestIsLessOp;
1671 /// \brief This flag is true when condition is strict ( < or > ).
1672 bool TestIsStrictOp;
1673 /// \brief This flag is true when step is subtracted on each iteration.
1674 bool SubtractStep;
1675
1676public:
1677 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1678 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1679 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1680 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1681 SubtractStep(false) {}
1682 /// \brief Check init-expr for canonical loop form and save loop counter
1683 /// variable - #Var and its initialization value - #LB.
1684 bool CheckInit(Stmt *S);
1685 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1686 /// for less/greater and for strict/non-strict comparison.
1687 bool CheckCond(Expr *S);
1688 /// \brief Check incr-expr for canonical loop form and return true if it
1689 /// does not conform, otherwise save loop step (#Step).
1690 bool CheckInc(Expr *S);
1691 /// \brief Return the loop counter variable.
1692 VarDecl *GetLoopVar() const { return Var; }
1693 /// \brief Return true if any expression is dependent.
1694 bool Dependent() const;
1695
1696private:
1697 /// \brief Check the right-hand side of an assignment in the increment
1698 /// expression.
1699 bool CheckIncRHS(Expr *RHS);
1700 /// \brief Helper to set loop counter variable and its initializer.
1701 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1702 /// \brief Helper to set upper bound.
1703 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1704 const SourceLocation &SL);
1705 /// \brief Helper to set loop increment.
1706 bool SetStep(Expr *NewStep, bool Subtract);
1707};
1708
1709bool OpenMPIterationSpaceChecker::Dependent() const {
1710 if (!Var) {
1711 assert(!LB && !UB && !Step);
1712 return false;
1713 }
1714 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1715 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1716}
1717
1718bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1719 // State consistency checking to ensure correct usage.
1720 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1721 !TestIsLessOp && !TestIsStrictOp);
1722 if (!NewVar || !NewLB)
1723 return true;
1724 Var = NewVar;
1725 LB = NewLB;
1726 return false;
1727}
1728
1729bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1730 const SourceRange &SR,
1731 const SourceLocation &SL) {
1732 // State consistency checking to ensure correct usage.
1733 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1734 !TestIsLessOp && !TestIsStrictOp);
1735 if (!NewUB)
1736 return true;
1737 UB = NewUB;
1738 TestIsLessOp = LessOp;
1739 TestIsStrictOp = StrictOp;
1740 ConditionSrcRange = SR;
1741 ConditionLoc = SL;
1742 return false;
1743}
1744
1745bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1746 // State consistency checking to ensure correct usage.
1747 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1748 if (!NewStep)
1749 return true;
1750 if (!NewStep->isValueDependent()) {
1751 // Check that the step is integer expression.
1752 SourceLocation StepLoc = NewStep->getLocStart();
1753 ExprResult Val =
1754 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1755 if (Val.isInvalid())
1756 return true;
1757 NewStep = Val.get();
1758
1759 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1760 // If test-expr is of form var relational-op b and relational-op is < or
1761 // <= then incr-expr must cause var to increase on each iteration of the
1762 // loop. If test-expr is of form var relational-op b and relational-op is
1763 // > or >= then incr-expr must cause var to decrease on each iteration of
1764 // the loop.
1765 // If test-expr is of form b relational-op var and relational-op is < or
1766 // <= then incr-expr must cause var to decrease on each iteration of the
1767 // loop. If test-expr is of form b relational-op var and relational-op is
1768 // > or >= then incr-expr must cause var to increase on each iteration of
1769 // the loop.
1770 llvm::APSInt Result;
1771 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1772 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1773 bool IsConstNeg =
1774 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1775 bool IsConstZero = IsConstant && !Result.getBoolValue();
1776 if (UB && (IsConstZero ||
1777 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1778 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1779 SemaRef.Diag(NewStep->getExprLoc(),
1780 diag::err_omp_loop_incr_not_compatible)
1781 << Var << TestIsLessOp << NewStep->getSourceRange();
1782 SemaRef.Diag(ConditionLoc,
1783 diag::note_omp_loop_cond_requres_compatible_incr)
1784 << TestIsLessOp << ConditionSrcRange;
1785 return true;
1786 }
1787 }
1788
1789 Step = NewStep;
1790 SubtractStep = Subtract;
1791 return false;
1792}
1793
1794bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1795 // Check init-expr for canonical loop form and save loop counter
1796 // variable - #Var and its initialization value - #LB.
1797 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1798 // var = lb
1799 // integer-type var = lb
1800 // random-access-iterator-type var = lb
1801 // pointer-type var = lb
1802 //
1803 if (!S) {
1804 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1805 return true;
1806 }
1807 if (Expr *E = dyn_cast<Expr>(S))
1808 S = E->IgnoreParens();
1809 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1810 if (BO->getOpcode() == BO_Assign)
1811 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1812 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1813 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1814 if (DS->isSingleDecl()) {
1815 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1816 if (Var->hasInit()) {
1817 // Accept non-canonical init form here but emit ext. warning.
1818 if (Var->getInitStyle() != VarDecl::CInit)
1819 SemaRef.Diag(S->getLocStart(),
1820 diag::ext_omp_loop_not_canonical_init)
1821 << S->getSourceRange();
1822 return SetVarAndLB(Var, Var->getInit());
1823 }
1824 }
1825 }
1826 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1827 if (CE->getOperator() == OO_Equal)
1828 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1829 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1830
1831 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1832 << S->getSourceRange();
1833 return true;
1834}
1835
Alexey Bataev23b69422014-06-18 07:08:49 +00001836/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001837/// variable (which may be the loop variable) if possible.
1838static const VarDecl *GetInitVarDecl(const Expr *E) {
1839 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001840 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001841 E = E->IgnoreParenImpCasts();
1842 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1843 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1844 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1845 CE->getArg(0) != nullptr)
1846 E = CE->getArg(0)->IgnoreParenImpCasts();
1847 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1848 if (!DRE)
1849 return nullptr;
1850 return dyn_cast<VarDecl>(DRE->getDecl());
1851}
1852
1853bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1854 // Check test-expr for canonical form, save upper-bound UB, flags for
1855 // less/greater and for strict/non-strict comparison.
1856 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1857 // var relational-op b
1858 // b relational-op var
1859 //
1860 if (!S) {
1861 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1862 return true;
1863 }
1864 S = S->IgnoreParenImpCasts();
1865 SourceLocation CondLoc = S->getLocStart();
1866 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1867 if (BO->isRelationalOp()) {
1868 if (GetInitVarDecl(BO->getLHS()) == Var)
1869 return SetUB(BO->getRHS(),
1870 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1871 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1872 BO->getSourceRange(), BO->getOperatorLoc());
1873 if (GetInitVarDecl(BO->getRHS()) == Var)
1874 return SetUB(BO->getLHS(),
1875 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1876 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1877 BO->getSourceRange(), BO->getOperatorLoc());
1878 }
1879 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1880 if (CE->getNumArgs() == 2) {
1881 auto Op = CE->getOperator();
1882 switch (Op) {
1883 case OO_Greater:
1884 case OO_GreaterEqual:
1885 case OO_Less:
1886 case OO_LessEqual:
1887 if (GetInitVarDecl(CE->getArg(0)) == Var)
1888 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1889 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1890 CE->getOperatorLoc());
1891 if (GetInitVarDecl(CE->getArg(1)) == Var)
1892 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1893 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1894 CE->getOperatorLoc());
1895 break;
1896 default:
1897 break;
1898 }
1899 }
1900 }
1901 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1902 << S->getSourceRange() << Var;
1903 return true;
1904}
1905
1906bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1907 // RHS of canonical loop form increment can be:
1908 // var + incr
1909 // incr + var
1910 // var - incr
1911 //
1912 RHS = RHS->IgnoreParenImpCasts();
1913 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1914 if (BO->isAdditiveOp()) {
1915 bool IsAdd = BO->getOpcode() == BO_Add;
1916 if (GetInitVarDecl(BO->getLHS()) == Var)
1917 return SetStep(BO->getRHS(), !IsAdd);
1918 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1919 return SetStep(BO->getLHS(), false);
1920 }
1921 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1922 bool IsAdd = CE->getOperator() == OO_Plus;
1923 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1924 if (GetInitVarDecl(CE->getArg(0)) == Var)
1925 return SetStep(CE->getArg(1), !IsAdd);
1926 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1927 return SetStep(CE->getArg(0), false);
1928 }
1929 }
1930 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1931 << RHS->getSourceRange() << Var;
1932 return true;
1933}
1934
1935bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1936 // Check incr-expr for canonical loop form and return true if it
1937 // does not conform.
1938 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1939 // ++var
1940 // var++
1941 // --var
1942 // var--
1943 // var += incr
1944 // var -= incr
1945 // var = var + incr
1946 // var = incr + var
1947 // var = var - incr
1948 //
1949 if (!S) {
1950 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1951 return true;
1952 }
1953 S = S->IgnoreParens();
1954 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1955 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1956 return SetStep(
1957 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1958 (UO->isDecrementOp() ? -1 : 1)).get(),
1959 false);
1960 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1961 switch (BO->getOpcode()) {
1962 case BO_AddAssign:
1963 case BO_SubAssign:
1964 if (GetInitVarDecl(BO->getLHS()) == Var)
1965 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1966 break;
1967 case BO_Assign:
1968 if (GetInitVarDecl(BO->getLHS()) == Var)
1969 return CheckIncRHS(BO->getRHS());
1970 break;
1971 default:
1972 break;
1973 }
1974 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1975 switch (CE->getOperator()) {
1976 case OO_PlusPlus:
1977 case OO_MinusMinus:
1978 if (GetInitVarDecl(CE->getArg(0)) == Var)
1979 return SetStep(
1980 SemaRef.ActOnIntegerConstant(
1981 CE->getLocStart(),
1982 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1983 false);
1984 break;
1985 case OO_PlusEqual:
1986 case OO_MinusEqual:
1987 if (GetInitVarDecl(CE->getArg(0)) == Var)
1988 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1989 break;
1990 case OO_Equal:
1991 if (GetInitVarDecl(CE->getArg(0)) == Var)
1992 return CheckIncRHS(CE->getArg(1));
1993 break;
1994 default:
1995 break;
1996 }
1997 }
1998 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1999 << S->getSourceRange() << Var;
2000 return true;
2001}
Alexey Bataev23b69422014-06-18 07:08:49 +00002002} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002003
2004/// \brief Called on a for stmt to check and extract its iteration space
2005/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002006static bool CheckOpenMPIterationSpace(
2007 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2008 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2009 Expr *NestedLoopCountExpr,
2010 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002011 // OpenMP [2.6, Canonical Loop Form]
2012 // for (init-expr; test-expr; incr-expr) structured-block
2013 auto For = dyn_cast_or_null<ForStmt>(S);
2014 if (!For) {
2015 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002016 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2017 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2018 << CurrentNestedLoopCount;
2019 if (NestedLoopCount > 1)
2020 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2021 diag::note_omp_collapse_expr)
2022 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002023 return true;
2024 }
2025 assert(For->getBody());
2026
2027 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2028
2029 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002030 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002031 if (ISC.CheckInit(Init)) {
2032 return true;
2033 }
2034
2035 bool HasErrors = false;
2036
2037 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002038 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002039
2040 // OpenMP [2.6, Canonical Loop Form]
2041 // Var is one of the following:
2042 // A variable of signed or unsigned integer type.
2043 // For C++, a variable of a random access iterator type.
2044 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002045 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002046 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2047 !VarType->isPointerType() &&
2048 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2049 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2050 << SemaRef.getLangOpts().CPlusPlus;
2051 HasErrors = true;
2052 }
2053
Alexey Bataev4acb8592014-07-07 13:01:15 +00002054 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2055 // Construct
2056 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2057 // parallel for construct is (are) private.
2058 // The loop iteration variable in the associated for-loop of a simd construct
2059 // with just one associated for-loop is linear with a constant-linear-step
2060 // that is the increment of the associated for-loop.
2061 // Exclude loop var from the list of variables with implicitly defined data
2062 // sharing attributes.
2063 while (VarsWithImplicitDSA.count(Var) > 0)
2064 VarsWithImplicitDSA.erase(Var);
2065
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002066 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2067 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002068 // The loop iteration variable in the associated for-loop of a simd construct
2069 // with just one associated for-loop may be listed in a linear clause with a
2070 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002071 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2072 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002073 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002074 auto PredeterminedCKind =
2075 isOpenMPSimdDirective(DKind)
2076 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2077 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002078 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002079 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00002080 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
2081 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002082 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002083 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002084 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2085 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002086 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002087 HasErrors = true;
2088 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002089 // Make the loop iteration variable private (for worksharing constructs),
2090 // linear (for simd directives with the only one associated loop) or
2091 // lastprivate (for simd directives with several collapsed loops).
2092 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002093 }
2094
Alexey Bataev7ff55242014-06-19 09:13:45 +00002095 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002096
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002097 // Check test-expr.
2098 HasErrors |= ISC.CheckCond(For->getCond());
2099
2100 // Check incr-expr.
2101 HasErrors |= ISC.CheckInc(For->getInc());
2102
2103 if (ISC.Dependent())
2104 return HasErrors;
2105
2106 // FIXME: Build loop's iteration space representation.
2107 return HasErrors;
2108}
2109
2110/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
2111/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
2112/// to get the first for loop.
2113static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
2114 if (IgnoreCaptured)
2115 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
2116 S = CapS->getCapturedStmt();
2117 // OpenMP [2.8.1, simd construct, Restrictions]
2118 // All loops associated with the construct must be perfectly nested; that is,
2119 // there must be no intervening code nor any OpenMP directive between any two
2120 // loops.
2121 while (true) {
2122 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
2123 S = AS->getSubStmt();
2124 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
2125 if (CS->size() != 1)
2126 break;
2127 S = CS->body_back();
2128 } else
2129 break;
2130 }
2131 return S;
2132}
2133
2134/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002135/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2136/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002137static unsigned
2138CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2139 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2140 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002141 unsigned NestedLoopCount = 1;
2142 if (NestedLoopCountExpr) {
2143 // Found 'collapse' clause - calculate collapse number.
2144 llvm::APSInt Result;
2145 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2146 NestedLoopCount = Result.getLimitedValue();
2147 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002148 // This is helper routine for loop directives (e.g., 'for', 'simd',
2149 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002150 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
2151 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002152 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002153 NestedLoopCount, NestedLoopCountExpr,
2154 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002155 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002156 // Move on to the next nested for loop, or to the loop body.
2157 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
2158 }
2159
2160 // FIXME: Build resulting iteration space for IR generation (collapsing
2161 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002162 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002163}
2164
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002165static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002166 auto CollapseFilter = [](const OMPClause *C) -> bool {
2167 return C->getClauseKind() == OMPC_collapse;
2168 };
2169 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2170 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002171 if (I)
2172 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2173 return nullptr;
2174}
2175
Alexey Bataev4acb8592014-07-07 13:01:15 +00002176StmtResult Sema::ActOnOpenMPSimdDirective(
2177 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2178 SourceLocation EndLoc,
2179 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002180 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002181 unsigned NestedLoopCount =
2182 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
2183 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002184 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002185 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002186
2187 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002188 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2189 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002190}
2191
Alexey Bataev4acb8592014-07-07 13:01:15 +00002192StmtResult Sema::ActOnOpenMPForDirective(
2193 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2194 SourceLocation EndLoc,
2195 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002196 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002197 unsigned NestedLoopCount =
2198 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
2199 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002200 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002201 return StmtError();
2202
2203 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002204 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2205 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002206}
2207
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002208StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2209 Stmt *AStmt,
2210 SourceLocation StartLoc,
2211 SourceLocation EndLoc) {
2212 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2213 auto BaseStmt = AStmt;
2214 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2215 BaseStmt = CS->getCapturedStmt();
2216 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2217 auto S = C->children();
2218 if (!S)
2219 return StmtError();
2220 // All associated statements must be '#pragma omp section' except for
2221 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002222 for (++S; S; ++S) {
2223 auto SectionStmt = *S;
2224 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2225 if (SectionStmt)
2226 Diag(SectionStmt->getLocStart(),
2227 diag::err_omp_sections_substmt_not_section);
2228 return StmtError();
2229 }
2230 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002231 } else {
2232 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2233 return StmtError();
2234 }
2235
2236 getCurFunction()->setHasBranchProtectedScope();
2237
2238 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2239 AStmt);
2240}
2241
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002242StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2243 SourceLocation StartLoc,
2244 SourceLocation EndLoc) {
2245 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2246
2247 getCurFunction()->setHasBranchProtectedScope();
2248
2249 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
2250}
2251
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002252StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
2253 Stmt *AStmt,
2254 SourceLocation StartLoc,
2255 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00002256 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2257
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002258 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00002259
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002260 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2261}
2262
Alexander Musman80c22892014-07-17 08:54:58 +00002263StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
2264 SourceLocation StartLoc,
2265 SourceLocation EndLoc) {
2266 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2267
2268 getCurFunction()->setHasBranchProtectedScope();
2269
2270 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
2271}
2272
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002273StmtResult
2274Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
2275 Stmt *AStmt, SourceLocation StartLoc,
2276 SourceLocation EndLoc) {
2277 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2278
2279 getCurFunction()->setHasBranchProtectedScope();
2280
2281 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
2282 AStmt);
2283}
2284
Alexey Bataev4acb8592014-07-07 13:01:15 +00002285StmtResult Sema::ActOnOpenMPParallelForDirective(
2286 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2287 SourceLocation EndLoc,
2288 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2289 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2290 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2291 // 1.2.2 OpenMP Language Terminology
2292 // Structured block - An executable statement with a single entry at the
2293 // top and a single exit at the bottom.
2294 // The point of exit cannot be a branch out of the structured block.
2295 // longjmp() and throw() must not violate the entry/exit criteria.
2296 CS->getCapturedDecl()->setNothrow();
2297
2298 // In presence of clause 'collapse', it will define the nested loops number.
2299 unsigned NestedLoopCount =
2300 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
2301 *this, *DSAStack, VarsWithImplicitDSA);
2302 if (NestedLoopCount == 0)
2303 return StmtError();
2304
2305 getCurFunction()->setHasBranchProtectedScope();
2306 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
2307 NestedLoopCount, Clauses, AStmt);
2308}
2309
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002310StmtResult
2311Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2312 Stmt *AStmt, SourceLocation StartLoc,
2313 SourceLocation EndLoc) {
2314 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2315 auto BaseStmt = AStmt;
2316 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2317 BaseStmt = CS->getCapturedStmt();
2318 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2319 auto S = C->children();
2320 if (!S)
2321 return StmtError();
2322 // All associated statements must be '#pragma omp section' except for
2323 // the first one.
2324 for (++S; S; ++S) {
2325 auto SectionStmt = *S;
2326 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2327 if (SectionStmt)
2328 Diag(SectionStmt->getLocStart(),
2329 diag::err_omp_parallel_sections_substmt_not_section);
2330 return StmtError();
2331 }
2332 }
2333 } else {
2334 Diag(AStmt->getLocStart(),
2335 diag::err_omp_parallel_sections_not_compound_stmt);
2336 return StmtError();
2337 }
2338
2339 getCurFunction()->setHasBranchProtectedScope();
2340
2341 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2342 Clauses, AStmt);
2343}
2344
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002345StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2346 Stmt *AStmt, SourceLocation StartLoc,
2347 SourceLocation EndLoc) {
2348 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2349 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2350 // 1.2.2 OpenMP Language Terminology
2351 // Structured block - An executable statement with a single entry at the
2352 // top and a single exit at the bottom.
2353 // The point of exit cannot be a branch out of the structured block.
2354 // longjmp() and throw() must not violate the entry/exit criteria.
2355 CS->getCapturedDecl()->setNothrow();
2356
2357 getCurFunction()->setHasBranchProtectedScope();
2358
2359 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2360}
2361
Alexey Bataev68446b72014-07-18 07:47:19 +00002362StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
2363 SourceLocation EndLoc) {
2364 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
2365}
2366
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002367StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
2368 SourceLocation EndLoc) {
2369 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
2370}
2371
Alexey Bataev2df347a2014-07-18 10:17:07 +00002372StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
2373 SourceLocation EndLoc) {
2374 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
2375}
2376
Alexey Bataev6125da92014-07-21 11:26:11 +00002377StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
2378 SourceLocation StartLoc,
2379 SourceLocation EndLoc) {
2380 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
2381 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
2382}
2383
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002384StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
2385 SourceLocation StartLoc,
2386 SourceLocation EndLoc) {
2387 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2388
2389 getCurFunction()->setHasBranchProtectedScope();
2390
2391 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
2392}
2393
Alexey Bataev0162e452014-07-22 10:10:35 +00002394StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
2395 Stmt *AStmt,
2396 SourceLocation StartLoc,
2397 SourceLocation EndLoc) {
2398 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002399 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00002400 // 1.2.2 OpenMP Language Terminology
2401 // Structured block - An executable statement with a single entry at the
2402 // top and a single exit at the bottom.
2403 // The point of exit cannot be a branch out of the structured block.
2404 // longjmp() and throw() must not violate the entry/exit criteria.
2405 // TODO further analysis of associated statements and clauses.
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002406 for (auto *C : Clauses) {
2407 if (C->getClauseKind() == OMPC_read) {
2408 if (!isa<Expr>(CS->getCapturedStmt())) {
2409 Diag(CS->getCapturedStmt()->getLocStart(),
2410 diag::err_omp_atomic_read_not_expression_statement);
2411 return StmtError();
2412 }
2413 }
2414 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002415
2416 getCurFunction()->setHasBranchProtectedScope();
2417
2418 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2419}
2420
Alexey Bataeved09d242014-05-28 05:53:51 +00002421OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002422 SourceLocation StartLoc,
2423 SourceLocation LParenLoc,
2424 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002425 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002426 switch (Kind) {
2427 case OMPC_if:
2428 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2429 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002430 case OMPC_final:
2431 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2432 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002433 case OMPC_num_threads:
2434 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2435 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002436 case OMPC_safelen:
2437 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2438 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002439 case OMPC_collapse:
2440 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2441 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002442 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002443 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002444 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002445 case OMPC_private:
2446 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002447 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002448 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002449 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002450 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002451 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002452 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002453 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002454 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002455 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002456 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002457 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002458 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002459 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002460 case OMPC_read:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002461 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002462 llvm_unreachable("Clause is not allowed.");
2463 }
2464 return Res;
2465}
2466
Alexey Bataeved09d242014-05-28 05:53:51 +00002467OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002468 SourceLocation LParenLoc,
2469 SourceLocation EndLoc) {
2470 Expr *ValExpr = Condition;
2471 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2472 !Condition->isInstantiationDependent() &&
2473 !Condition->containsUnexpandedParameterPack()) {
2474 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002475 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002476 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002477 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002478
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002479 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002480 }
2481
2482 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2483}
2484
Alexey Bataev3778b602014-07-17 07:32:53 +00002485OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2486 SourceLocation StartLoc,
2487 SourceLocation LParenLoc,
2488 SourceLocation EndLoc) {
2489 Expr *ValExpr = Condition;
2490 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2491 !Condition->isInstantiationDependent() &&
2492 !Condition->containsUnexpandedParameterPack()) {
2493 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2494 Condition->getExprLoc(), Condition);
2495 if (Val.isInvalid())
2496 return nullptr;
2497
2498 ValExpr = Val.get();
2499 }
2500
2501 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2502}
2503
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002504ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2505 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002506 if (!Op)
2507 return ExprError();
2508
2509 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2510 public:
2511 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002512 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002513 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2514 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002515 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2516 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002517 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2518 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002519 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2520 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002521 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2522 QualType T,
2523 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002524 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2525 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002526 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2527 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002528 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002529 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002530 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002531 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2532 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002533 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2534 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002535 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2536 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002537 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002538 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002539 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002540 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2541 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002542 llvm_unreachable("conversion functions are permitted");
2543 }
2544 } ConvertDiagnoser;
2545 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2546}
2547
2548OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2549 SourceLocation StartLoc,
2550 SourceLocation LParenLoc,
2551 SourceLocation EndLoc) {
2552 Expr *ValExpr = NumThreads;
2553 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2554 !NumThreads->isInstantiationDependent() &&
2555 !NumThreads->containsUnexpandedParameterPack()) {
2556 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2557 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002558 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002559 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002560 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002561
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002562 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002563
2564 // OpenMP [2.5, Restrictions]
2565 // The num_threads expression must evaluate to a positive integer value.
2566 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002567 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2568 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002569 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2570 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002571 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002572 }
2573 }
2574
Alexey Bataeved09d242014-05-28 05:53:51 +00002575 return new (Context)
2576 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002577}
2578
Alexey Bataev62c87d22014-03-21 04:51:18 +00002579ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2580 OpenMPClauseKind CKind) {
2581 if (!E)
2582 return ExprError();
2583 if (E->isValueDependent() || E->isTypeDependent() ||
2584 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002585 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002586 llvm::APSInt Result;
2587 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2588 if (ICE.isInvalid())
2589 return ExprError();
2590 if (!Result.isStrictlyPositive()) {
2591 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2592 << getOpenMPClauseName(CKind) << E->getSourceRange();
2593 return ExprError();
2594 }
2595 return ICE;
2596}
2597
2598OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2599 SourceLocation LParenLoc,
2600 SourceLocation EndLoc) {
2601 // OpenMP [2.8.1, simd construct, Description]
2602 // The parameter of the safelen clause must be a constant
2603 // positive integer expression.
2604 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2605 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002606 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002607 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002608 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002609}
2610
Alexander Musman64d33f12014-06-04 07:53:32 +00002611OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2612 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002613 SourceLocation LParenLoc,
2614 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002615 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002616 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002617 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002618 // The parameter of the collapse clause must be a constant
2619 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002620 ExprResult NumForLoopsResult =
2621 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2622 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002623 return nullptr;
2624 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002625 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002626}
2627
Alexey Bataeved09d242014-05-28 05:53:51 +00002628OMPClause *Sema::ActOnOpenMPSimpleClause(
2629 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2630 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002631 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002632 switch (Kind) {
2633 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002634 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002635 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2636 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002637 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002638 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002639 Res = ActOnOpenMPProcBindClause(
2640 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2641 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002642 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002643 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002644 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002645 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002646 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002647 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002648 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002649 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002650 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002651 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002652 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002653 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002654 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002655 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002656 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002657 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002658 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002659 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002660 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002661 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002662 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002663 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002664 case OMPC_read:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002665 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002666 llvm_unreachable("Clause is not allowed.");
2667 }
2668 return Res;
2669}
2670
2671OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2672 SourceLocation KindKwLoc,
2673 SourceLocation StartLoc,
2674 SourceLocation LParenLoc,
2675 SourceLocation EndLoc) {
2676 if (Kind == OMPC_DEFAULT_unknown) {
2677 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002678 static_assert(OMPC_DEFAULT_unknown > 0,
2679 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002680 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002681 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002682 Values += "'";
2683 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2684 Values += "'";
2685 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002686 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002687 Values += " or ";
2688 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002689 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002690 break;
2691 default:
2692 Values += Sep;
2693 break;
2694 }
2695 }
2696 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002697 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002698 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002699 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002700 switch (Kind) {
2701 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002702 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002703 break;
2704 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002705 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002706 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002707 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002708 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002709 break;
2710 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002711 return new (Context)
2712 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002713}
2714
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002715OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2716 SourceLocation KindKwLoc,
2717 SourceLocation StartLoc,
2718 SourceLocation LParenLoc,
2719 SourceLocation EndLoc) {
2720 if (Kind == OMPC_PROC_BIND_unknown) {
2721 std::string Values;
2722 std::string Sep(", ");
2723 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2724 Values += "'";
2725 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2726 Values += "'";
2727 switch (i) {
2728 case OMPC_PROC_BIND_unknown - 2:
2729 Values += " or ";
2730 break;
2731 case OMPC_PROC_BIND_unknown - 1:
2732 break;
2733 default:
2734 Values += Sep;
2735 break;
2736 }
2737 }
2738 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002739 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002740 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002741 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002742 return new (Context)
2743 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002744}
2745
Alexey Bataev56dafe82014-06-20 07:16:17 +00002746OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2747 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2748 SourceLocation StartLoc, SourceLocation LParenLoc,
2749 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2750 SourceLocation EndLoc) {
2751 OMPClause *Res = nullptr;
2752 switch (Kind) {
2753 case OMPC_schedule:
2754 Res = ActOnOpenMPScheduleClause(
2755 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2756 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2757 break;
2758 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002759 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002760 case OMPC_num_threads:
2761 case OMPC_safelen:
2762 case OMPC_collapse:
2763 case OMPC_default:
2764 case OMPC_proc_bind:
2765 case OMPC_private:
2766 case OMPC_firstprivate:
2767 case OMPC_lastprivate:
2768 case OMPC_shared:
2769 case OMPC_reduction:
2770 case OMPC_linear:
2771 case OMPC_aligned:
2772 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002773 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002774 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002775 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002776 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002777 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002778 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002779 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002780 case OMPC_read:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002781 case OMPC_unknown:
2782 llvm_unreachable("Clause is not allowed.");
2783 }
2784 return Res;
2785}
2786
2787OMPClause *Sema::ActOnOpenMPScheduleClause(
2788 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2789 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2790 SourceLocation EndLoc) {
2791 if (Kind == OMPC_SCHEDULE_unknown) {
2792 std::string Values;
2793 std::string Sep(", ");
2794 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2795 Values += "'";
2796 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2797 Values += "'";
2798 switch (i) {
2799 case OMPC_SCHEDULE_unknown - 2:
2800 Values += " or ";
2801 break;
2802 case OMPC_SCHEDULE_unknown - 1:
2803 break;
2804 default:
2805 Values += Sep;
2806 break;
2807 }
2808 }
2809 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2810 << Values << getOpenMPClauseName(OMPC_schedule);
2811 return nullptr;
2812 }
2813 Expr *ValExpr = ChunkSize;
2814 if (ChunkSize) {
2815 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2816 !ChunkSize->isInstantiationDependent() &&
2817 !ChunkSize->containsUnexpandedParameterPack()) {
2818 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2819 ExprResult Val =
2820 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2821 if (Val.isInvalid())
2822 return nullptr;
2823
2824 ValExpr = Val.get();
2825
2826 // OpenMP [2.7.1, Restrictions]
2827 // chunk_size must be a loop invariant integer expression with a positive
2828 // value.
2829 llvm::APSInt Result;
2830 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2831 Result.isSigned() && !Result.isStrictlyPositive()) {
2832 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2833 << "schedule" << ChunkSize->getSourceRange();
2834 return nullptr;
2835 }
2836 }
2837 }
2838
2839 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2840 EndLoc, Kind, ValExpr);
2841}
2842
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002843OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2844 SourceLocation StartLoc,
2845 SourceLocation EndLoc) {
2846 OMPClause *Res = nullptr;
2847 switch (Kind) {
2848 case OMPC_ordered:
2849 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2850 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002851 case OMPC_nowait:
2852 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2853 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002854 case OMPC_untied:
2855 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
2856 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002857 case OMPC_mergeable:
2858 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
2859 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002860 case OMPC_read:
2861 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
2862 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002863 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002864 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002865 case OMPC_num_threads:
2866 case OMPC_safelen:
2867 case OMPC_collapse:
2868 case OMPC_schedule:
2869 case OMPC_private:
2870 case OMPC_firstprivate:
2871 case OMPC_lastprivate:
2872 case OMPC_shared:
2873 case OMPC_reduction:
2874 case OMPC_linear:
2875 case OMPC_aligned:
2876 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002877 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002878 case OMPC_default:
2879 case OMPC_proc_bind:
2880 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002881 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002882 case OMPC_unknown:
2883 llvm_unreachable("Clause is not allowed.");
2884 }
2885 return Res;
2886}
2887
2888OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2889 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002890 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002891 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2892}
2893
Alexey Bataev236070f2014-06-20 11:19:47 +00002894OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2895 SourceLocation EndLoc) {
2896 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2897}
2898
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002899OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
2900 SourceLocation EndLoc) {
2901 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
2902}
2903
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002904OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
2905 SourceLocation EndLoc) {
2906 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
2907}
2908
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002909OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
2910 SourceLocation EndLoc) {
2911 DSAStack->setAtomicClauseLoc(StartLoc);
2912 return new (Context) OMPReadClause(StartLoc, EndLoc);
2913}
2914
Alexey Bataevc5e02582014-06-16 07:08:35 +00002915OMPClause *Sema::ActOnOpenMPVarListClause(
2916 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2917 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2918 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2919 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002920 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002921 switch (Kind) {
2922 case OMPC_private:
2923 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2924 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002925 case OMPC_firstprivate:
2926 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2927 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002928 case OMPC_lastprivate:
2929 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2930 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002931 case OMPC_shared:
2932 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2933 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002934 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002935 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2936 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002937 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002938 case OMPC_linear:
2939 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2940 ColonLoc, EndLoc);
2941 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002942 case OMPC_aligned:
2943 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2944 ColonLoc, EndLoc);
2945 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002946 case OMPC_copyin:
2947 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2948 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002949 case OMPC_copyprivate:
2950 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2951 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002952 case OMPC_flush:
2953 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
2954 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002955 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002956 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002957 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002958 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002959 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002960 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002961 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002962 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002963 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002964 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002965 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002966 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002967 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002968 case OMPC_read:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002969 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002970 llvm_unreachable("Clause is not allowed.");
2971 }
2972 return Res;
2973}
2974
2975OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2976 SourceLocation StartLoc,
2977 SourceLocation LParenLoc,
2978 SourceLocation EndLoc) {
2979 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002980 for (auto &RefExpr : VarList) {
2981 assert(RefExpr && "NULL expr in OpenMP private clause.");
2982 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002983 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002984 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002985 continue;
2986 }
2987
Alexey Bataeved09d242014-05-28 05:53:51 +00002988 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002989 // OpenMP [2.1, C/C++]
2990 // A list item is a variable name.
2991 // OpenMP [2.9.3.3, Restrictions, p.1]
2992 // A variable that is part of another variable (as an array or
2993 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002994 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002995 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002996 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002997 continue;
2998 }
2999 Decl *D = DE->getDecl();
3000 VarDecl *VD = cast<VarDecl>(D);
3001
3002 QualType Type = VD->getType();
3003 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3004 // It will be analyzed later.
3005 Vars.push_back(DE);
3006 continue;
3007 }
3008
3009 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3010 // A variable that appears in a private clause must not have an incomplete
3011 // type or a reference type.
3012 if (RequireCompleteType(ELoc, Type,
3013 diag::err_omp_private_incomplete_type)) {
3014 continue;
3015 }
3016 if (Type->isReferenceType()) {
3017 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003018 << getOpenMPClauseName(OMPC_private) << Type;
3019 bool IsDecl =
3020 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3021 Diag(VD->getLocation(),
3022 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3023 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003024 continue;
3025 }
3026
3027 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3028 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003029 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003030 // class type.
3031 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003032 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3033 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003034 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003035 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3036 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3037 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003038 // FIXME This code must be replaced by actual constructing/destructing of
3039 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003040 if (RD) {
3041 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3042 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003043 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003044 if (!CD ||
3045 CheckConstructorAccess(ELoc, CD,
3046 InitializedEntity::InitializeTemporary(Type),
3047 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003048 CD->isDeleted()) {
3049 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003050 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003051 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3052 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003053 Diag(VD->getLocation(),
3054 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3055 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003056 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3057 continue;
3058 }
3059 MarkFunctionReferenced(ELoc, CD);
3060 DiagnoseUseOfDecl(CD, ELoc);
3061
3062 CXXDestructorDecl *DD = RD->getDestructor();
3063 if (DD) {
3064 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3065 DD->isDeleted()) {
3066 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003067 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003068 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3069 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003070 Diag(VD->getLocation(),
3071 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3072 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003073 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3074 continue;
3075 }
3076 MarkFunctionReferenced(ELoc, DD);
3077 DiagnoseUseOfDecl(DD, ELoc);
3078 }
3079 }
3080
Alexey Bataev758e55e2013-09-06 18:03:48 +00003081 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3082 // in a Construct]
3083 // Variables with the predetermined data-sharing attributes may not be
3084 // listed in data-sharing attributes clauses, except for the cases
3085 // listed below. For these exceptions only, listing a predetermined
3086 // variable in a data-sharing attribute clause is allowed and overrides
3087 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003088 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003089 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003090 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3091 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003092 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003093 continue;
3094 }
3095
3096 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003097 Vars.push_back(DE);
3098 }
3099
Alexey Bataeved09d242014-05-28 05:53:51 +00003100 if (Vars.empty())
3101 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003102
3103 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3104}
3105
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003106OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
3107 SourceLocation StartLoc,
3108 SourceLocation LParenLoc,
3109 SourceLocation EndLoc) {
3110 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003111 bool IsImplicitClause =
3112 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
3113 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
3114
Alexey Bataeved09d242014-05-28 05:53:51 +00003115 for (auto &RefExpr : VarList) {
3116 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
3117 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003118 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003119 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003120 continue;
3121 }
3122
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003123 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
3124 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003125 // OpenMP [2.1, C/C++]
3126 // A list item is a variable name.
3127 // OpenMP [2.9.3.3, Restrictions, p.1]
3128 // A variable that is part of another variable (as an array or
3129 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003130 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003131 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003132 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003133 continue;
3134 }
3135 Decl *D = DE->getDecl();
3136 VarDecl *VD = cast<VarDecl>(D);
3137
3138 QualType Type = VD->getType();
3139 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3140 // It will be analyzed later.
3141 Vars.push_back(DE);
3142 continue;
3143 }
3144
3145 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3146 // A variable that appears in a private clause must not have an incomplete
3147 // type or a reference type.
3148 if (RequireCompleteType(ELoc, Type,
3149 diag::err_omp_firstprivate_incomplete_type)) {
3150 continue;
3151 }
3152 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003153 if (IsImplicitClause) {
3154 Diag(ImplicitClauseLoc,
3155 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
3156 << Type;
3157 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3158 } else {
3159 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3160 << getOpenMPClauseName(OMPC_firstprivate) << Type;
3161 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003162 bool IsDecl =
3163 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3164 Diag(VD->getLocation(),
3165 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3166 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003167 continue;
3168 }
3169
3170 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
3171 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003172 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003173 // class type.
3174 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003175 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3176 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3177 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003178 // FIXME This code must be replaced by actual constructing/destructing of
3179 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003180 if (RD) {
3181 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
3182 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003183 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003184 if (!CD ||
3185 CheckConstructorAccess(ELoc, CD,
3186 InitializedEntity::InitializeTemporary(Type),
3187 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003188 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003189 if (IsImplicitClause) {
3190 Diag(ImplicitClauseLoc,
3191 diag::err_omp_task_predetermined_firstprivate_required_method)
3192 << 0;
3193 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3194 } else {
3195 Diag(ELoc, diag::err_omp_required_method)
3196 << getOpenMPClauseName(OMPC_firstprivate) << 1;
3197 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003198 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3199 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003200 Diag(VD->getLocation(),
3201 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3202 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003203 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3204 continue;
3205 }
3206 MarkFunctionReferenced(ELoc, CD);
3207 DiagnoseUseOfDecl(CD, ELoc);
3208
3209 CXXDestructorDecl *DD = RD->getDestructor();
3210 if (DD) {
3211 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3212 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003213 if (IsImplicitClause) {
3214 Diag(ImplicitClauseLoc,
3215 diag::err_omp_task_predetermined_firstprivate_required_method)
3216 << 1;
3217 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3218 } else {
3219 Diag(ELoc, diag::err_omp_required_method)
3220 << getOpenMPClauseName(OMPC_firstprivate) << 4;
3221 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003222 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3223 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003224 Diag(VD->getLocation(),
3225 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3226 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003227 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3228 continue;
3229 }
3230 MarkFunctionReferenced(ELoc, DD);
3231 DiagnoseUseOfDecl(DD, ELoc);
3232 }
3233 }
3234
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003235 // If an implicit firstprivate variable found it was checked already.
3236 if (!IsImplicitClause) {
3237 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003238 Type = Type.getNonReferenceType().getCanonicalType();
3239 bool IsConstant = Type.isConstant(Context);
3240 Type = Context.getBaseElementType(Type);
3241 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
3242 // A list item that specifies a given variable may not appear in more
3243 // than one clause on the same directive, except that a variable may be
3244 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003245 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00003246 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003247 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003248 << getOpenMPClauseName(DVar.CKind)
3249 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003250 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003251 continue;
3252 }
3253
3254 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3255 // in a Construct]
3256 // Variables with the predetermined data-sharing attributes may not be
3257 // listed in data-sharing attributes clauses, except for the cases
3258 // listed below. For these exceptions only, listing a predetermined
3259 // variable in a data-sharing attribute clause is allowed and overrides
3260 // the variable's predetermined data-sharing attributes.
3261 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3262 // in a Construct, C/C++, p.2]
3263 // Variables with const-qualified type having no mutable member may be
3264 // listed in a firstprivate clause, even if they are static data members.
3265 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
3266 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
3267 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003268 << getOpenMPClauseName(DVar.CKind)
3269 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003270 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003271 continue;
3272 }
3273
Alexey Bataevf29276e2014-06-18 04:14:57 +00003274 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003275 // OpenMP [2.9.3.4, Restrictions, p.2]
3276 // A list item that is private within a parallel region must not appear
3277 // in a firstprivate clause on a worksharing construct if any of the
3278 // worksharing regions arising from the worksharing construct ever bind
3279 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00003280 if (isOpenMPWorksharingDirective(CurrDir) &&
3281 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003282 DVar = DSAStack->getImplicitDSA(VD, true);
3283 if (DVar.CKind != OMPC_shared &&
3284 (isOpenMPParallelDirective(DVar.DKind) ||
3285 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003286 Diag(ELoc, diag::err_omp_required_access)
3287 << getOpenMPClauseName(OMPC_firstprivate)
3288 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003289 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003290 continue;
3291 }
3292 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003293 // OpenMP [2.9.3.4, Restrictions, p.3]
3294 // A list item that appears in a reduction clause of a parallel construct
3295 // must not appear in a firstprivate clause on a worksharing or task
3296 // construct if any of the worksharing or task regions arising from the
3297 // worksharing or task construct ever bind to any of the parallel regions
3298 // arising from the parallel construct.
3299 // OpenMP [2.9.3.4, Restrictions, p.4]
3300 // A list item that appears in a reduction clause in worksharing
3301 // construct must not appear in a firstprivate clause in a task construct
3302 // encountered during execution of any of the worksharing regions arising
3303 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003304 if (CurrDir == OMPD_task) {
3305 DVar =
3306 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
3307 [](OpenMPDirectiveKind K) -> bool {
3308 return isOpenMPParallelDirective(K) ||
3309 isOpenMPWorksharingDirective(K);
3310 },
3311 false);
3312 if (DVar.CKind == OMPC_reduction &&
3313 (isOpenMPParallelDirective(DVar.DKind) ||
3314 isOpenMPWorksharingDirective(DVar.DKind))) {
3315 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
3316 << getOpenMPDirectiveName(DVar.DKind);
3317 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3318 continue;
3319 }
3320 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003321 }
3322
3323 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
3324 Vars.push_back(DE);
3325 }
3326
Alexey Bataeved09d242014-05-28 05:53:51 +00003327 if (Vars.empty())
3328 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003329
3330 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3331 Vars);
3332}
3333
Alexander Musman1bb328c2014-06-04 13:06:39 +00003334OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
3335 SourceLocation StartLoc,
3336 SourceLocation LParenLoc,
3337 SourceLocation EndLoc) {
3338 SmallVector<Expr *, 8> Vars;
3339 for (auto &RefExpr : VarList) {
3340 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
3341 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3342 // It will be analyzed later.
3343 Vars.push_back(RefExpr);
3344 continue;
3345 }
3346
3347 SourceLocation ELoc = RefExpr->getExprLoc();
3348 // OpenMP [2.1, C/C++]
3349 // A list item is a variable name.
3350 // OpenMP [2.14.3.5, Restrictions, p.1]
3351 // A variable that is part of another variable (as an array or structure
3352 // element) cannot appear in a lastprivate clause.
3353 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3354 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3355 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3356 continue;
3357 }
3358 Decl *D = DE->getDecl();
3359 VarDecl *VD = cast<VarDecl>(D);
3360
3361 QualType Type = VD->getType();
3362 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3363 // It will be analyzed later.
3364 Vars.push_back(DE);
3365 continue;
3366 }
3367
3368 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
3369 // A variable that appears in a lastprivate clause must not have an
3370 // incomplete type or a reference type.
3371 if (RequireCompleteType(ELoc, Type,
3372 diag::err_omp_lastprivate_incomplete_type)) {
3373 continue;
3374 }
3375 if (Type->isReferenceType()) {
3376 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3377 << getOpenMPClauseName(OMPC_lastprivate) << Type;
3378 bool IsDecl =
3379 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3380 Diag(VD->getLocation(),
3381 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3382 << VD;
3383 continue;
3384 }
3385
3386 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3387 // in a Construct]
3388 // Variables with the predetermined data-sharing attributes may not be
3389 // listed in data-sharing attributes clauses, except for the cases
3390 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003391 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003392 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
3393 DVar.CKind != OMPC_firstprivate &&
3394 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3395 Diag(ELoc, diag::err_omp_wrong_dsa)
3396 << getOpenMPClauseName(DVar.CKind)
3397 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003398 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003399 continue;
3400 }
3401
Alexey Bataevf29276e2014-06-18 04:14:57 +00003402 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
3403 // OpenMP [2.14.3.5, Restrictions, p.2]
3404 // A list item that is private within a parallel region, or that appears in
3405 // the reduction clause of a parallel construct, must not appear in a
3406 // lastprivate clause on a worksharing construct if any of the corresponding
3407 // worksharing regions ever binds to any of the corresponding parallel
3408 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00003409 if (isOpenMPWorksharingDirective(CurrDir) &&
3410 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003411 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003412 if (DVar.CKind != OMPC_shared) {
3413 Diag(ELoc, diag::err_omp_required_access)
3414 << getOpenMPClauseName(OMPC_lastprivate)
3415 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003416 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003417 continue;
3418 }
3419 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003420 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003421 // A variable of class type (or array thereof) that appears in a
3422 // lastprivate clause requires an accessible, unambiguous default
3423 // constructor for the class type, unless the list item is also specified
3424 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003425 // A variable of class type (or array thereof) that appears in a
3426 // lastprivate clause requires an accessible, unambiguous copy assignment
3427 // operator for the class type.
3428 while (Type.getNonReferenceType()->isArrayType())
3429 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3430 ->getElementType();
3431 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3432 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3433 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003434 // FIXME This code must be replaced by actual copying and destructing of the
3435 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003436 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003437 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3438 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003439 if (MD) {
3440 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3441 MD->isDeleted()) {
3442 Diag(ELoc, diag::err_omp_required_method)
3443 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3444 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3445 VarDecl::DeclarationOnly;
3446 Diag(VD->getLocation(),
3447 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3448 << VD;
3449 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3450 continue;
3451 }
3452 MarkFunctionReferenced(ELoc, MD);
3453 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003454 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003455
3456 CXXDestructorDecl *DD = RD->getDestructor();
3457 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003458 PartialDiagnostic PD =
3459 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003460 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3461 DD->isDeleted()) {
3462 Diag(ELoc, diag::err_omp_required_method)
3463 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3464 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3465 VarDecl::DeclarationOnly;
3466 Diag(VD->getLocation(),
3467 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3468 << VD;
3469 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3470 continue;
3471 }
3472 MarkFunctionReferenced(ELoc, DD);
3473 DiagnoseUseOfDecl(DD, ELoc);
3474 }
3475 }
3476
Alexey Bataevf29276e2014-06-18 04:14:57 +00003477 if (DVar.CKind != OMPC_firstprivate)
3478 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003479 Vars.push_back(DE);
3480 }
3481
3482 if (Vars.empty())
3483 return nullptr;
3484
3485 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3486 Vars);
3487}
3488
Alexey Bataev758e55e2013-09-06 18:03:48 +00003489OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3490 SourceLocation StartLoc,
3491 SourceLocation LParenLoc,
3492 SourceLocation EndLoc) {
3493 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003494 for (auto &RefExpr : VarList) {
3495 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3496 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003497 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003498 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003499 continue;
3500 }
3501
Alexey Bataeved09d242014-05-28 05:53:51 +00003502 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003503 // OpenMP [2.1, C/C++]
3504 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003505 // OpenMP [2.14.3.2, Restrictions, p.1]
3506 // A variable that is part of another variable (as an array or structure
3507 // element) cannot appear in a shared unless it is a static data member
3508 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003509 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003510 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003511 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003512 continue;
3513 }
3514 Decl *D = DE->getDecl();
3515 VarDecl *VD = cast<VarDecl>(D);
3516
3517 QualType Type = VD->getType();
3518 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3519 // It will be analyzed later.
3520 Vars.push_back(DE);
3521 continue;
3522 }
3523
3524 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3525 // in a Construct]
3526 // Variables with the predetermined data-sharing attributes may not be
3527 // listed in data-sharing attributes clauses, except for the cases
3528 // listed below. For these exceptions only, listing a predetermined
3529 // variable in a data-sharing attribute clause is allowed and overrides
3530 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003531 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003532 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3533 DVar.RefExpr) {
3534 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3535 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003536 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003537 continue;
3538 }
3539
3540 DSAStack->addDSA(VD, DE, OMPC_shared);
3541 Vars.push_back(DE);
3542 }
3543
Alexey Bataeved09d242014-05-28 05:53:51 +00003544 if (Vars.empty())
3545 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003546
3547 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3548}
3549
Alexey Bataevc5e02582014-06-16 07:08:35 +00003550namespace {
3551class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3552 DSAStackTy *Stack;
3553
3554public:
3555 bool VisitDeclRefExpr(DeclRefExpr *E) {
3556 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003557 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003558 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3559 return false;
3560 if (DVar.CKind != OMPC_unknown)
3561 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003562 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003563 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003564 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003565 return true;
3566 return false;
3567 }
3568 return false;
3569 }
3570 bool VisitStmt(Stmt *S) {
3571 for (auto Child : S->children()) {
3572 if (Child && Visit(Child))
3573 return true;
3574 }
3575 return false;
3576 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003577 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003578};
Alexey Bataev23b69422014-06-18 07:08:49 +00003579} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003580
3581OMPClause *Sema::ActOnOpenMPReductionClause(
3582 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3583 SourceLocation ColonLoc, SourceLocation EndLoc,
3584 CXXScopeSpec &ReductionIdScopeSpec,
3585 const DeclarationNameInfo &ReductionId) {
3586 // TODO: Allow scope specification search when 'declare reduction' is
3587 // supported.
3588 assert(ReductionIdScopeSpec.isEmpty() &&
3589 "No support for scoped reduction identifiers yet.");
3590
3591 auto DN = ReductionId.getName();
3592 auto OOK = DN.getCXXOverloadedOperator();
3593 BinaryOperatorKind BOK = BO_Comma;
3594
3595 // OpenMP [2.14.3.6, reduction clause]
3596 // C
3597 // reduction-identifier is either an identifier or one of the following
3598 // operators: +, -, *, &, |, ^, && and ||
3599 // C++
3600 // reduction-identifier is either an id-expression or one of the following
3601 // operators: +, -, *, &, |, ^, && and ||
3602 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3603 switch (OOK) {
3604 case OO_Plus:
3605 case OO_Minus:
3606 BOK = BO_AddAssign;
3607 break;
3608 case OO_Star:
3609 BOK = BO_MulAssign;
3610 break;
3611 case OO_Amp:
3612 BOK = BO_AndAssign;
3613 break;
3614 case OO_Pipe:
3615 BOK = BO_OrAssign;
3616 break;
3617 case OO_Caret:
3618 BOK = BO_XorAssign;
3619 break;
3620 case OO_AmpAmp:
3621 BOK = BO_LAnd;
3622 break;
3623 case OO_PipePipe:
3624 BOK = BO_LOr;
3625 break;
3626 default:
3627 if (auto II = DN.getAsIdentifierInfo()) {
3628 if (II->isStr("max"))
3629 BOK = BO_GT;
3630 else if (II->isStr("min"))
3631 BOK = BO_LT;
3632 }
3633 break;
3634 }
3635 SourceRange ReductionIdRange;
3636 if (ReductionIdScopeSpec.isValid()) {
3637 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3638 }
3639 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3640 if (BOK == BO_Comma) {
3641 // Not allowed reduction identifier is found.
3642 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3643 << ReductionIdRange;
3644 return nullptr;
3645 }
3646
3647 SmallVector<Expr *, 8> Vars;
3648 for (auto RefExpr : VarList) {
3649 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3650 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3651 // It will be analyzed later.
3652 Vars.push_back(RefExpr);
3653 continue;
3654 }
3655
3656 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3657 RefExpr->isInstantiationDependent() ||
3658 RefExpr->containsUnexpandedParameterPack()) {
3659 // It will be analyzed later.
3660 Vars.push_back(RefExpr);
3661 continue;
3662 }
3663
3664 auto ELoc = RefExpr->getExprLoc();
3665 auto ERange = RefExpr->getSourceRange();
3666 // OpenMP [2.1, C/C++]
3667 // A list item is a variable or array section, subject to the restrictions
3668 // specified in Section 2.4 on page 42 and in each of the sections
3669 // describing clauses and directives for which a list appears.
3670 // OpenMP [2.14.3.3, Restrictions, p.1]
3671 // A variable that is part of another variable (as an array or
3672 // structure element) cannot appear in a private clause.
3673 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3674 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3675 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3676 continue;
3677 }
3678 auto D = DE->getDecl();
3679 auto VD = cast<VarDecl>(D);
3680 auto Type = VD->getType();
3681 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3682 // A variable that appears in a private clause must not have an incomplete
3683 // type or a reference type.
3684 if (RequireCompleteType(ELoc, Type,
3685 diag::err_omp_reduction_incomplete_type))
3686 continue;
3687 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3688 // Arrays may not appear in a reduction clause.
3689 if (Type.getNonReferenceType()->isArrayType()) {
3690 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3691 bool IsDecl =
3692 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3693 Diag(VD->getLocation(),
3694 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3695 << VD;
3696 continue;
3697 }
3698 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3699 // A list item that appears in a reduction clause must not be
3700 // const-qualified.
3701 if (Type.getNonReferenceType().isConstant(Context)) {
3702 Diag(ELoc, diag::err_omp_const_variable)
3703 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3704 bool IsDecl =
3705 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3706 Diag(VD->getLocation(),
3707 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3708 << VD;
3709 continue;
3710 }
3711 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3712 // If a list-item is a reference type then it must bind to the same object
3713 // for all threads of the team.
3714 VarDecl *VDDef = VD->getDefinition();
3715 if (Type->isReferenceType() && VDDef) {
3716 DSARefChecker Check(DSAStack);
3717 if (Check.Visit(VDDef->getInit())) {
3718 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3719 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3720 continue;
3721 }
3722 }
3723 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3724 // The type of a list item that appears in a reduction clause must be valid
3725 // for the reduction-identifier. For a max or min reduction in C, the type
3726 // of the list item must be an allowed arithmetic data type: char, int,
3727 // float, double, or _Bool, possibly modified with long, short, signed, or
3728 // unsigned. For a max or min reduction in C++, the type of the list item
3729 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3730 // double, or bool, possibly modified with long, short, signed, or unsigned.
3731 if ((BOK == BO_GT || BOK == BO_LT) &&
3732 !(Type->isScalarType() ||
3733 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3734 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3735 << getLangOpts().CPlusPlus;
3736 bool IsDecl =
3737 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3738 Diag(VD->getLocation(),
3739 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3740 << VD;
3741 continue;
3742 }
3743 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3744 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3745 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3746 bool IsDecl =
3747 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3748 Diag(VD->getLocation(),
3749 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3750 << VD;
3751 continue;
3752 }
3753 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3754 getDiagnostics().setSuppressAllDiagnostics(true);
3755 ExprResult ReductionOp =
3756 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3757 RefExpr, RefExpr);
3758 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3759 if (ReductionOp.isInvalid()) {
3760 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003761 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003762 bool IsDecl =
3763 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3764 Diag(VD->getLocation(),
3765 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3766 << VD;
3767 continue;
3768 }
3769
3770 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3771 // in a Construct]
3772 // Variables with the predetermined data-sharing attributes may not be
3773 // listed in data-sharing attributes clauses, except for the cases
3774 // listed below. For these exceptions only, listing a predetermined
3775 // variable in a data-sharing attribute clause is allowed and overrides
3776 // the variable's predetermined data-sharing attributes.
3777 // OpenMP [2.14.3.6, Restrictions, p.3]
3778 // Any number of reduction clauses can be specified on the directive,
3779 // but a list item can appear only once in the reduction clauses for that
3780 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003781 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003782 if (DVar.CKind == OMPC_reduction) {
3783 Diag(ELoc, diag::err_omp_once_referenced)
3784 << getOpenMPClauseName(OMPC_reduction);
3785 if (DVar.RefExpr) {
3786 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3787 }
3788 } else if (DVar.CKind != OMPC_unknown) {
3789 Diag(ELoc, diag::err_omp_wrong_dsa)
3790 << getOpenMPClauseName(DVar.CKind)
3791 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003792 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003793 continue;
3794 }
3795
3796 // OpenMP [2.14.3.6, Restrictions, p.1]
3797 // A list item that appears in a reduction clause of a worksharing
3798 // construct must be shared in the parallel regions to which any of the
3799 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003800 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003801 if (isOpenMPWorksharingDirective(CurrDir) &&
3802 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003803 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003804 if (DVar.CKind != OMPC_shared) {
3805 Diag(ELoc, diag::err_omp_required_access)
3806 << getOpenMPClauseName(OMPC_reduction)
3807 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003808 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003809 continue;
3810 }
3811 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003812
3813 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3814 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3815 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003816 // FIXME This code must be replaced by actual constructing/destructing of
3817 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003818 if (RD) {
3819 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3820 PartialDiagnostic PD =
3821 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003822 if (!CD ||
3823 CheckConstructorAccess(ELoc, CD,
3824 InitializedEntity::InitializeTemporary(Type),
3825 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003826 CD->isDeleted()) {
3827 Diag(ELoc, diag::err_omp_required_method)
3828 << getOpenMPClauseName(OMPC_reduction) << 0;
3829 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3830 VarDecl::DeclarationOnly;
3831 Diag(VD->getLocation(),
3832 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3833 << VD;
3834 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3835 continue;
3836 }
3837 MarkFunctionReferenced(ELoc, CD);
3838 DiagnoseUseOfDecl(CD, ELoc);
3839
3840 CXXDestructorDecl *DD = RD->getDestructor();
3841 if (DD) {
3842 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3843 DD->isDeleted()) {
3844 Diag(ELoc, diag::err_omp_required_method)
3845 << getOpenMPClauseName(OMPC_reduction) << 4;
3846 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3847 VarDecl::DeclarationOnly;
3848 Diag(VD->getLocation(),
3849 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3850 << VD;
3851 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3852 continue;
3853 }
3854 MarkFunctionReferenced(ELoc, DD);
3855 DiagnoseUseOfDecl(DD, ELoc);
3856 }
3857 }
3858
3859 DSAStack->addDSA(VD, DE, OMPC_reduction);
3860 Vars.push_back(DE);
3861 }
3862
3863 if (Vars.empty())
3864 return nullptr;
3865
3866 return OMPReductionClause::Create(
3867 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3868 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3869}
3870
Alexander Musman8dba6642014-04-22 13:09:42 +00003871OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3872 SourceLocation StartLoc,
3873 SourceLocation LParenLoc,
3874 SourceLocation ColonLoc,
3875 SourceLocation EndLoc) {
3876 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003877 for (auto &RefExpr : VarList) {
3878 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3879 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003880 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003881 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003882 continue;
3883 }
3884
3885 // OpenMP [2.14.3.7, linear clause]
3886 // A list item that appears in a linear clause is subject to the private
3887 // clause semantics described in Section 2.14.3.3 on page 159 except as
3888 // noted. In addition, the value of the new list item on each iteration
3889 // of the associated loop(s) corresponds to the value of the original
3890 // list item before entering the construct plus the logical number of
3891 // the iteration times linear-step.
3892
Alexey Bataeved09d242014-05-28 05:53:51 +00003893 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003894 // OpenMP [2.1, C/C++]
3895 // A list item is a variable name.
3896 // OpenMP [2.14.3.3, Restrictions, p.1]
3897 // A variable that is part of another variable (as an array or
3898 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003899 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003900 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003901 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003902 continue;
3903 }
3904
3905 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3906
3907 // OpenMP [2.14.3.7, linear clause]
3908 // A list-item cannot appear in more than one linear clause.
3909 // A list-item that appears in a linear clause cannot appear in any
3910 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003911 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003912 if (DVar.RefExpr) {
3913 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3914 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003915 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003916 continue;
3917 }
3918
3919 QualType QType = VD->getType();
3920 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3921 // It will be analyzed later.
3922 Vars.push_back(DE);
3923 continue;
3924 }
3925
3926 // A variable must not have an incomplete type or a reference type.
3927 if (RequireCompleteType(ELoc, QType,
3928 diag::err_omp_linear_incomplete_type)) {
3929 continue;
3930 }
3931 if (QType->isReferenceType()) {
3932 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3933 << getOpenMPClauseName(OMPC_linear) << QType;
3934 bool IsDecl =
3935 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3936 Diag(VD->getLocation(),
3937 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3938 << VD;
3939 continue;
3940 }
3941
3942 // A list item must not be const-qualified.
3943 if (QType.isConstant(Context)) {
3944 Diag(ELoc, diag::err_omp_const_variable)
3945 << getOpenMPClauseName(OMPC_linear);
3946 bool IsDecl =
3947 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3948 Diag(VD->getLocation(),
3949 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3950 << VD;
3951 continue;
3952 }
3953
3954 // A list item must be of integral or pointer type.
3955 QType = QType.getUnqualifiedType().getCanonicalType();
3956 const Type *Ty = QType.getTypePtrOrNull();
3957 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3958 !Ty->isPointerType())) {
3959 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3960 bool IsDecl =
3961 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3962 Diag(VD->getLocation(),
3963 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3964 << VD;
3965 continue;
3966 }
3967
3968 DSAStack->addDSA(VD, DE, OMPC_linear);
3969 Vars.push_back(DE);
3970 }
3971
3972 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003973 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003974
3975 Expr *StepExpr = Step;
3976 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3977 !Step->isInstantiationDependent() &&
3978 !Step->containsUnexpandedParameterPack()) {
3979 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003980 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003981 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003982 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003983 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003984
3985 // Warn about zero linear step (it would be probably better specified as
3986 // making corresponding variables 'const').
3987 llvm::APSInt Result;
3988 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3989 !Result.isNegative() && !Result.isStrictlyPositive())
3990 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3991 << (Vars.size() > 1);
3992 }
3993
3994 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3995 Vars, StepExpr);
3996}
3997
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003998OMPClause *Sema::ActOnOpenMPAlignedClause(
3999 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4000 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4001
4002 SmallVector<Expr *, 8> Vars;
4003 for (auto &RefExpr : VarList) {
4004 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4005 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4006 // It will be analyzed later.
4007 Vars.push_back(RefExpr);
4008 continue;
4009 }
4010
4011 SourceLocation ELoc = RefExpr->getExprLoc();
4012 // OpenMP [2.1, C/C++]
4013 // A list item is a variable name.
4014 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4015 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4016 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4017 continue;
4018 }
4019
4020 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4021
4022 // OpenMP [2.8.1, simd construct, Restrictions]
4023 // The type of list items appearing in the aligned clause must be
4024 // array, pointer, reference to array, or reference to pointer.
4025 QualType QType = DE->getType()
4026 .getNonReferenceType()
4027 .getUnqualifiedType()
4028 .getCanonicalType();
4029 const Type *Ty = QType.getTypePtrOrNull();
4030 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4031 !Ty->isPointerType())) {
4032 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4033 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4034 bool IsDecl =
4035 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4036 Diag(VD->getLocation(),
4037 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4038 << VD;
4039 continue;
4040 }
4041
4042 // OpenMP [2.8.1, simd construct, Restrictions]
4043 // A list-item cannot appear in more than one aligned clause.
4044 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4045 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4046 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4047 << getOpenMPClauseName(OMPC_aligned);
4048 continue;
4049 }
4050
4051 Vars.push_back(DE);
4052 }
4053
4054 // OpenMP [2.8.1, simd construct, Description]
4055 // The parameter of the aligned clause, alignment, must be a constant
4056 // positive integer expression.
4057 // If no optional parameter is specified, implementation-defined default
4058 // alignments for SIMD instructions on the target platforms are assumed.
4059 if (Alignment != nullptr) {
4060 ExprResult AlignResult =
4061 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
4062 if (AlignResult.isInvalid())
4063 return nullptr;
4064 Alignment = AlignResult.get();
4065 }
4066 if (Vars.empty())
4067 return nullptr;
4068
4069 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
4070 EndLoc, Vars, Alignment);
4071}
4072
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004073OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
4074 SourceLocation StartLoc,
4075 SourceLocation LParenLoc,
4076 SourceLocation EndLoc) {
4077 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004078 for (auto &RefExpr : VarList) {
4079 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
4080 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004081 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004082 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004083 continue;
4084 }
4085
Alexey Bataeved09d242014-05-28 05:53:51 +00004086 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004087 // OpenMP [2.1, C/C++]
4088 // A list item is a variable name.
4089 // OpenMP [2.14.4.1, Restrictions, p.1]
4090 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00004091 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004092 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004093 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004094 continue;
4095 }
4096
4097 Decl *D = DE->getDecl();
4098 VarDecl *VD = cast<VarDecl>(D);
4099
4100 QualType Type = VD->getType();
4101 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4102 // It will be analyzed later.
4103 Vars.push_back(DE);
4104 continue;
4105 }
4106
4107 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
4108 // A list item that appears in a copyin clause must be threadprivate.
4109 if (!DSAStack->isThreadPrivate(VD)) {
4110 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00004111 << getOpenMPClauseName(OMPC_copyin)
4112 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004113 continue;
4114 }
4115
4116 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4117 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00004118 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004119 // operator for the class type.
4120 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004121 CXXRecordDecl *RD =
4122 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004123 // FIXME This code must be replaced by actual assignment of the
4124 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004125 if (RD) {
4126 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4127 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004128 if (MD) {
4129 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4130 MD->isDeleted()) {
4131 Diag(ELoc, diag::err_omp_required_method)
4132 << getOpenMPClauseName(OMPC_copyin) << 2;
4133 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4134 VarDecl::DeclarationOnly;
4135 Diag(VD->getLocation(),
4136 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4137 << VD;
4138 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4139 continue;
4140 }
4141 MarkFunctionReferenced(ELoc, MD);
4142 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004143 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004144 }
4145
4146 DSAStack->addDSA(VD, DE, OMPC_copyin);
4147 Vars.push_back(DE);
4148 }
4149
Alexey Bataeved09d242014-05-28 05:53:51 +00004150 if (Vars.empty())
4151 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004152
4153 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4154}
4155
Alexey Bataevbae9a792014-06-27 10:37:06 +00004156OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
4157 SourceLocation StartLoc,
4158 SourceLocation LParenLoc,
4159 SourceLocation EndLoc) {
4160 SmallVector<Expr *, 8> Vars;
4161 for (auto &RefExpr : VarList) {
4162 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
4163 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4164 // It will be analyzed later.
4165 Vars.push_back(RefExpr);
4166 continue;
4167 }
4168
4169 SourceLocation ELoc = RefExpr->getExprLoc();
4170 // OpenMP [2.1, C/C++]
4171 // A list item is a variable name.
4172 // OpenMP [2.14.4.1, Restrictions, p.1]
4173 // A list item that appears in a copyin clause must be threadprivate.
4174 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4175 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4176 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4177 continue;
4178 }
4179
4180 Decl *D = DE->getDecl();
4181 VarDecl *VD = cast<VarDecl>(D);
4182
4183 QualType Type = VD->getType();
4184 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4185 // It will be analyzed later.
4186 Vars.push_back(DE);
4187 continue;
4188 }
4189
4190 // OpenMP [2.14.4.2, Restrictions, p.2]
4191 // A list item that appears in a copyprivate clause may not appear in a
4192 // private or firstprivate clause on the single construct.
4193 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004194 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004195 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
4196 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
4197 Diag(ELoc, diag::err_omp_wrong_dsa)
4198 << getOpenMPClauseName(DVar.CKind)
4199 << getOpenMPClauseName(OMPC_copyprivate);
4200 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4201 continue;
4202 }
4203
4204 // OpenMP [2.11.4.2, Restrictions, p.1]
4205 // All list items that appear in a copyprivate clause must be either
4206 // threadprivate or private in the enclosing context.
4207 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004208 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004209 if (DVar.CKind == OMPC_shared) {
4210 Diag(ELoc, diag::err_omp_required_access)
4211 << getOpenMPClauseName(OMPC_copyprivate)
4212 << "threadprivate or private in the enclosing context";
4213 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4214 continue;
4215 }
4216 }
4217 }
4218
4219 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4220 // A variable of class type (or array thereof) that appears in a
4221 // copyin clause requires an accessible, unambiguous copy assignment
4222 // operator for the class type.
4223 Type = Context.getBaseElementType(Type);
4224 CXXRecordDecl *RD =
4225 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
4226 // FIXME This code must be replaced by actual assignment of the
4227 // threadprivate variable.
4228 if (RD) {
4229 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4230 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
4231 if (MD) {
4232 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4233 MD->isDeleted()) {
4234 Diag(ELoc, diag::err_omp_required_method)
4235 << getOpenMPClauseName(OMPC_copyprivate) << 2;
4236 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4237 VarDecl::DeclarationOnly;
4238 Diag(VD->getLocation(),
4239 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4240 << VD;
4241 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4242 continue;
4243 }
4244 MarkFunctionReferenced(ELoc, MD);
4245 DiagnoseUseOfDecl(MD, ELoc);
4246 }
4247 }
4248
4249 // No need to mark vars as copyprivate, they are already threadprivate or
4250 // implicitly private.
4251 Vars.push_back(DE);
4252 }
4253
4254 if (Vars.empty())
4255 return nullptr;
4256
4257 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4258}
4259
Alexey Bataev6125da92014-07-21 11:26:11 +00004260OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
4261 SourceLocation StartLoc,
4262 SourceLocation LParenLoc,
4263 SourceLocation EndLoc) {
4264 if (VarList.empty())
4265 return nullptr;
4266
4267 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
4268}