blob: c7c7654250166d6d849549dd3c82bb7966060f0c [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 Bataeved09d242014-05-28 05:53:51 +000095 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000096 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000097 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev9fb6e642014-07-22 06:45:04 +000099 ConstructLoc(Loc), OrderedRegion(false) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000100 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000101 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000103 ConstructLoc(), OrderedRegion(false) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 };
105
106 typedef SmallVector<SharingMapTy, 64> StackTy;
107
108 /// \brief Stack of used declaration and their data-sharing attributes.
109 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000110 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111
112 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
113
114 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000115
116 /// \brief Checks if the variable is a local for OpenMP region.
117 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000118
Alexey Bataev758e55e2013-09-06 18:03:48 +0000119public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000120 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121
122 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000123 Scope *CurScope, SourceLocation Loc) {
124 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
125 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126 }
127
128 void pop() {
129 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
130 Stack.pop_back();
131 }
132
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000133 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000134 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000135 /// for diagnostics.
136 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
137
Alexey Bataev758e55e2013-09-06 18:03:48 +0000138 /// \brief Adds explicit data sharing attribute to the specified declaration.
139 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
140
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141 /// \brief Returns data sharing attributes from top of the stack for the
142 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000143 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000145 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000146 /// \brief Checks if the specified variables has data-sharing attributes which
147 /// match specified \a CPred predicate in any directive which matches \a DPred
148 /// predicate.
149 template <class ClausesPredicate, class DirectivesPredicate>
150 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000151 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000152 /// \brief Checks if the specified variables has data-sharing attributes which
153 /// match specified \a CPred predicate in any innermost directive which
154 /// matches \a DPred predicate.
155 template <class ClausesPredicate, class DirectivesPredicate>
156 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000157 DirectivesPredicate DPred,
158 bool FromParent);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000159 /// \brief Finds a directive which matches specified \a DPred predicate.
160 template <class NamedDirectivesPredicate>
161 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000162
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163 /// \brief Returns currently analyzed directive.
164 OpenMPDirectiveKind getCurrentDirective() const {
165 return Stack.back().Directive;
166 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000167 /// \brief Returns parent directive.
168 OpenMPDirectiveKind getParentDirective() const {
169 if (Stack.size() > 2)
170 return Stack[Stack.size() - 2].Directive;
171 return OMPD_unknown;
172 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000173
174 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000175 void setDefaultDSANone(SourceLocation Loc) {
176 Stack.back().DefaultAttr = DSA_none;
177 Stack.back().DefaultAttrLoc = Loc;
178 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000179 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000180 void setDefaultDSAShared(SourceLocation Loc) {
181 Stack.back().DefaultAttr = DSA_shared;
182 Stack.back().DefaultAttrLoc = Loc;
183 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000184
185 DefaultDataSharingAttributes getDefaultDSA() const {
186 return Stack.back().DefaultAttr;
187 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000188 SourceLocation getDefaultDSALocation() const {
189 return Stack.back().DefaultAttrLoc;
190 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000191
Alexey Bataevf29276e2014-06-18 04:14:57 +0000192 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000193 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000194 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000195 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000196 }
197
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000198 /// \brief Marks current region as ordered (it has an 'ordered' clause).
199 void setOrderedRegion(bool IsOrdered = true) {
200 Stack.back().OrderedRegion = IsOrdered;
201 }
202 /// \brief Returns true, if parent region is ordered (has associated
203 /// 'ordered' clause), false - otherwise.
204 bool isParentOrderedRegion() const {
205 if (Stack.size() > 2)
206 return Stack[Stack.size() - 2].OrderedRegion;
207 return false;
208 }
209
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000210 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000211 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000212 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000213};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000214bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
215 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
216 DKind == OMPD_unknown;
217}
Alexey Bataeved09d242014-05-28 05:53:51 +0000218} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000219
220DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
221 VarDecl *D) {
222 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000223 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000224 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
225 // in a region but not in construct]
226 // File-scope or namespace-scope variables referenced in called routines
227 // in the region are shared unless they appear in a threadprivate
228 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000229 if (!D->isFunctionOrMethodVarDecl())
230 DVar.CKind = OMPC_shared;
231
232 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
233 // in a region but not in construct]
234 // Variables with static storage duration that are declared in called
235 // routines in the region are shared.
236 if (D->hasGlobalStorage())
237 DVar.CKind = OMPC_shared;
238
Alexey Bataev758e55e2013-09-06 18:03:48 +0000239 return DVar;
240 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000241
Alexey Bataev758e55e2013-09-06 18:03:48 +0000242 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
244 // in a Construct, C/C++, predetermined, p.1]
245 // Variables with automatic storage duration that are declared in a scope
246 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000247 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
248 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
249 DVar.CKind = OMPC_private;
250 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000251 }
252
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253 // Explicitly specified attributes and local variables with predetermined
254 // attributes.
255 if (Iter->SharingMap.count(D)) {
256 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
257 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000258 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000259 return DVar;
260 }
261
262 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
263 // in a Construct, C/C++, implicitly determined, p.1]
264 // In a parallel or task construct, the data-sharing attributes of these
265 // variables are determined by the default clause, if present.
266 switch (Iter->DefaultAttr) {
267 case DSA_shared:
268 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000269 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270 return DVar;
271 case DSA_none:
272 return DVar;
273 case DSA_unspecified:
274 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
275 // in a Construct, implicitly determined, p.2]
276 // In a parallel construct, if no default clause is present, these
277 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000278 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000279 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 DVar.CKind = OMPC_shared;
281 return DVar;
282 }
283
284 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
285 // in a Construct, implicitly determined, p.4]
286 // In a task construct, if no default clause is present, a variable that in
287 // the enclosing context is determined to be shared by all implicit tasks
288 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 if (DVar.DKind == OMPD_task) {
290 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000291 for (StackTy::reverse_iterator I = std::next(Iter),
292 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000293 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000294 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
295 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000296 // in a Construct, implicitly determined, p.6]
297 // In a task construct, if no default clause is present, a variable
298 // whose data-sharing attribute is not determined by the rules above is
299 // firstprivate.
300 DVarTemp = getDSA(I, D);
301 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000302 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000303 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000304 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000305 return DVar;
306 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000307 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000308 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309 }
310 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000311 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000312 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000313 return DVar;
314 }
315 }
316 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
317 // in a Construct, implicitly determined, p.3]
318 // For constructs other than task, if no default clause is present, these
319 // variables inherit their data-sharing attributes from the enclosing
320 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000321 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322}
323
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000324DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
325 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
326 auto It = Stack.back().AlignedMap.find(D);
327 if (It == Stack.back().AlignedMap.end()) {
328 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
329 Stack.back().AlignedMap[D] = NewDE;
330 return nullptr;
331 } else {
332 assert(It->second && "Unexpected nullptr expr in the aligned map");
333 return It->second;
334 }
335 return nullptr;
336}
337
Alexey Bataev758e55e2013-09-06 18:03:48 +0000338void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
339 if (A == OMPC_threadprivate) {
340 Stack[0].SharingMap[D].Attributes = A;
341 Stack[0].SharingMap[D].RefExpr = E;
342 } else {
343 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
344 Stack.back().SharingMap[D].Attributes = A;
345 Stack.back().SharingMap[D].RefExpr = E;
346 }
347}
348
Alexey Bataeved09d242014-05-28 05:53:51 +0000349bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000350 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000351 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000352 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000353 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000354 ++I;
355 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000356 if (I == E)
357 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000358 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000359 Scope *CurScope = getCurScope();
360 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000361 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000362 }
363 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000364 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000365 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000366}
367
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000368DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000369 DSAVarData DVar;
370
371 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
372 // in a Construct, C/C++, predetermined, p.1]
373 // Variables appearing in threadprivate directives are threadprivate.
374 if (D->getTLSKind() != VarDecl::TLS_None) {
375 DVar.CKind = OMPC_threadprivate;
376 return DVar;
377 }
378 if (Stack[0].SharingMap.count(D)) {
379 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
380 DVar.CKind = OMPC_threadprivate;
381 return DVar;
382 }
383
384 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
385 // in a Construct, C/C++, predetermined, p.1]
386 // Variables with automatic storage duration that are declared in a scope
387 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000388 OpenMPDirectiveKind Kind =
389 FromParent ? getParentDirective() : getCurrentDirective();
390 auto StartI = std::next(Stack.rbegin());
391 auto EndI = std::prev(Stack.rend());
392 if (FromParent && StartI != EndI) {
393 StartI = std::next(StartI);
394 }
395 if (!isParallelOrTaskRegion(Kind)) {
396 if (isOpenMPLocal(D, StartI) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000397 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000398 DVar.CKind = OMPC_private;
399 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000400 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401 }
402
403 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
404 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000405 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000406 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000407 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000408 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000409 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
410 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000411 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
412 return DVar;
413
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414 DVar.CKind = OMPC_shared;
415 return DVar;
416 }
417
418 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000419 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000420 while (Type->isArrayType()) {
421 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
422 Type = ElemType.getNonReferenceType().getCanonicalType();
423 }
424 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
425 // in a Construct, C/C++, predetermined, p.6]
426 // Variables with const qualified type having no mutable member are
427 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000428 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000429 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000430 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000431 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432 // Variables with const-qualified type having no mutable member may be
433 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000434 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
435 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000436 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
437 return DVar;
438
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 DVar.CKind = OMPC_shared;
440 return DVar;
441 }
442
443 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
444 // in a Construct, C/C++, predetermined, p.7]
445 // Variables with static storage duration that are declared in a scope
446 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000447 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000448 DVar.CKind = OMPC_shared;
449 return DVar;
450 }
451
452 // Explicitly specified attributes and local variables with predetermined
453 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000454 auto I = std::prev(StartI);
455 if (I->SharingMap.count(D)) {
456 DVar.RefExpr = I->SharingMap[D].RefExpr;
457 DVar.CKind = I->SharingMap[D].Attributes;
458 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000459 }
460
461 return DVar;
462}
463
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000464DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
465 auto StartI = Stack.rbegin();
466 auto EndI = std::prev(Stack.rend());
467 if (FromParent && StartI != EndI) {
468 StartI = std::next(StartI);
469 }
470 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000471}
472
Alexey Bataevf29276e2014-06-18 04:14:57 +0000473template <class ClausesPredicate, class DirectivesPredicate>
474DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000475 DirectivesPredicate DPred,
476 bool FromParent) {
477 auto StartI = std::next(Stack.rbegin());
478 auto EndI = std::prev(Stack.rend());
479 if (FromParent && StartI != EndI) {
480 StartI = std::next(StartI);
481 }
482 for (auto I = StartI, EE = EndI; I != EE; ++I) {
483 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000484 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000485 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000486 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000487 return DVar;
488 }
489 return DSAVarData();
490}
491
Alexey Bataevf29276e2014-06-18 04:14:57 +0000492template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000493DSAStackTy::DSAVarData
494DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
495 DirectivesPredicate DPred, bool FromParent) {
496 auto StartI = std::next(Stack.rbegin());
497 auto EndI = std::prev(Stack.rend());
498 if (FromParent && StartI != EndI) {
499 StartI = std::next(StartI);
500 }
501 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000502 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000503 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000504 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000505 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000506 return DVar;
507 return DSAVarData();
508 }
509 return DSAVarData();
510}
511
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000512template <class NamedDirectivesPredicate>
513bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
514 auto StartI = std::next(Stack.rbegin());
515 auto EndI = std::prev(Stack.rend());
516 if (FromParent && StartI != EndI) {
517 StartI = std::next(StartI);
518 }
519 for (auto I = StartI, EE = EndI; I != EE; ++I) {
520 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
521 return true;
522 }
523 return false;
524}
525
Alexey Bataev758e55e2013-09-06 18:03:48 +0000526void Sema::InitDataSharingAttributesStack() {
527 VarDataSharingAttributesStack = new DSAStackTy(*this);
528}
529
530#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
531
Alexey Bataeved09d242014-05-28 05:53:51 +0000532void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000533
534void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
535 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000536 Scope *CurScope, SourceLocation Loc) {
537 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000538 PushExpressionEvaluationContext(PotentiallyEvaluated);
539}
540
541void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000542 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
543 // A variable of class type (or array thereof) that appears in a lastprivate
544 // clause requires an accessible, unambiguous default constructor for the
545 // class type, unless the list item is also specified in a firstprivate
546 // clause.
547 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
548 for (auto C : D->clauses()) {
549 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
550 for (auto VarRef : Clause->varlists()) {
551 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
552 continue;
553 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000554 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000555 if (DVar.CKind == OMPC_lastprivate) {
556 SourceLocation ELoc = VarRef->getExprLoc();
557 auto Type = VarRef->getType();
558 if (Type->isArrayType())
559 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
560 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000561 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
562 // FIXME This code must be replaced by actual constructing of the
563 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000564 if (RD) {
565 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
566 PartialDiagnostic PD =
567 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
568 if (!CD ||
569 CheckConstructorAccess(
570 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
571 CD->getAccess(), PD) == AR_inaccessible ||
572 CD->isDeleted()) {
573 Diag(ELoc, diag::err_omp_required_method)
574 << getOpenMPClauseName(OMPC_lastprivate) << 0;
575 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
576 VarDecl::DeclarationOnly;
577 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
578 : diag::note_defined_here)
579 << VD;
580 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
581 continue;
582 }
583 MarkFunctionReferenced(ELoc, CD);
584 DiagnoseUseOfDecl(CD, ELoc);
585 }
586 }
587 }
588 }
589 }
590 }
591
Alexey Bataev758e55e2013-09-06 18:03:48 +0000592 DSAStack->pop();
593 DiscardCleanupsInEvaluationContext();
594 PopExpressionEvaluationContext();
595}
596
Alexey Bataeva769e072013-03-22 06:34:35 +0000597namespace {
598
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000599class VarDeclFilterCCC : public CorrectionCandidateCallback {
600private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000601 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000602
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000603public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000604 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000605 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000606 NamedDecl *ND = Candidate.getCorrectionDecl();
607 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
608 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000609 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
610 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000611 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000612 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614};
Alexey Bataeved09d242014-05-28 05:53:51 +0000615} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000616
617ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
618 CXXScopeSpec &ScopeSpec,
619 const DeclarationNameInfo &Id) {
620 LookupResult Lookup(*this, Id, LookupOrdinaryName);
621 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
622
623 if (Lookup.isAmbiguous())
624 return ExprError();
625
626 VarDecl *VD;
627 if (!Lookup.isSingleResult()) {
628 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000629 if (TypoCorrection Corrected =
630 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
631 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000632 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000633 PDiag(Lookup.empty()
634 ? diag::err_undeclared_var_use_suggest
635 : diag::err_omp_expected_var_arg_suggest)
636 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000637 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000638 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000639 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
640 : diag::err_omp_expected_var_arg)
641 << Id.getName();
642 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000643 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000644 } else {
645 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000646 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000647 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
648 return ExprError();
649 }
650 }
651 Lookup.suppressDiagnostics();
652
653 // OpenMP [2.9.2, Syntax, C/C++]
654 // Variables must be file-scope, namespace-scope, or static block-scope.
655 if (!VD->hasGlobalStorage()) {
656 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000657 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
658 bool IsDecl =
659 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000660 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000661 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
662 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000663 return ExprError();
664 }
665
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000666 VarDecl *CanonicalVD = VD->getCanonicalDecl();
667 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000668 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
669 // A threadprivate directive for file-scope variables must appear outside
670 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000671 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
672 !getCurLexicalContext()->isTranslationUnit()) {
673 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000674 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
675 bool IsDecl =
676 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
677 Diag(VD->getLocation(),
678 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
679 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000680 return ExprError();
681 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000682 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
683 // A threadprivate directive for static class member variables must appear
684 // in the class definition, in the same scope in which the member
685 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000686 if (CanonicalVD->isStaticDataMember() &&
687 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
688 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000689 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
690 bool IsDecl =
691 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
692 Diag(VD->getLocation(),
693 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
694 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000695 return ExprError();
696 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000697 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
698 // A threadprivate directive for namespace-scope variables must appear
699 // outside any definition or declaration other than the namespace
700 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000701 if (CanonicalVD->getDeclContext()->isNamespace() &&
702 (!getCurLexicalContext()->isFileContext() ||
703 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
704 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000705 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
706 bool IsDecl =
707 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
708 Diag(VD->getLocation(),
709 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
710 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000711 return ExprError();
712 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000713 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
714 // A threadprivate directive for static block-scope variables must appear
715 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000716 if (CanonicalVD->isStaticLocal() && CurScope &&
717 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000718 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000719 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
720 bool IsDecl =
721 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
722 Diag(VD->getLocation(),
723 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
724 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000725 return ExprError();
726 }
727
728 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
729 // A threadprivate directive must lexically precede all references to any
730 // of the variables in its list.
731 if (VD->isUsed()) {
732 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000733 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000734 return ExprError();
735 }
736
737 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000738 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000739 return DE;
740}
741
Alexey Bataeved09d242014-05-28 05:53:51 +0000742Sema::DeclGroupPtrTy
743Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
744 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000745 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000746 CurContext->addDecl(D);
747 return DeclGroupPtrTy::make(DeclGroupRef(D));
748 }
749 return DeclGroupPtrTy();
750}
751
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000752namespace {
753class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
754 Sema &SemaRef;
755
756public:
757 bool VisitDeclRefExpr(const DeclRefExpr *E) {
758 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
759 if (VD->hasLocalStorage()) {
760 SemaRef.Diag(E->getLocStart(),
761 diag::err_omp_local_var_in_threadprivate_init)
762 << E->getSourceRange();
763 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
764 << VD << VD->getSourceRange();
765 return true;
766 }
767 }
768 return false;
769 }
770 bool VisitStmt(const Stmt *S) {
771 for (auto Child : S->children()) {
772 if (Child && Visit(Child))
773 return true;
774 }
775 return false;
776 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000777 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000778};
779} // namespace
780
Alexey Bataeved09d242014-05-28 05:53:51 +0000781OMPThreadPrivateDecl *
782Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000783 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000784 for (auto &RefExpr : VarList) {
785 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000786 VarDecl *VD = cast<VarDecl>(DE->getDecl());
787 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000788
789 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
790 // A threadprivate variable must not have an incomplete type.
791 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000792 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000793 continue;
794 }
795
796 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
797 // A threadprivate variable must not have a reference type.
798 if (VD->getType()->isReferenceType()) {
799 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000800 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
801 bool IsDecl =
802 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
803 Diag(VD->getLocation(),
804 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
805 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000806 continue;
807 }
808
Richard Smithfd3834f2013-04-13 02:43:54 +0000809 // Check if this is a TLS variable.
810 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000811 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000812 bool IsDecl =
813 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
814 Diag(VD->getLocation(),
815 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
816 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000817 continue;
818 }
819
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000820 // Check if initial value of threadprivate variable reference variable with
821 // local storage (it is not supported by runtime).
822 if (auto Init = VD->getAnyInitializer()) {
823 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000824 if (Checker.Visit(Init))
825 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000826 }
827
Alexey Bataeved09d242014-05-28 05:53:51 +0000828 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000829 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000830 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000831 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000832 if (!Vars.empty()) {
833 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
834 Vars);
835 D->setAccess(AS_public);
836 }
837 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000838}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000839
Alexey Bataev7ff55242014-06-19 09:13:45 +0000840static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
841 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
842 bool IsLoopIterVar = false) {
843 if (DVar.RefExpr) {
844 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
845 << getOpenMPClauseName(DVar.CKind);
846 return;
847 }
848 enum {
849 PDSA_StaticMemberShared,
850 PDSA_StaticLocalVarShared,
851 PDSA_LoopIterVarPrivate,
852 PDSA_LoopIterVarLinear,
853 PDSA_LoopIterVarLastprivate,
854 PDSA_ConstVarShared,
855 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000856 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000857 PDSA_LocalVarPrivate,
858 PDSA_Implicit
859 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000860 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000861 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000862 if (IsLoopIterVar) {
863 if (DVar.CKind == OMPC_private)
864 Reason = PDSA_LoopIterVarPrivate;
865 else if (DVar.CKind == OMPC_lastprivate)
866 Reason = PDSA_LoopIterVarLastprivate;
867 else
868 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000869 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
870 Reason = PDSA_TaskVarFirstprivate;
871 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000872 } else if (VD->isStaticLocal())
873 Reason = PDSA_StaticLocalVarShared;
874 else if (VD->isStaticDataMember())
875 Reason = PDSA_StaticMemberShared;
876 else if (VD->isFileVarDecl())
877 Reason = PDSA_GlobalVarShared;
878 else if (VD->getType().isConstant(SemaRef.getASTContext()))
879 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000880 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000881 ReportHint = true;
882 Reason = PDSA_LocalVarPrivate;
883 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000884 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000885 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000886 << Reason << ReportHint
887 << getOpenMPDirectiveName(Stack->getCurrentDirective());
888 } else if (DVar.ImplicitDSALoc.isValid()) {
889 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
890 << getOpenMPClauseName(DVar.CKind);
891 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000892}
893
Alexey Bataev758e55e2013-09-06 18:03:48 +0000894namespace {
895class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
896 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000897 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000898 bool ErrorFound;
899 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000900 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000901 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000902
Alexey Bataev758e55e2013-09-06 18:03:48 +0000903public:
904 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000905 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000906 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000907 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
908 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000909
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000910 auto DVar = Stack->getTopDSA(VD, false);
911 // Check if the variable has explicit DSA set and stop analysis if it so.
912 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000913
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000914 auto ELoc = E->getExprLoc();
915 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000916 // The default(none) clause requires that each variable that is referenced
917 // in the construct, and does not have a predetermined data-sharing
918 // attribute, must have its data-sharing attribute explicitly determined
919 // by being listed in a data-sharing attribute clause.
920 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000921 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000922 VarsWithInheritedDSA.count(VD) == 0) {
923 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000924 return;
925 }
926
927 // OpenMP [2.9.3.6, Restrictions, p.2]
928 // A list item that appears in a reduction clause of the innermost
929 // enclosing worksharing or parallel construct may not be accessed in an
930 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000931 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000932 [](OpenMPDirectiveKind K) -> bool {
933 return isOpenMPParallelDirective(K) ||
934 isOpenMPWorksharingDirective(K);
935 },
936 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000937 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
938 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000939 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
940 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000941 return;
942 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000943
944 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000945 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000946 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000947 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000948 }
949 }
950 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000951 for (auto *C : S->clauses()) {
952 // Skip analysis of arguments of implicitly defined firstprivate clause
953 // for task directives.
954 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
955 for (auto *CC : C->children()) {
956 if (CC)
957 Visit(CC);
958 }
959 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000960 }
961 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000962 for (auto *C : S->children()) {
963 if (C && !isa<OMPExecutableDirective>(C))
964 Visit(C);
965 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000966 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000967
968 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000969 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000970 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
971 return VarsWithInheritedDSA;
972 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000973
Alexey Bataev7ff55242014-06-19 09:13:45 +0000974 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
975 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000976};
Alexey Bataeved09d242014-05-28 05:53:51 +0000977} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978
Alexey Bataevbae9a792014-06-27 10:37:06 +0000979void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000980 switch (DKind) {
981 case OMPD_parallel: {
982 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
983 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000984 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000985 std::make_pair(".global_tid.", KmpInt32PtrTy),
986 std::make_pair(".bound_tid.", KmpInt32PtrTy),
987 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000988 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000989 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
990 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000991 break;
992 }
993 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000994 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000995 std::make_pair(StringRef(), QualType()) // __context with shared vars
996 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000997 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
998 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000999 break;
1000 }
1001 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001002 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001003 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001004 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001005 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1006 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001007 break;
1008 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001009 case OMPD_sections: {
1010 Sema::CapturedParamNameType Params[] = {
1011 std::make_pair(StringRef(), QualType()) // __context with shared vars
1012 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001013 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1014 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001015 break;
1016 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001017 case OMPD_section: {
1018 Sema::CapturedParamNameType Params[] = {
1019 std::make_pair(StringRef(), QualType()) // __context with shared vars
1020 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001021 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1022 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001023 break;
1024 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001025 case OMPD_single: {
1026 Sema::CapturedParamNameType Params[] = {
1027 std::make_pair(StringRef(), QualType()) // __context with shared vars
1028 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001029 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1030 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001031 break;
1032 }
Alexander Musman80c22892014-07-17 08:54:58 +00001033 case OMPD_master: {
1034 Sema::CapturedParamNameType Params[] = {
1035 std::make_pair(StringRef(), QualType()) // __context with shared vars
1036 };
1037 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1038 Params);
1039 break;
1040 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001041 case OMPD_critical: {
1042 Sema::CapturedParamNameType Params[] = {
1043 std::make_pair(StringRef(), QualType()) // __context with shared vars
1044 };
1045 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1046 Params);
1047 break;
1048 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001049 case OMPD_parallel_for: {
1050 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1051 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1052 Sema::CapturedParamNameType Params[] = {
1053 std::make_pair(".global_tid.", KmpInt32PtrTy),
1054 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1055 std::make_pair(StringRef(), QualType()) // __context with shared vars
1056 };
1057 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1058 Params);
1059 break;
1060 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001061 case OMPD_parallel_sections: {
1062 Sema::CapturedParamNameType Params[] = {
1063 std::make_pair(StringRef(), QualType()) // __context with shared vars
1064 };
1065 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1066 Params);
1067 break;
1068 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001069 case OMPD_task: {
1070 Sema::CapturedParamNameType Params[] = {
1071 std::make_pair(StringRef(), QualType()) // __context with shared vars
1072 };
1073 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1074 Params);
1075 break;
1076 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001077 case OMPD_taskyield: {
1078 Sema::CapturedParamNameType Params[] = {
1079 std::make_pair(StringRef(), QualType()) // __context with shared vars
1080 };
1081 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1082 Params);
1083 break;
1084 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001085 case OMPD_barrier: {
1086 Sema::CapturedParamNameType Params[] = {
1087 std::make_pair(StringRef(), QualType()) // __context with shared vars
1088 };
1089 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1090 Params);
1091 break;
1092 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001093 case OMPD_taskwait: {
1094 Sema::CapturedParamNameType Params[] = {
1095 std::make_pair(StringRef(), QualType()) // __context with shared vars
1096 };
1097 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1098 Params);
1099 break;
1100 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001101 case OMPD_flush: {
1102 Sema::CapturedParamNameType Params[] = {
1103 std::make_pair(StringRef(), QualType()) // __context with shared vars
1104 };
1105 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1106 Params);
1107 break;
1108 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001109 case OMPD_ordered: {
1110 Sema::CapturedParamNameType Params[] = {
1111 std::make_pair(StringRef(), QualType()) // __context with shared vars
1112 };
1113 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1114 Params);
1115 break;
1116 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001117 case OMPD_atomic: {
1118 Sema::CapturedParamNameType Params[] = {
1119 std::make_pair(StringRef(), QualType()) // __context with shared vars
1120 };
1121 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1122 Params);
1123 break;
1124 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001125 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001126 llvm_unreachable("OpenMP Directive is not allowed");
1127 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001128 llvm_unreachable("Unknown OpenMP directive");
1129 }
1130}
1131
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001132static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1133 OpenMPDirectiveKind CurrentRegion,
1134 const DeclarationNameInfo &CurrentName,
1135 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001136 // Allowed nesting of constructs
1137 // +------------------+-----------------+------------------------------------+
1138 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1139 // +------------------+-----------------+------------------------------------+
1140 // | parallel | parallel | * |
1141 // | parallel | for | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001142 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001143 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001144 // | parallel | simd | * |
1145 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001146 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001147 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001148 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001149 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001150 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001151 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001152 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001153 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001154 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001155 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001156 // | parallel | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001157 // +------------------+-----------------+------------------------------------+
1158 // | for | parallel | * |
1159 // | for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001160 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001161 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001162 // | for | simd | * |
1163 // | for | sections | + |
1164 // | for | section | + |
1165 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001166 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001167 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001168 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001169 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001170 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001171 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001172 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001173 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001174 // | for | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001175 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001176 // | master | parallel | * |
1177 // | master | for | + |
1178 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001179 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001180 // | master | simd | * |
1181 // | master | sections | + |
1182 // | master | section | + |
1183 // | master | single | + |
1184 // | master | parallel for | * |
1185 // | master |parallel sections| * |
1186 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001187 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001188 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001189 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001190 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001191 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001192 // | master | atomic | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001193 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001194 // | critical | parallel | * |
1195 // | critical | for | + |
1196 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001197 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001198 // | critical | simd | * |
1199 // | critical | sections | + |
1200 // | critical | section | + |
1201 // | critical | single | + |
1202 // | critical | parallel for | * |
1203 // | critical |parallel sections| * |
1204 // | critical | task | * |
1205 // | critical | taskyield | * |
1206 // | critical | barrier | + |
1207 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001208 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001209 // | critical | atomic | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001210 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001211 // | simd | parallel | |
1212 // | simd | for | |
Alexander Musman80c22892014-07-17 08:54:58 +00001213 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001214 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001215 // | simd | simd | |
1216 // | simd | sections | |
1217 // | simd | section | |
1218 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001219 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001220 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001221 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001222 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001223 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001224 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001225 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001226 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001227 // | simd | atomic | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001228 // +------------------+-----------------+------------------------------------+
1229 // | sections | parallel | * |
1230 // | sections | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001231 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001232 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001233 // | sections | simd | * |
1234 // | sections | sections | + |
1235 // | sections | section | * |
1236 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001237 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001238 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001239 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001240 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001241 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001242 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001243 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001244 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001245 // | sections | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001246 // +------------------+-----------------+------------------------------------+
1247 // | section | parallel | * |
1248 // | section | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001249 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001250 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001251 // | section | simd | * |
1252 // | section | sections | + |
1253 // | section | section | + |
1254 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001255 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001256 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001257 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001258 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001259 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001260 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001261 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001262 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001263 // | section | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001264 // +------------------+-----------------+------------------------------------+
1265 // | single | parallel | * |
1266 // | single | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001267 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001268 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001269 // | single | simd | * |
1270 // | single | sections | + |
1271 // | single | section | + |
1272 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001273 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001274 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001275 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001276 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001277 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001278 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001279 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001280 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001281 // | single | atomic | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001282 // +------------------+-----------------+------------------------------------+
1283 // | parallel for | parallel | * |
1284 // | parallel for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001285 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001286 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001287 // | parallel for | simd | * |
1288 // | parallel for | sections | + |
1289 // | parallel for | section | + |
1290 // | parallel for | single | + |
1291 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001292 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001293 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001294 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001295 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001296 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001297 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001298 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001299 // | parallel for | atomic | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001300 // +------------------+-----------------+------------------------------------+
1301 // | parallel sections| parallel | * |
1302 // | parallel sections| for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001303 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001304 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001305 // | parallel sections| simd | * |
1306 // | parallel sections| sections | + |
1307 // | parallel sections| section | * |
1308 // | parallel sections| single | + |
1309 // | parallel sections| parallel for | * |
1310 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001311 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001312 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001313 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001314 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001315 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001316 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001317 // | parallel sections| atomic | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001318 // +------------------+-----------------+------------------------------------+
1319 // | task | parallel | * |
1320 // | task | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001321 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001322 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001323 // | task | simd | * |
1324 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001325 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001326 // | task | single | + |
1327 // | task | parallel for | * |
1328 // | task |parallel sections| * |
1329 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001330 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001331 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001332 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001333 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001334 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001335 // | task | atomic | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001336 // +------------------+-----------------+------------------------------------+
1337 // | ordered | parallel | * |
1338 // | ordered | for | + |
1339 // | ordered | master | * |
1340 // | ordered | critical | * |
1341 // | ordered | simd | * |
1342 // | ordered | sections | + |
1343 // | ordered | section | + |
1344 // | ordered | single | + |
1345 // | ordered | parallel for | * |
1346 // | ordered |parallel sections| * |
1347 // | ordered | task | * |
1348 // | ordered | taskyield | * |
1349 // | ordered | barrier | + |
1350 // | ordered | taskwait | * |
1351 // | ordered | flush | * |
1352 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001353 // | ordered | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001354 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001355 if (Stack->getCurScope()) {
1356 auto ParentRegion = Stack->getParentDirective();
1357 bool NestingProhibited = false;
1358 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001359 enum {
1360 NoRecommend,
1361 ShouldBeInParallelRegion,
1362 ShouldBeInOrderedRegion
1363 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001364 if (isOpenMPSimdDirective(ParentRegion)) {
1365 // OpenMP [2.16, Nesting of Regions]
1366 // OpenMP constructs may not be nested inside a simd region.
1367 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1368 return true;
1369 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001370 if (ParentRegion == OMPD_atomic) {
1371 // OpenMP [2.16, Nesting of Regions]
1372 // OpenMP constructs may not be nested inside an atomic region.
1373 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1374 return true;
1375 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001376 if (CurrentRegion == OMPD_section) {
1377 // OpenMP [2.7.2, sections Construct, Restrictions]
1378 // Orphaned section directives are prohibited. That is, the section
1379 // directives must appear within the sections construct and must not be
1380 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001381 if (ParentRegion != OMPD_sections &&
1382 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001383 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1384 << (ParentRegion != OMPD_unknown)
1385 << getOpenMPDirectiveName(ParentRegion);
1386 return true;
1387 }
1388 return false;
1389 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001390 // Allow some constructs to be orphaned (they could be used in functions,
1391 // called from OpenMP regions with the required preconditions).
1392 if (ParentRegion == OMPD_unknown)
1393 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001394 if (CurrentRegion == OMPD_master) {
1395 // OpenMP [2.16, Nesting of Regions]
1396 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001397 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001398 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1399 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001400 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1401 // OpenMP [2.16, Nesting of Regions]
1402 // A critical region may not be nested (closely or otherwise) inside a
1403 // critical region with the same name. Note that this restriction is not
1404 // sufficient to prevent deadlock.
1405 SourceLocation PreviousCriticalLoc;
1406 bool DeadLock =
1407 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1408 OpenMPDirectiveKind K,
1409 const DeclarationNameInfo &DNI,
1410 SourceLocation Loc)
1411 ->bool {
1412 if (K == OMPD_critical &&
1413 DNI.getName() == CurrentName.getName()) {
1414 PreviousCriticalLoc = Loc;
1415 return true;
1416 } else
1417 return false;
1418 },
1419 false /* skip top directive */);
1420 if (DeadLock) {
1421 SemaRef.Diag(StartLoc,
1422 diag::err_omp_prohibited_region_critical_same_name)
1423 << CurrentName.getName();
1424 if (PreviousCriticalLoc.isValid())
1425 SemaRef.Diag(PreviousCriticalLoc,
1426 diag::note_omp_previous_critical_region);
1427 return true;
1428 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001429 } else if (CurrentRegion == OMPD_barrier) {
1430 // OpenMP [2.16, Nesting of Regions]
1431 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001432 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001433 NestingProhibited =
1434 isOpenMPWorksharingDirective(ParentRegion) ||
1435 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1436 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001437 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1438 !isOpenMPParallelDirective(CurrentRegion) &&
1439 !isOpenMPSimdDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001440 // OpenMP [2.16, Nesting of Regions]
1441 // A worksharing region may not be closely nested inside a worksharing,
1442 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001443 NestingProhibited =
1444 (isOpenMPWorksharingDirective(ParentRegion) &&
1445 !isOpenMPSimdDirective(ParentRegion)) ||
1446 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1447 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1448 Recommend = ShouldBeInParallelRegion;
1449 } else if (CurrentRegion == OMPD_ordered) {
1450 // OpenMP [2.16, Nesting of Regions]
1451 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001452 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001453 // An ordered region must be closely nested inside a loop region (or
1454 // parallel loop region) with an ordered clause.
1455 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001456 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001457 !Stack->isParentOrderedRegion();
1458 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001459 }
1460 if (NestingProhibited) {
1461 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001462 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1463 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001464 return true;
1465 }
1466 }
1467 return false;
1468}
1469
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001470StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001471 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001472 ArrayRef<OMPClause *> Clauses,
1473 Stmt *AStmt,
1474 SourceLocation StartLoc,
1475 SourceLocation EndLoc) {
1476 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001477 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001478 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001479
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001480 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001481 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001482 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001483 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001484 if (AStmt) {
1485 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1486
1487 // Check default data sharing attributes for referenced variables.
1488 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1489 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1490 if (DSAChecker.isErrorFound())
1491 return StmtError();
1492 // Generate list of implicitly defined firstprivate variables.
1493 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001494
1495 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1496 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1497 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1498 SourceLocation(), SourceLocation())) {
1499 ClausesWithImplicit.push_back(Implicit);
1500 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1501 DSAChecker.getImplicitFirstprivate().size();
1502 } else
1503 ErrorFound = true;
1504 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001505 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001506
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001507 switch (Kind) {
1508 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001509 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1510 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001511 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001512 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001513 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1514 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001515 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001516 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001517 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1518 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001519 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001520 case OMPD_sections:
1521 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1522 EndLoc);
1523 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001524 case OMPD_section:
1525 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001526 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001527 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1528 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001529 case OMPD_single:
1530 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1531 EndLoc);
1532 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001533 case OMPD_master:
1534 assert(ClausesWithImplicit.empty() &&
1535 "No clauses are allowed for 'omp master' directive");
1536 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1537 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001538 case OMPD_critical:
1539 assert(ClausesWithImplicit.empty() &&
1540 "No clauses are allowed for 'omp critical' directive");
1541 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1542 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001543 case OMPD_parallel_for:
1544 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1545 EndLoc, VarsWithInheritedDSA);
1546 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001547 case OMPD_parallel_sections:
1548 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1549 StartLoc, EndLoc);
1550 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001551 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001552 Res =
1553 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1554 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001555 case OMPD_taskyield:
1556 assert(ClausesWithImplicit.empty() &&
1557 "No clauses are allowed for 'omp taskyield' directive");
1558 assert(AStmt == nullptr &&
1559 "No associated statement allowed for 'omp taskyield' directive");
1560 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1561 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001562 case OMPD_barrier:
1563 assert(ClausesWithImplicit.empty() &&
1564 "No clauses are allowed for 'omp barrier' directive");
1565 assert(AStmt == nullptr &&
1566 "No associated statement allowed for 'omp barrier' directive");
1567 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1568 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001569 case OMPD_taskwait:
1570 assert(ClausesWithImplicit.empty() &&
1571 "No clauses are allowed for 'omp taskwait' directive");
1572 assert(AStmt == nullptr &&
1573 "No associated statement allowed for 'omp taskwait' directive");
1574 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1575 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001576 case OMPD_flush:
1577 assert(AStmt == nullptr &&
1578 "No associated statement allowed for 'omp flush' directive");
1579 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1580 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001581 case OMPD_ordered:
1582 assert(ClausesWithImplicit.empty() &&
1583 "No clauses are allowed for 'omp ordered' directive");
1584 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1585 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001586 case OMPD_atomic:
1587 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1588 EndLoc);
1589 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001590 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001591 llvm_unreachable("OpenMP Directive is not allowed");
1592 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001593 llvm_unreachable("Unknown OpenMP directive");
1594 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001595
Alexey Bataev4acb8592014-07-07 13:01:15 +00001596 for (auto P : VarsWithInheritedDSA) {
1597 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1598 << P.first << P.second->getSourceRange();
1599 }
1600 if (!VarsWithInheritedDSA.empty())
1601 return StmtError();
1602
Alexey Bataeved09d242014-05-28 05:53:51 +00001603 if (ErrorFound)
1604 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001605 return Res;
1606}
1607
1608StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1609 Stmt *AStmt,
1610 SourceLocation StartLoc,
1611 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001612 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1613 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1614 // 1.2.2 OpenMP Language Terminology
1615 // Structured block - An executable statement with a single entry at the
1616 // top and a single exit at the bottom.
1617 // The point of exit cannot be a branch out of the structured block.
1618 // longjmp() and throw() must not violate the entry/exit criteria.
1619 CS->getCapturedDecl()->setNothrow();
1620
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001621 getCurFunction()->setHasBranchProtectedScope();
1622
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001623 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1624 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001625}
1626
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001627namespace {
1628/// \brief Helper class for checking canonical form of the OpenMP loops and
1629/// extracting iteration space of each loop in the loop nest, that will be used
1630/// for IR generation.
1631class OpenMPIterationSpaceChecker {
1632 /// \brief Reference to Sema.
1633 Sema &SemaRef;
1634 /// \brief A location for diagnostics (when there is no some better location).
1635 SourceLocation DefaultLoc;
1636 /// \brief A location for diagnostics (when increment is not compatible).
1637 SourceLocation ConditionLoc;
1638 /// \brief A source location for referring to condition later.
1639 SourceRange ConditionSrcRange;
1640 /// \brief Loop variable.
1641 VarDecl *Var;
1642 /// \brief Lower bound (initializer for the var).
1643 Expr *LB;
1644 /// \brief Upper bound.
1645 Expr *UB;
1646 /// \brief Loop step (increment).
1647 Expr *Step;
1648 /// \brief This flag is true when condition is one of:
1649 /// Var < UB
1650 /// Var <= UB
1651 /// UB > Var
1652 /// UB >= Var
1653 bool TestIsLessOp;
1654 /// \brief This flag is true when condition is strict ( < or > ).
1655 bool TestIsStrictOp;
1656 /// \brief This flag is true when step is subtracted on each iteration.
1657 bool SubtractStep;
1658
1659public:
1660 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1661 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1662 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1663 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1664 SubtractStep(false) {}
1665 /// \brief Check init-expr for canonical loop form and save loop counter
1666 /// variable - #Var and its initialization value - #LB.
1667 bool CheckInit(Stmt *S);
1668 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1669 /// for less/greater and for strict/non-strict comparison.
1670 bool CheckCond(Expr *S);
1671 /// \brief Check incr-expr for canonical loop form and return true if it
1672 /// does not conform, otherwise save loop step (#Step).
1673 bool CheckInc(Expr *S);
1674 /// \brief Return the loop counter variable.
1675 VarDecl *GetLoopVar() const { return Var; }
1676 /// \brief Return true if any expression is dependent.
1677 bool Dependent() const;
1678
1679private:
1680 /// \brief Check the right-hand side of an assignment in the increment
1681 /// expression.
1682 bool CheckIncRHS(Expr *RHS);
1683 /// \brief Helper to set loop counter variable and its initializer.
1684 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1685 /// \brief Helper to set upper bound.
1686 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1687 const SourceLocation &SL);
1688 /// \brief Helper to set loop increment.
1689 bool SetStep(Expr *NewStep, bool Subtract);
1690};
1691
1692bool OpenMPIterationSpaceChecker::Dependent() const {
1693 if (!Var) {
1694 assert(!LB && !UB && !Step);
1695 return false;
1696 }
1697 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1698 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1699}
1700
1701bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1702 // State consistency checking to ensure correct usage.
1703 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1704 !TestIsLessOp && !TestIsStrictOp);
1705 if (!NewVar || !NewLB)
1706 return true;
1707 Var = NewVar;
1708 LB = NewLB;
1709 return false;
1710}
1711
1712bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1713 const SourceRange &SR,
1714 const SourceLocation &SL) {
1715 // State consistency checking to ensure correct usage.
1716 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1717 !TestIsLessOp && !TestIsStrictOp);
1718 if (!NewUB)
1719 return true;
1720 UB = NewUB;
1721 TestIsLessOp = LessOp;
1722 TestIsStrictOp = StrictOp;
1723 ConditionSrcRange = SR;
1724 ConditionLoc = SL;
1725 return false;
1726}
1727
1728bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1729 // State consistency checking to ensure correct usage.
1730 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1731 if (!NewStep)
1732 return true;
1733 if (!NewStep->isValueDependent()) {
1734 // Check that the step is integer expression.
1735 SourceLocation StepLoc = NewStep->getLocStart();
1736 ExprResult Val =
1737 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1738 if (Val.isInvalid())
1739 return true;
1740 NewStep = Val.get();
1741
1742 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1743 // If test-expr is of form var relational-op b and relational-op is < or
1744 // <= then incr-expr must cause var to increase on each iteration of the
1745 // loop. If test-expr is of form var relational-op b and relational-op is
1746 // > or >= then incr-expr must cause var to decrease on each iteration of
1747 // the loop.
1748 // If test-expr is of form b relational-op var and relational-op is < or
1749 // <= then incr-expr must cause var to decrease on each iteration of the
1750 // loop. If test-expr is of form b relational-op var and relational-op is
1751 // > or >= then incr-expr must cause var to increase on each iteration of
1752 // the loop.
1753 llvm::APSInt Result;
1754 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1755 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1756 bool IsConstNeg =
1757 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1758 bool IsConstZero = IsConstant && !Result.getBoolValue();
1759 if (UB && (IsConstZero ||
1760 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1761 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1762 SemaRef.Diag(NewStep->getExprLoc(),
1763 diag::err_omp_loop_incr_not_compatible)
1764 << Var << TestIsLessOp << NewStep->getSourceRange();
1765 SemaRef.Diag(ConditionLoc,
1766 diag::note_omp_loop_cond_requres_compatible_incr)
1767 << TestIsLessOp << ConditionSrcRange;
1768 return true;
1769 }
1770 }
1771
1772 Step = NewStep;
1773 SubtractStep = Subtract;
1774 return false;
1775}
1776
1777bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1778 // Check init-expr for canonical loop form and save loop counter
1779 // variable - #Var and its initialization value - #LB.
1780 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1781 // var = lb
1782 // integer-type var = lb
1783 // random-access-iterator-type var = lb
1784 // pointer-type var = lb
1785 //
1786 if (!S) {
1787 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1788 return true;
1789 }
1790 if (Expr *E = dyn_cast<Expr>(S))
1791 S = E->IgnoreParens();
1792 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1793 if (BO->getOpcode() == BO_Assign)
1794 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1795 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1796 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1797 if (DS->isSingleDecl()) {
1798 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1799 if (Var->hasInit()) {
1800 // Accept non-canonical init form here but emit ext. warning.
1801 if (Var->getInitStyle() != VarDecl::CInit)
1802 SemaRef.Diag(S->getLocStart(),
1803 diag::ext_omp_loop_not_canonical_init)
1804 << S->getSourceRange();
1805 return SetVarAndLB(Var, Var->getInit());
1806 }
1807 }
1808 }
1809 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1810 if (CE->getOperator() == OO_Equal)
1811 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1812 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1813
1814 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1815 << S->getSourceRange();
1816 return true;
1817}
1818
Alexey Bataev23b69422014-06-18 07:08:49 +00001819/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001820/// variable (which may be the loop variable) if possible.
1821static const VarDecl *GetInitVarDecl(const Expr *E) {
1822 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001823 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001824 E = E->IgnoreParenImpCasts();
1825 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1826 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1827 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1828 CE->getArg(0) != nullptr)
1829 E = CE->getArg(0)->IgnoreParenImpCasts();
1830 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1831 if (!DRE)
1832 return nullptr;
1833 return dyn_cast<VarDecl>(DRE->getDecl());
1834}
1835
1836bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1837 // Check test-expr for canonical form, save upper-bound UB, flags for
1838 // less/greater and for strict/non-strict comparison.
1839 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1840 // var relational-op b
1841 // b relational-op var
1842 //
1843 if (!S) {
1844 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1845 return true;
1846 }
1847 S = S->IgnoreParenImpCasts();
1848 SourceLocation CondLoc = S->getLocStart();
1849 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1850 if (BO->isRelationalOp()) {
1851 if (GetInitVarDecl(BO->getLHS()) == Var)
1852 return SetUB(BO->getRHS(),
1853 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1854 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1855 BO->getSourceRange(), BO->getOperatorLoc());
1856 if (GetInitVarDecl(BO->getRHS()) == Var)
1857 return SetUB(BO->getLHS(),
1858 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1859 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1860 BO->getSourceRange(), BO->getOperatorLoc());
1861 }
1862 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1863 if (CE->getNumArgs() == 2) {
1864 auto Op = CE->getOperator();
1865 switch (Op) {
1866 case OO_Greater:
1867 case OO_GreaterEqual:
1868 case OO_Less:
1869 case OO_LessEqual:
1870 if (GetInitVarDecl(CE->getArg(0)) == Var)
1871 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1872 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1873 CE->getOperatorLoc());
1874 if (GetInitVarDecl(CE->getArg(1)) == Var)
1875 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1876 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1877 CE->getOperatorLoc());
1878 break;
1879 default:
1880 break;
1881 }
1882 }
1883 }
1884 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1885 << S->getSourceRange() << Var;
1886 return true;
1887}
1888
1889bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1890 // RHS of canonical loop form increment can be:
1891 // var + incr
1892 // incr + var
1893 // var - incr
1894 //
1895 RHS = RHS->IgnoreParenImpCasts();
1896 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1897 if (BO->isAdditiveOp()) {
1898 bool IsAdd = BO->getOpcode() == BO_Add;
1899 if (GetInitVarDecl(BO->getLHS()) == Var)
1900 return SetStep(BO->getRHS(), !IsAdd);
1901 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1902 return SetStep(BO->getLHS(), false);
1903 }
1904 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1905 bool IsAdd = CE->getOperator() == OO_Plus;
1906 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1907 if (GetInitVarDecl(CE->getArg(0)) == Var)
1908 return SetStep(CE->getArg(1), !IsAdd);
1909 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1910 return SetStep(CE->getArg(0), false);
1911 }
1912 }
1913 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1914 << RHS->getSourceRange() << Var;
1915 return true;
1916}
1917
1918bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1919 // Check incr-expr for canonical loop form and return true if it
1920 // does not conform.
1921 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1922 // ++var
1923 // var++
1924 // --var
1925 // var--
1926 // var += incr
1927 // var -= incr
1928 // var = var + incr
1929 // var = incr + var
1930 // var = var - incr
1931 //
1932 if (!S) {
1933 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1934 return true;
1935 }
1936 S = S->IgnoreParens();
1937 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1938 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1939 return SetStep(
1940 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1941 (UO->isDecrementOp() ? -1 : 1)).get(),
1942 false);
1943 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1944 switch (BO->getOpcode()) {
1945 case BO_AddAssign:
1946 case BO_SubAssign:
1947 if (GetInitVarDecl(BO->getLHS()) == Var)
1948 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1949 break;
1950 case BO_Assign:
1951 if (GetInitVarDecl(BO->getLHS()) == Var)
1952 return CheckIncRHS(BO->getRHS());
1953 break;
1954 default:
1955 break;
1956 }
1957 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1958 switch (CE->getOperator()) {
1959 case OO_PlusPlus:
1960 case OO_MinusMinus:
1961 if (GetInitVarDecl(CE->getArg(0)) == Var)
1962 return SetStep(
1963 SemaRef.ActOnIntegerConstant(
1964 CE->getLocStart(),
1965 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1966 false);
1967 break;
1968 case OO_PlusEqual:
1969 case OO_MinusEqual:
1970 if (GetInitVarDecl(CE->getArg(0)) == Var)
1971 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1972 break;
1973 case OO_Equal:
1974 if (GetInitVarDecl(CE->getArg(0)) == Var)
1975 return CheckIncRHS(CE->getArg(1));
1976 break;
1977 default:
1978 break;
1979 }
1980 }
1981 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1982 << S->getSourceRange() << Var;
1983 return true;
1984}
Alexey Bataev23b69422014-06-18 07:08:49 +00001985} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001986
1987/// \brief Called on a for stmt to check and extract its iteration space
1988/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00001989static bool CheckOpenMPIterationSpace(
1990 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
1991 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
1992 Expr *NestedLoopCountExpr,
1993 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001994 // OpenMP [2.6, Canonical Loop Form]
1995 // for (init-expr; test-expr; incr-expr) structured-block
1996 auto For = dyn_cast_or_null<ForStmt>(S);
1997 if (!For) {
1998 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001999 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2000 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2001 << CurrentNestedLoopCount;
2002 if (NestedLoopCount > 1)
2003 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2004 diag::note_omp_collapse_expr)
2005 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002006 return true;
2007 }
2008 assert(For->getBody());
2009
2010 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2011
2012 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002013 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002014 if (ISC.CheckInit(Init)) {
2015 return true;
2016 }
2017
2018 bool HasErrors = false;
2019
2020 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002021 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002022
2023 // OpenMP [2.6, Canonical Loop Form]
2024 // Var is one of the following:
2025 // A variable of signed or unsigned integer type.
2026 // For C++, a variable of a random access iterator type.
2027 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002028 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002029 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2030 !VarType->isPointerType() &&
2031 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2032 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2033 << SemaRef.getLangOpts().CPlusPlus;
2034 HasErrors = true;
2035 }
2036
Alexey Bataev4acb8592014-07-07 13:01:15 +00002037 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2038 // Construct
2039 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2040 // parallel for construct is (are) private.
2041 // The loop iteration variable in the associated for-loop of a simd construct
2042 // with just one associated for-loop is linear with a constant-linear-step
2043 // that is the increment of the associated for-loop.
2044 // Exclude loop var from the list of variables with implicitly defined data
2045 // sharing attributes.
2046 while (VarsWithImplicitDSA.count(Var) > 0)
2047 VarsWithImplicitDSA.erase(Var);
2048
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002049 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2050 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002051 // The loop iteration variable in the associated for-loop of a simd construct
2052 // with just one associated for-loop may be listed in a linear clause with a
2053 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002054 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2055 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002056 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002057 auto PredeterminedCKind =
2058 isOpenMPSimdDirective(DKind)
2059 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2060 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002061 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002062 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00002063 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
2064 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002065 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002066 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002067 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2068 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002069 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002070 HasErrors = true;
2071 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002072 // Make the loop iteration variable private (for worksharing constructs),
2073 // linear (for simd directives with the only one associated loop) or
2074 // lastprivate (for simd directives with several collapsed loops).
2075 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002076 }
2077
Alexey Bataev7ff55242014-06-19 09:13:45 +00002078 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002079
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002080 // Check test-expr.
2081 HasErrors |= ISC.CheckCond(For->getCond());
2082
2083 // Check incr-expr.
2084 HasErrors |= ISC.CheckInc(For->getInc());
2085
2086 if (ISC.Dependent())
2087 return HasErrors;
2088
2089 // FIXME: Build loop's iteration space representation.
2090 return HasErrors;
2091}
2092
2093/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
2094/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
2095/// to get the first for loop.
2096static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
2097 if (IgnoreCaptured)
2098 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
2099 S = CapS->getCapturedStmt();
2100 // OpenMP [2.8.1, simd construct, Restrictions]
2101 // All loops associated with the construct must be perfectly nested; that is,
2102 // there must be no intervening code nor any OpenMP directive between any two
2103 // loops.
2104 while (true) {
2105 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
2106 S = AS->getSubStmt();
2107 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
2108 if (CS->size() != 1)
2109 break;
2110 S = CS->body_back();
2111 } else
2112 break;
2113 }
2114 return S;
2115}
2116
2117/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002118/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2119/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002120static unsigned
2121CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2122 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2123 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002124 unsigned NestedLoopCount = 1;
2125 if (NestedLoopCountExpr) {
2126 // Found 'collapse' clause - calculate collapse number.
2127 llvm::APSInt Result;
2128 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2129 NestedLoopCount = Result.getLimitedValue();
2130 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002131 // This is helper routine for loop directives (e.g., 'for', 'simd',
2132 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002133 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
2134 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002135 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002136 NestedLoopCount, NestedLoopCountExpr,
2137 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002138 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002139 // Move on to the next nested for loop, or to the loop body.
2140 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
2141 }
2142
2143 // FIXME: Build resulting iteration space for IR generation (collapsing
2144 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002145 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002146}
2147
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002148static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002149 auto CollapseFilter = [](const OMPClause *C) -> bool {
2150 return C->getClauseKind() == OMPC_collapse;
2151 };
2152 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2153 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002154 if (I)
2155 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2156 return nullptr;
2157}
2158
Alexey Bataev4acb8592014-07-07 13:01:15 +00002159StmtResult Sema::ActOnOpenMPSimdDirective(
2160 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2161 SourceLocation EndLoc,
2162 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002163 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002164 unsigned NestedLoopCount =
2165 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
2166 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002167 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002168 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002169
2170 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002171 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2172 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002173}
2174
Alexey Bataev4acb8592014-07-07 13:01:15 +00002175StmtResult Sema::ActOnOpenMPForDirective(
2176 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2177 SourceLocation EndLoc,
2178 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002179 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002180 unsigned NestedLoopCount =
2181 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
2182 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002183 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002184 return StmtError();
2185
2186 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002187 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2188 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002189}
2190
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002191StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2192 Stmt *AStmt,
2193 SourceLocation StartLoc,
2194 SourceLocation EndLoc) {
2195 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2196 auto BaseStmt = AStmt;
2197 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2198 BaseStmt = CS->getCapturedStmt();
2199 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2200 auto S = C->children();
2201 if (!S)
2202 return StmtError();
2203 // All associated statements must be '#pragma omp section' except for
2204 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002205 for (++S; S; ++S) {
2206 auto SectionStmt = *S;
2207 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2208 if (SectionStmt)
2209 Diag(SectionStmt->getLocStart(),
2210 diag::err_omp_sections_substmt_not_section);
2211 return StmtError();
2212 }
2213 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002214 } else {
2215 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2216 return StmtError();
2217 }
2218
2219 getCurFunction()->setHasBranchProtectedScope();
2220
2221 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2222 AStmt);
2223}
2224
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002225StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2226 SourceLocation StartLoc,
2227 SourceLocation EndLoc) {
2228 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2229
2230 getCurFunction()->setHasBranchProtectedScope();
2231
2232 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
2233}
2234
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002235StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
2236 Stmt *AStmt,
2237 SourceLocation StartLoc,
2238 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00002239 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2240
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002241 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00002242
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002243 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2244}
2245
Alexander Musman80c22892014-07-17 08:54:58 +00002246StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
2247 SourceLocation StartLoc,
2248 SourceLocation EndLoc) {
2249 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2250
2251 getCurFunction()->setHasBranchProtectedScope();
2252
2253 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
2254}
2255
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002256StmtResult
2257Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
2258 Stmt *AStmt, SourceLocation StartLoc,
2259 SourceLocation EndLoc) {
2260 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2261
2262 getCurFunction()->setHasBranchProtectedScope();
2263
2264 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
2265 AStmt);
2266}
2267
Alexey Bataev4acb8592014-07-07 13:01:15 +00002268StmtResult Sema::ActOnOpenMPParallelForDirective(
2269 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2270 SourceLocation EndLoc,
2271 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2272 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2273 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2274 // 1.2.2 OpenMP Language Terminology
2275 // Structured block - An executable statement with a single entry at the
2276 // top and a single exit at the bottom.
2277 // The point of exit cannot be a branch out of the structured block.
2278 // longjmp() and throw() must not violate the entry/exit criteria.
2279 CS->getCapturedDecl()->setNothrow();
2280
2281 // In presence of clause 'collapse', it will define the nested loops number.
2282 unsigned NestedLoopCount =
2283 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
2284 *this, *DSAStack, VarsWithImplicitDSA);
2285 if (NestedLoopCount == 0)
2286 return StmtError();
2287
2288 getCurFunction()->setHasBranchProtectedScope();
2289 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
2290 NestedLoopCount, Clauses, AStmt);
2291}
2292
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002293StmtResult
2294Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2295 Stmt *AStmt, SourceLocation StartLoc,
2296 SourceLocation EndLoc) {
2297 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2298 auto BaseStmt = AStmt;
2299 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2300 BaseStmt = CS->getCapturedStmt();
2301 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2302 auto S = C->children();
2303 if (!S)
2304 return StmtError();
2305 // All associated statements must be '#pragma omp section' except for
2306 // the first one.
2307 for (++S; S; ++S) {
2308 auto SectionStmt = *S;
2309 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2310 if (SectionStmt)
2311 Diag(SectionStmt->getLocStart(),
2312 diag::err_omp_parallel_sections_substmt_not_section);
2313 return StmtError();
2314 }
2315 }
2316 } else {
2317 Diag(AStmt->getLocStart(),
2318 diag::err_omp_parallel_sections_not_compound_stmt);
2319 return StmtError();
2320 }
2321
2322 getCurFunction()->setHasBranchProtectedScope();
2323
2324 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2325 Clauses, AStmt);
2326}
2327
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002328StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2329 Stmt *AStmt, SourceLocation StartLoc,
2330 SourceLocation EndLoc) {
2331 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2332 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2333 // 1.2.2 OpenMP Language Terminology
2334 // Structured block - An executable statement with a single entry at the
2335 // top and a single exit at the bottom.
2336 // The point of exit cannot be a branch out of the structured block.
2337 // longjmp() and throw() must not violate the entry/exit criteria.
2338 CS->getCapturedDecl()->setNothrow();
2339
2340 getCurFunction()->setHasBranchProtectedScope();
2341
2342 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2343}
2344
Alexey Bataev68446b72014-07-18 07:47:19 +00002345StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
2346 SourceLocation EndLoc) {
2347 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
2348}
2349
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002350StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
2351 SourceLocation EndLoc) {
2352 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
2353}
2354
Alexey Bataev2df347a2014-07-18 10:17:07 +00002355StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
2356 SourceLocation EndLoc) {
2357 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
2358}
2359
Alexey Bataev6125da92014-07-21 11:26:11 +00002360StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
2361 SourceLocation StartLoc,
2362 SourceLocation EndLoc) {
2363 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
2364 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
2365}
2366
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002367StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
2368 SourceLocation StartLoc,
2369 SourceLocation EndLoc) {
2370 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2371
2372 getCurFunction()->setHasBranchProtectedScope();
2373
2374 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
2375}
2376
Alexey Bataev0162e452014-07-22 10:10:35 +00002377StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
2378 Stmt *AStmt,
2379 SourceLocation StartLoc,
2380 SourceLocation EndLoc) {
2381 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2382 // 1.2.2 OpenMP Language Terminology
2383 // Structured block - An executable statement with a single entry at the
2384 // top and a single exit at the bottom.
2385 // The point of exit cannot be a branch out of the structured block.
2386 // longjmp() and throw() must not violate the entry/exit criteria.
2387 // TODO further analysis of associated statements and clauses.
2388
2389 getCurFunction()->setHasBranchProtectedScope();
2390
2391 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2392}
2393
Alexey Bataeved09d242014-05-28 05:53:51 +00002394OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002395 SourceLocation StartLoc,
2396 SourceLocation LParenLoc,
2397 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002398 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002399 switch (Kind) {
2400 case OMPC_if:
2401 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2402 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002403 case OMPC_final:
2404 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2405 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002406 case OMPC_num_threads:
2407 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2408 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002409 case OMPC_safelen:
2410 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2411 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002412 case OMPC_collapse:
2413 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2414 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002415 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002416 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002417 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002418 case OMPC_private:
2419 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002420 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002421 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002422 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002423 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002424 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002425 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002426 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002427 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002428 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002429 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002430 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002431 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002432 case OMPC_flush:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002433 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002434 llvm_unreachable("Clause is not allowed.");
2435 }
2436 return Res;
2437}
2438
Alexey Bataeved09d242014-05-28 05:53:51 +00002439OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002440 SourceLocation LParenLoc,
2441 SourceLocation EndLoc) {
2442 Expr *ValExpr = Condition;
2443 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2444 !Condition->isInstantiationDependent() &&
2445 !Condition->containsUnexpandedParameterPack()) {
2446 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002447 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002448 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002449 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002450
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002451 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002452 }
2453
2454 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2455}
2456
Alexey Bataev3778b602014-07-17 07:32:53 +00002457OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2458 SourceLocation StartLoc,
2459 SourceLocation LParenLoc,
2460 SourceLocation EndLoc) {
2461 Expr *ValExpr = Condition;
2462 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2463 !Condition->isInstantiationDependent() &&
2464 !Condition->containsUnexpandedParameterPack()) {
2465 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2466 Condition->getExprLoc(), Condition);
2467 if (Val.isInvalid())
2468 return nullptr;
2469
2470 ValExpr = Val.get();
2471 }
2472
2473 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2474}
2475
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002476ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2477 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002478 if (!Op)
2479 return ExprError();
2480
2481 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2482 public:
2483 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002484 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002485 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2486 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002487 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2488 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002489 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2490 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002491 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2492 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002493 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2494 QualType T,
2495 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002496 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2497 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002498 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2499 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002500 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002501 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002502 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002503 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2504 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002505 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2506 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002507 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2508 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002509 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002510 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002511 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002512 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2513 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002514 llvm_unreachable("conversion functions are permitted");
2515 }
2516 } ConvertDiagnoser;
2517 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2518}
2519
2520OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2521 SourceLocation StartLoc,
2522 SourceLocation LParenLoc,
2523 SourceLocation EndLoc) {
2524 Expr *ValExpr = NumThreads;
2525 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2526 !NumThreads->isInstantiationDependent() &&
2527 !NumThreads->containsUnexpandedParameterPack()) {
2528 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2529 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002530 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002531 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002532 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002533
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002534 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002535
2536 // OpenMP [2.5, Restrictions]
2537 // The num_threads expression must evaluate to a positive integer value.
2538 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002539 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2540 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002541 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2542 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002543 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002544 }
2545 }
2546
Alexey Bataeved09d242014-05-28 05:53:51 +00002547 return new (Context)
2548 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002549}
2550
Alexey Bataev62c87d22014-03-21 04:51:18 +00002551ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2552 OpenMPClauseKind CKind) {
2553 if (!E)
2554 return ExprError();
2555 if (E->isValueDependent() || E->isTypeDependent() ||
2556 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002557 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002558 llvm::APSInt Result;
2559 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2560 if (ICE.isInvalid())
2561 return ExprError();
2562 if (!Result.isStrictlyPositive()) {
2563 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2564 << getOpenMPClauseName(CKind) << E->getSourceRange();
2565 return ExprError();
2566 }
2567 return ICE;
2568}
2569
2570OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2571 SourceLocation LParenLoc,
2572 SourceLocation EndLoc) {
2573 // OpenMP [2.8.1, simd construct, Description]
2574 // The parameter of the safelen clause must be a constant
2575 // positive integer expression.
2576 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2577 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002578 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002579 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002580 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002581}
2582
Alexander Musman64d33f12014-06-04 07:53:32 +00002583OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2584 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002585 SourceLocation LParenLoc,
2586 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002587 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002588 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002589 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002590 // The parameter of the collapse clause must be a constant
2591 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002592 ExprResult NumForLoopsResult =
2593 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2594 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002595 return nullptr;
2596 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002597 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002598}
2599
Alexey Bataeved09d242014-05-28 05:53:51 +00002600OMPClause *Sema::ActOnOpenMPSimpleClause(
2601 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2602 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002603 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002604 switch (Kind) {
2605 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002606 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002607 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2608 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002609 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002610 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002611 Res = ActOnOpenMPProcBindClause(
2612 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2613 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002614 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002615 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002616 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002617 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002618 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002619 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002620 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002621 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002622 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002623 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002624 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002625 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002626 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002627 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002628 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002629 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002630 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002631 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002632 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002633 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002634 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002635 case OMPC_flush:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002636 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002637 llvm_unreachable("Clause is not allowed.");
2638 }
2639 return Res;
2640}
2641
2642OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2643 SourceLocation KindKwLoc,
2644 SourceLocation StartLoc,
2645 SourceLocation LParenLoc,
2646 SourceLocation EndLoc) {
2647 if (Kind == OMPC_DEFAULT_unknown) {
2648 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002649 static_assert(OMPC_DEFAULT_unknown > 0,
2650 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002651 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002652 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002653 Values += "'";
2654 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2655 Values += "'";
2656 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002657 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002658 Values += " or ";
2659 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002660 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002661 break;
2662 default:
2663 Values += Sep;
2664 break;
2665 }
2666 }
2667 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002668 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002669 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002670 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002671 switch (Kind) {
2672 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002673 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002674 break;
2675 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002676 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002677 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002678 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002679 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002680 break;
2681 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002682 return new (Context)
2683 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002684}
2685
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002686OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2687 SourceLocation KindKwLoc,
2688 SourceLocation StartLoc,
2689 SourceLocation LParenLoc,
2690 SourceLocation EndLoc) {
2691 if (Kind == OMPC_PROC_BIND_unknown) {
2692 std::string Values;
2693 std::string Sep(", ");
2694 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2695 Values += "'";
2696 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2697 Values += "'";
2698 switch (i) {
2699 case OMPC_PROC_BIND_unknown - 2:
2700 Values += " or ";
2701 break;
2702 case OMPC_PROC_BIND_unknown - 1:
2703 break;
2704 default:
2705 Values += Sep;
2706 break;
2707 }
2708 }
2709 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002710 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002711 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002712 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002713 return new (Context)
2714 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002715}
2716
Alexey Bataev56dafe82014-06-20 07:16:17 +00002717OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2718 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2719 SourceLocation StartLoc, SourceLocation LParenLoc,
2720 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2721 SourceLocation EndLoc) {
2722 OMPClause *Res = nullptr;
2723 switch (Kind) {
2724 case OMPC_schedule:
2725 Res = ActOnOpenMPScheduleClause(
2726 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2727 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2728 break;
2729 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002730 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002731 case OMPC_num_threads:
2732 case OMPC_safelen:
2733 case OMPC_collapse:
2734 case OMPC_default:
2735 case OMPC_proc_bind:
2736 case OMPC_private:
2737 case OMPC_firstprivate:
2738 case OMPC_lastprivate:
2739 case OMPC_shared:
2740 case OMPC_reduction:
2741 case OMPC_linear:
2742 case OMPC_aligned:
2743 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002744 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002745 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002746 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002747 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002748 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002749 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002750 case OMPC_flush:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002751 case OMPC_unknown:
2752 llvm_unreachable("Clause is not allowed.");
2753 }
2754 return Res;
2755}
2756
2757OMPClause *Sema::ActOnOpenMPScheduleClause(
2758 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2759 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2760 SourceLocation EndLoc) {
2761 if (Kind == OMPC_SCHEDULE_unknown) {
2762 std::string Values;
2763 std::string Sep(", ");
2764 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2765 Values += "'";
2766 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2767 Values += "'";
2768 switch (i) {
2769 case OMPC_SCHEDULE_unknown - 2:
2770 Values += " or ";
2771 break;
2772 case OMPC_SCHEDULE_unknown - 1:
2773 break;
2774 default:
2775 Values += Sep;
2776 break;
2777 }
2778 }
2779 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2780 << Values << getOpenMPClauseName(OMPC_schedule);
2781 return nullptr;
2782 }
2783 Expr *ValExpr = ChunkSize;
2784 if (ChunkSize) {
2785 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2786 !ChunkSize->isInstantiationDependent() &&
2787 !ChunkSize->containsUnexpandedParameterPack()) {
2788 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2789 ExprResult Val =
2790 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2791 if (Val.isInvalid())
2792 return nullptr;
2793
2794 ValExpr = Val.get();
2795
2796 // OpenMP [2.7.1, Restrictions]
2797 // chunk_size must be a loop invariant integer expression with a positive
2798 // value.
2799 llvm::APSInt Result;
2800 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2801 Result.isSigned() && !Result.isStrictlyPositive()) {
2802 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2803 << "schedule" << ChunkSize->getSourceRange();
2804 return nullptr;
2805 }
2806 }
2807 }
2808
2809 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2810 EndLoc, Kind, ValExpr);
2811}
2812
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002813OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2814 SourceLocation StartLoc,
2815 SourceLocation EndLoc) {
2816 OMPClause *Res = nullptr;
2817 switch (Kind) {
2818 case OMPC_ordered:
2819 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2820 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002821 case OMPC_nowait:
2822 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2823 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002824 case OMPC_untied:
2825 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
2826 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002827 case OMPC_mergeable:
2828 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
2829 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002830 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002831 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002832 case OMPC_num_threads:
2833 case OMPC_safelen:
2834 case OMPC_collapse:
2835 case OMPC_schedule:
2836 case OMPC_private:
2837 case OMPC_firstprivate:
2838 case OMPC_lastprivate:
2839 case OMPC_shared:
2840 case OMPC_reduction:
2841 case OMPC_linear:
2842 case OMPC_aligned:
2843 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002844 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002845 case OMPC_default:
2846 case OMPC_proc_bind:
2847 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002848 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002849 case OMPC_unknown:
2850 llvm_unreachable("Clause is not allowed.");
2851 }
2852 return Res;
2853}
2854
2855OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2856 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002857 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002858 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2859}
2860
Alexey Bataev236070f2014-06-20 11:19:47 +00002861OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2862 SourceLocation EndLoc) {
2863 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2864}
2865
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002866OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
2867 SourceLocation EndLoc) {
2868 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
2869}
2870
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002871OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
2872 SourceLocation EndLoc) {
2873 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
2874}
2875
Alexey Bataevc5e02582014-06-16 07:08:35 +00002876OMPClause *Sema::ActOnOpenMPVarListClause(
2877 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2878 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2879 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2880 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002881 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002882 switch (Kind) {
2883 case OMPC_private:
2884 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2885 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002886 case OMPC_firstprivate:
2887 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2888 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002889 case OMPC_lastprivate:
2890 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2891 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002892 case OMPC_shared:
2893 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2894 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002895 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002896 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2897 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002898 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002899 case OMPC_linear:
2900 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2901 ColonLoc, EndLoc);
2902 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002903 case OMPC_aligned:
2904 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2905 ColonLoc, EndLoc);
2906 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002907 case OMPC_copyin:
2908 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2909 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002910 case OMPC_copyprivate:
2911 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2912 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002913 case OMPC_flush:
2914 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
2915 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002916 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002917 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002918 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002919 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002920 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002921 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002922 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002923 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002924 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002925 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002926 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002927 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002928 case OMPC_threadprivate:
2929 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002930 llvm_unreachable("Clause is not allowed.");
2931 }
2932 return Res;
2933}
2934
2935OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2936 SourceLocation StartLoc,
2937 SourceLocation LParenLoc,
2938 SourceLocation EndLoc) {
2939 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002940 for (auto &RefExpr : VarList) {
2941 assert(RefExpr && "NULL expr in OpenMP private clause.");
2942 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002943 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002944 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002945 continue;
2946 }
2947
Alexey Bataeved09d242014-05-28 05:53:51 +00002948 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002949 // OpenMP [2.1, C/C++]
2950 // A list item is a variable name.
2951 // OpenMP [2.9.3.3, Restrictions, p.1]
2952 // A variable that is part of another variable (as an array or
2953 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002954 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002955 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002956 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002957 continue;
2958 }
2959 Decl *D = DE->getDecl();
2960 VarDecl *VD = cast<VarDecl>(D);
2961
2962 QualType Type = VD->getType();
2963 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2964 // It will be analyzed later.
2965 Vars.push_back(DE);
2966 continue;
2967 }
2968
2969 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2970 // A variable that appears in a private clause must not have an incomplete
2971 // type or a reference type.
2972 if (RequireCompleteType(ELoc, Type,
2973 diag::err_omp_private_incomplete_type)) {
2974 continue;
2975 }
2976 if (Type->isReferenceType()) {
2977 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002978 << getOpenMPClauseName(OMPC_private) << Type;
2979 bool IsDecl =
2980 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2981 Diag(VD->getLocation(),
2982 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2983 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002984 continue;
2985 }
2986
2987 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2988 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002989 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002990 // class type.
2991 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002992 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2993 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002994 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002995 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2996 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2997 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002998 // FIXME This code must be replaced by actual constructing/destructing of
2999 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003000 if (RD) {
3001 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3002 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003003 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003004 if (!CD ||
3005 CheckConstructorAccess(ELoc, CD,
3006 InitializedEntity::InitializeTemporary(Type),
3007 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003008 CD->isDeleted()) {
3009 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003010 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003011 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3012 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003013 Diag(VD->getLocation(),
3014 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3015 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003016 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3017 continue;
3018 }
3019 MarkFunctionReferenced(ELoc, CD);
3020 DiagnoseUseOfDecl(CD, ELoc);
3021
3022 CXXDestructorDecl *DD = RD->getDestructor();
3023 if (DD) {
3024 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3025 DD->isDeleted()) {
3026 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003027 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003028 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3029 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003030 Diag(VD->getLocation(),
3031 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3032 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003033 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3034 continue;
3035 }
3036 MarkFunctionReferenced(ELoc, DD);
3037 DiagnoseUseOfDecl(DD, ELoc);
3038 }
3039 }
3040
Alexey Bataev758e55e2013-09-06 18:03:48 +00003041 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3042 // in a Construct]
3043 // Variables with the predetermined data-sharing attributes may not be
3044 // listed in data-sharing attributes clauses, except for the cases
3045 // listed below. For these exceptions only, listing a predetermined
3046 // variable in a data-sharing attribute clause is allowed and overrides
3047 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003048 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003049 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003050 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3051 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003052 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003053 continue;
3054 }
3055
3056 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003057 Vars.push_back(DE);
3058 }
3059
Alexey Bataeved09d242014-05-28 05:53:51 +00003060 if (Vars.empty())
3061 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003062
3063 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3064}
3065
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003066OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
3067 SourceLocation StartLoc,
3068 SourceLocation LParenLoc,
3069 SourceLocation EndLoc) {
3070 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003071 bool IsImplicitClause =
3072 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
3073 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
3074
Alexey Bataeved09d242014-05-28 05:53:51 +00003075 for (auto &RefExpr : VarList) {
3076 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
3077 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003078 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003079 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003080 continue;
3081 }
3082
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003083 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
3084 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003085 // OpenMP [2.1, C/C++]
3086 // A list item is a variable name.
3087 // OpenMP [2.9.3.3, Restrictions, p.1]
3088 // A variable that is part of another variable (as an array or
3089 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003090 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003091 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003092 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003093 continue;
3094 }
3095 Decl *D = DE->getDecl();
3096 VarDecl *VD = cast<VarDecl>(D);
3097
3098 QualType Type = VD->getType();
3099 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3100 // It will be analyzed later.
3101 Vars.push_back(DE);
3102 continue;
3103 }
3104
3105 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3106 // A variable that appears in a private clause must not have an incomplete
3107 // type or a reference type.
3108 if (RequireCompleteType(ELoc, Type,
3109 diag::err_omp_firstprivate_incomplete_type)) {
3110 continue;
3111 }
3112 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003113 if (IsImplicitClause) {
3114 Diag(ImplicitClauseLoc,
3115 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
3116 << Type;
3117 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3118 } else {
3119 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3120 << getOpenMPClauseName(OMPC_firstprivate) << Type;
3121 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003122 bool IsDecl =
3123 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3124 Diag(VD->getLocation(),
3125 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3126 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003127 continue;
3128 }
3129
3130 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
3131 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003132 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003133 // class type.
3134 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003135 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3136 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3137 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003138 // FIXME This code must be replaced by actual constructing/destructing of
3139 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003140 if (RD) {
3141 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
3142 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003143 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003144 if (!CD ||
3145 CheckConstructorAccess(ELoc, CD,
3146 InitializedEntity::InitializeTemporary(Type),
3147 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003148 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003149 if (IsImplicitClause) {
3150 Diag(ImplicitClauseLoc,
3151 diag::err_omp_task_predetermined_firstprivate_required_method)
3152 << 0;
3153 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3154 } else {
3155 Diag(ELoc, diag::err_omp_required_method)
3156 << getOpenMPClauseName(OMPC_firstprivate) << 1;
3157 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003158 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3159 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003160 Diag(VD->getLocation(),
3161 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3162 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003163 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3164 continue;
3165 }
3166 MarkFunctionReferenced(ELoc, CD);
3167 DiagnoseUseOfDecl(CD, ELoc);
3168
3169 CXXDestructorDecl *DD = RD->getDestructor();
3170 if (DD) {
3171 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3172 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003173 if (IsImplicitClause) {
3174 Diag(ImplicitClauseLoc,
3175 diag::err_omp_task_predetermined_firstprivate_required_method)
3176 << 1;
3177 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3178 } else {
3179 Diag(ELoc, diag::err_omp_required_method)
3180 << getOpenMPClauseName(OMPC_firstprivate) << 4;
3181 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003182 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3183 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003184 Diag(VD->getLocation(),
3185 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3186 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003187 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3188 continue;
3189 }
3190 MarkFunctionReferenced(ELoc, DD);
3191 DiagnoseUseOfDecl(DD, ELoc);
3192 }
3193 }
3194
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003195 // If an implicit firstprivate variable found it was checked already.
3196 if (!IsImplicitClause) {
3197 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003198 Type = Type.getNonReferenceType().getCanonicalType();
3199 bool IsConstant = Type.isConstant(Context);
3200 Type = Context.getBaseElementType(Type);
3201 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
3202 // A list item that specifies a given variable may not appear in more
3203 // than one clause on the same directive, except that a variable may be
3204 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003205 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00003206 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003207 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003208 << getOpenMPClauseName(DVar.CKind)
3209 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003210 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003211 continue;
3212 }
3213
3214 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3215 // in a Construct]
3216 // Variables with the predetermined data-sharing attributes may not be
3217 // listed in data-sharing attributes clauses, except for the cases
3218 // listed below. For these exceptions only, listing a predetermined
3219 // variable in a data-sharing attribute clause is allowed and overrides
3220 // the variable's predetermined data-sharing attributes.
3221 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3222 // in a Construct, C/C++, p.2]
3223 // Variables with const-qualified type having no mutable member may be
3224 // listed in a firstprivate clause, even if they are static data members.
3225 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
3226 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
3227 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003228 << getOpenMPClauseName(DVar.CKind)
3229 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003230 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003231 continue;
3232 }
3233
Alexey Bataevf29276e2014-06-18 04:14:57 +00003234 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003235 // OpenMP [2.9.3.4, Restrictions, p.2]
3236 // A list item that is private within a parallel region must not appear
3237 // in a firstprivate clause on a worksharing construct if any of the
3238 // worksharing regions arising from the worksharing construct ever bind
3239 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00003240 if (isOpenMPWorksharingDirective(CurrDir) &&
3241 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003242 DVar = DSAStack->getImplicitDSA(VD, true);
3243 if (DVar.CKind != OMPC_shared &&
3244 (isOpenMPParallelDirective(DVar.DKind) ||
3245 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003246 Diag(ELoc, diag::err_omp_required_access)
3247 << getOpenMPClauseName(OMPC_firstprivate)
3248 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003249 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003250 continue;
3251 }
3252 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003253 // OpenMP [2.9.3.4, Restrictions, p.3]
3254 // A list item that appears in a reduction clause of a parallel construct
3255 // must not appear in a firstprivate clause on a worksharing or task
3256 // construct if any of the worksharing or task regions arising from the
3257 // worksharing or task construct ever bind to any of the parallel regions
3258 // arising from the parallel construct.
3259 // OpenMP [2.9.3.4, Restrictions, p.4]
3260 // A list item that appears in a reduction clause in worksharing
3261 // construct must not appear in a firstprivate clause in a task construct
3262 // encountered during execution of any of the worksharing regions arising
3263 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003264 if (CurrDir == OMPD_task) {
3265 DVar =
3266 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
3267 [](OpenMPDirectiveKind K) -> bool {
3268 return isOpenMPParallelDirective(K) ||
3269 isOpenMPWorksharingDirective(K);
3270 },
3271 false);
3272 if (DVar.CKind == OMPC_reduction &&
3273 (isOpenMPParallelDirective(DVar.DKind) ||
3274 isOpenMPWorksharingDirective(DVar.DKind))) {
3275 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
3276 << getOpenMPDirectiveName(DVar.DKind);
3277 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3278 continue;
3279 }
3280 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003281 }
3282
3283 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
3284 Vars.push_back(DE);
3285 }
3286
Alexey Bataeved09d242014-05-28 05:53:51 +00003287 if (Vars.empty())
3288 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003289
3290 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3291 Vars);
3292}
3293
Alexander Musman1bb328c2014-06-04 13:06:39 +00003294OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
3295 SourceLocation StartLoc,
3296 SourceLocation LParenLoc,
3297 SourceLocation EndLoc) {
3298 SmallVector<Expr *, 8> Vars;
3299 for (auto &RefExpr : VarList) {
3300 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
3301 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3302 // It will be analyzed later.
3303 Vars.push_back(RefExpr);
3304 continue;
3305 }
3306
3307 SourceLocation ELoc = RefExpr->getExprLoc();
3308 // OpenMP [2.1, C/C++]
3309 // A list item is a variable name.
3310 // OpenMP [2.14.3.5, Restrictions, p.1]
3311 // A variable that is part of another variable (as an array or structure
3312 // element) cannot appear in a lastprivate clause.
3313 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3314 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3315 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3316 continue;
3317 }
3318 Decl *D = DE->getDecl();
3319 VarDecl *VD = cast<VarDecl>(D);
3320
3321 QualType Type = VD->getType();
3322 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3323 // It will be analyzed later.
3324 Vars.push_back(DE);
3325 continue;
3326 }
3327
3328 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
3329 // A variable that appears in a lastprivate clause must not have an
3330 // incomplete type or a reference type.
3331 if (RequireCompleteType(ELoc, Type,
3332 diag::err_omp_lastprivate_incomplete_type)) {
3333 continue;
3334 }
3335 if (Type->isReferenceType()) {
3336 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3337 << getOpenMPClauseName(OMPC_lastprivate) << Type;
3338 bool IsDecl =
3339 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3340 Diag(VD->getLocation(),
3341 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3342 << VD;
3343 continue;
3344 }
3345
3346 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3347 // in a Construct]
3348 // Variables with the predetermined data-sharing attributes may not be
3349 // listed in data-sharing attributes clauses, except for the cases
3350 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003351 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003352 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
3353 DVar.CKind != OMPC_firstprivate &&
3354 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3355 Diag(ELoc, diag::err_omp_wrong_dsa)
3356 << getOpenMPClauseName(DVar.CKind)
3357 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003358 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003359 continue;
3360 }
3361
Alexey Bataevf29276e2014-06-18 04:14:57 +00003362 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
3363 // OpenMP [2.14.3.5, Restrictions, p.2]
3364 // A list item that is private within a parallel region, or that appears in
3365 // the reduction clause of a parallel construct, must not appear in a
3366 // lastprivate clause on a worksharing construct if any of the corresponding
3367 // worksharing regions ever binds to any of the corresponding parallel
3368 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00003369 if (isOpenMPWorksharingDirective(CurrDir) &&
3370 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003371 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003372 if (DVar.CKind != OMPC_shared) {
3373 Diag(ELoc, diag::err_omp_required_access)
3374 << getOpenMPClauseName(OMPC_lastprivate)
3375 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003376 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003377 continue;
3378 }
3379 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003380 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003381 // A variable of class type (or array thereof) that appears in a
3382 // lastprivate clause requires an accessible, unambiguous default
3383 // constructor for the class type, unless the list item is also specified
3384 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003385 // A variable of class type (or array thereof) that appears in a
3386 // lastprivate clause requires an accessible, unambiguous copy assignment
3387 // operator for the class type.
3388 while (Type.getNonReferenceType()->isArrayType())
3389 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3390 ->getElementType();
3391 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3392 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3393 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003394 // FIXME This code must be replaced by actual copying and destructing of the
3395 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003396 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003397 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3398 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003399 if (MD) {
3400 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3401 MD->isDeleted()) {
3402 Diag(ELoc, diag::err_omp_required_method)
3403 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3404 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3405 VarDecl::DeclarationOnly;
3406 Diag(VD->getLocation(),
3407 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3408 << VD;
3409 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3410 continue;
3411 }
3412 MarkFunctionReferenced(ELoc, MD);
3413 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003414 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003415
3416 CXXDestructorDecl *DD = RD->getDestructor();
3417 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003418 PartialDiagnostic PD =
3419 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003420 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3421 DD->isDeleted()) {
3422 Diag(ELoc, diag::err_omp_required_method)
3423 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3424 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3425 VarDecl::DeclarationOnly;
3426 Diag(VD->getLocation(),
3427 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3428 << VD;
3429 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3430 continue;
3431 }
3432 MarkFunctionReferenced(ELoc, DD);
3433 DiagnoseUseOfDecl(DD, ELoc);
3434 }
3435 }
3436
Alexey Bataevf29276e2014-06-18 04:14:57 +00003437 if (DVar.CKind != OMPC_firstprivate)
3438 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003439 Vars.push_back(DE);
3440 }
3441
3442 if (Vars.empty())
3443 return nullptr;
3444
3445 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3446 Vars);
3447}
3448
Alexey Bataev758e55e2013-09-06 18:03:48 +00003449OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3450 SourceLocation StartLoc,
3451 SourceLocation LParenLoc,
3452 SourceLocation EndLoc) {
3453 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003454 for (auto &RefExpr : VarList) {
3455 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3456 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003457 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003458 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003459 continue;
3460 }
3461
Alexey Bataeved09d242014-05-28 05:53:51 +00003462 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003463 // OpenMP [2.1, C/C++]
3464 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003465 // OpenMP [2.14.3.2, Restrictions, p.1]
3466 // A variable that is part of another variable (as an array or structure
3467 // element) cannot appear in a shared unless it is a static data member
3468 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003469 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003470 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003471 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003472 continue;
3473 }
3474 Decl *D = DE->getDecl();
3475 VarDecl *VD = cast<VarDecl>(D);
3476
3477 QualType Type = VD->getType();
3478 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3479 // It will be analyzed later.
3480 Vars.push_back(DE);
3481 continue;
3482 }
3483
3484 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3485 // in a Construct]
3486 // Variables with the predetermined data-sharing attributes may not be
3487 // listed in data-sharing attributes clauses, except for the cases
3488 // listed below. For these exceptions only, listing a predetermined
3489 // variable in a data-sharing attribute clause is allowed and overrides
3490 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003491 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003492 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3493 DVar.RefExpr) {
3494 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3495 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003496 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003497 continue;
3498 }
3499
3500 DSAStack->addDSA(VD, DE, OMPC_shared);
3501 Vars.push_back(DE);
3502 }
3503
Alexey Bataeved09d242014-05-28 05:53:51 +00003504 if (Vars.empty())
3505 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003506
3507 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3508}
3509
Alexey Bataevc5e02582014-06-16 07:08:35 +00003510namespace {
3511class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3512 DSAStackTy *Stack;
3513
3514public:
3515 bool VisitDeclRefExpr(DeclRefExpr *E) {
3516 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003517 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003518 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3519 return false;
3520 if (DVar.CKind != OMPC_unknown)
3521 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003522 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003523 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003524 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003525 return true;
3526 return false;
3527 }
3528 return false;
3529 }
3530 bool VisitStmt(Stmt *S) {
3531 for (auto Child : S->children()) {
3532 if (Child && Visit(Child))
3533 return true;
3534 }
3535 return false;
3536 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003537 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003538};
Alexey Bataev23b69422014-06-18 07:08:49 +00003539} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003540
3541OMPClause *Sema::ActOnOpenMPReductionClause(
3542 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3543 SourceLocation ColonLoc, SourceLocation EndLoc,
3544 CXXScopeSpec &ReductionIdScopeSpec,
3545 const DeclarationNameInfo &ReductionId) {
3546 // TODO: Allow scope specification search when 'declare reduction' is
3547 // supported.
3548 assert(ReductionIdScopeSpec.isEmpty() &&
3549 "No support for scoped reduction identifiers yet.");
3550
3551 auto DN = ReductionId.getName();
3552 auto OOK = DN.getCXXOverloadedOperator();
3553 BinaryOperatorKind BOK = BO_Comma;
3554
3555 // OpenMP [2.14.3.6, reduction clause]
3556 // C
3557 // reduction-identifier is either an identifier or one of the following
3558 // operators: +, -, *, &, |, ^, && and ||
3559 // C++
3560 // reduction-identifier is either an id-expression or one of the following
3561 // operators: +, -, *, &, |, ^, && and ||
3562 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3563 switch (OOK) {
3564 case OO_Plus:
3565 case OO_Minus:
3566 BOK = BO_AddAssign;
3567 break;
3568 case OO_Star:
3569 BOK = BO_MulAssign;
3570 break;
3571 case OO_Amp:
3572 BOK = BO_AndAssign;
3573 break;
3574 case OO_Pipe:
3575 BOK = BO_OrAssign;
3576 break;
3577 case OO_Caret:
3578 BOK = BO_XorAssign;
3579 break;
3580 case OO_AmpAmp:
3581 BOK = BO_LAnd;
3582 break;
3583 case OO_PipePipe:
3584 BOK = BO_LOr;
3585 break;
3586 default:
3587 if (auto II = DN.getAsIdentifierInfo()) {
3588 if (II->isStr("max"))
3589 BOK = BO_GT;
3590 else if (II->isStr("min"))
3591 BOK = BO_LT;
3592 }
3593 break;
3594 }
3595 SourceRange ReductionIdRange;
3596 if (ReductionIdScopeSpec.isValid()) {
3597 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3598 }
3599 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3600 if (BOK == BO_Comma) {
3601 // Not allowed reduction identifier is found.
3602 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3603 << ReductionIdRange;
3604 return nullptr;
3605 }
3606
3607 SmallVector<Expr *, 8> Vars;
3608 for (auto RefExpr : VarList) {
3609 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3610 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3611 // It will be analyzed later.
3612 Vars.push_back(RefExpr);
3613 continue;
3614 }
3615
3616 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3617 RefExpr->isInstantiationDependent() ||
3618 RefExpr->containsUnexpandedParameterPack()) {
3619 // It will be analyzed later.
3620 Vars.push_back(RefExpr);
3621 continue;
3622 }
3623
3624 auto ELoc = RefExpr->getExprLoc();
3625 auto ERange = RefExpr->getSourceRange();
3626 // OpenMP [2.1, C/C++]
3627 // A list item is a variable or array section, subject to the restrictions
3628 // specified in Section 2.4 on page 42 and in each of the sections
3629 // describing clauses and directives for which a list appears.
3630 // OpenMP [2.14.3.3, Restrictions, p.1]
3631 // A variable that is part of another variable (as an array or
3632 // structure element) cannot appear in a private clause.
3633 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3634 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3635 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3636 continue;
3637 }
3638 auto D = DE->getDecl();
3639 auto VD = cast<VarDecl>(D);
3640 auto Type = VD->getType();
3641 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3642 // A variable that appears in a private clause must not have an incomplete
3643 // type or a reference type.
3644 if (RequireCompleteType(ELoc, Type,
3645 diag::err_omp_reduction_incomplete_type))
3646 continue;
3647 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3648 // Arrays may not appear in a reduction clause.
3649 if (Type.getNonReferenceType()->isArrayType()) {
3650 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3651 bool IsDecl =
3652 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3653 Diag(VD->getLocation(),
3654 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3655 << VD;
3656 continue;
3657 }
3658 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3659 // A list item that appears in a reduction clause must not be
3660 // const-qualified.
3661 if (Type.getNonReferenceType().isConstant(Context)) {
3662 Diag(ELoc, diag::err_omp_const_variable)
3663 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3664 bool IsDecl =
3665 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3666 Diag(VD->getLocation(),
3667 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3668 << VD;
3669 continue;
3670 }
3671 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3672 // If a list-item is a reference type then it must bind to the same object
3673 // for all threads of the team.
3674 VarDecl *VDDef = VD->getDefinition();
3675 if (Type->isReferenceType() && VDDef) {
3676 DSARefChecker Check(DSAStack);
3677 if (Check.Visit(VDDef->getInit())) {
3678 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3679 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3680 continue;
3681 }
3682 }
3683 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3684 // The type of a list item that appears in a reduction clause must be valid
3685 // for the reduction-identifier. For a max or min reduction in C, the type
3686 // of the list item must be an allowed arithmetic data type: char, int,
3687 // float, double, or _Bool, possibly modified with long, short, signed, or
3688 // unsigned. For a max or min reduction in C++, the type of the list item
3689 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3690 // double, or bool, possibly modified with long, short, signed, or unsigned.
3691 if ((BOK == BO_GT || BOK == BO_LT) &&
3692 !(Type->isScalarType() ||
3693 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3694 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3695 << getLangOpts().CPlusPlus;
3696 bool IsDecl =
3697 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3698 Diag(VD->getLocation(),
3699 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3700 << VD;
3701 continue;
3702 }
3703 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3704 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3705 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3706 bool IsDecl =
3707 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3708 Diag(VD->getLocation(),
3709 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3710 << VD;
3711 continue;
3712 }
3713 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3714 getDiagnostics().setSuppressAllDiagnostics(true);
3715 ExprResult ReductionOp =
3716 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3717 RefExpr, RefExpr);
3718 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3719 if (ReductionOp.isInvalid()) {
3720 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003721 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003722 bool IsDecl =
3723 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3724 Diag(VD->getLocation(),
3725 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3726 << VD;
3727 continue;
3728 }
3729
3730 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3731 // in a Construct]
3732 // Variables with the predetermined data-sharing attributes may not be
3733 // listed in data-sharing attributes clauses, except for the cases
3734 // listed below. For these exceptions only, listing a predetermined
3735 // variable in a data-sharing attribute clause is allowed and overrides
3736 // the variable's predetermined data-sharing attributes.
3737 // OpenMP [2.14.3.6, Restrictions, p.3]
3738 // Any number of reduction clauses can be specified on the directive,
3739 // but a list item can appear only once in the reduction clauses for that
3740 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003741 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003742 if (DVar.CKind == OMPC_reduction) {
3743 Diag(ELoc, diag::err_omp_once_referenced)
3744 << getOpenMPClauseName(OMPC_reduction);
3745 if (DVar.RefExpr) {
3746 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3747 }
3748 } else if (DVar.CKind != OMPC_unknown) {
3749 Diag(ELoc, diag::err_omp_wrong_dsa)
3750 << getOpenMPClauseName(DVar.CKind)
3751 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003752 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003753 continue;
3754 }
3755
3756 // OpenMP [2.14.3.6, Restrictions, p.1]
3757 // A list item that appears in a reduction clause of a worksharing
3758 // construct must be shared in the parallel regions to which any of the
3759 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003760 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003761 if (isOpenMPWorksharingDirective(CurrDir) &&
3762 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003763 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003764 if (DVar.CKind != OMPC_shared) {
3765 Diag(ELoc, diag::err_omp_required_access)
3766 << getOpenMPClauseName(OMPC_reduction)
3767 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003768 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003769 continue;
3770 }
3771 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003772
3773 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3774 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3775 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003776 // FIXME This code must be replaced by actual constructing/destructing of
3777 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003778 if (RD) {
3779 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3780 PartialDiagnostic PD =
3781 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003782 if (!CD ||
3783 CheckConstructorAccess(ELoc, CD,
3784 InitializedEntity::InitializeTemporary(Type),
3785 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003786 CD->isDeleted()) {
3787 Diag(ELoc, diag::err_omp_required_method)
3788 << getOpenMPClauseName(OMPC_reduction) << 0;
3789 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3790 VarDecl::DeclarationOnly;
3791 Diag(VD->getLocation(),
3792 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3793 << VD;
3794 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3795 continue;
3796 }
3797 MarkFunctionReferenced(ELoc, CD);
3798 DiagnoseUseOfDecl(CD, ELoc);
3799
3800 CXXDestructorDecl *DD = RD->getDestructor();
3801 if (DD) {
3802 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3803 DD->isDeleted()) {
3804 Diag(ELoc, diag::err_omp_required_method)
3805 << getOpenMPClauseName(OMPC_reduction) << 4;
3806 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3807 VarDecl::DeclarationOnly;
3808 Diag(VD->getLocation(),
3809 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3810 << VD;
3811 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3812 continue;
3813 }
3814 MarkFunctionReferenced(ELoc, DD);
3815 DiagnoseUseOfDecl(DD, ELoc);
3816 }
3817 }
3818
3819 DSAStack->addDSA(VD, DE, OMPC_reduction);
3820 Vars.push_back(DE);
3821 }
3822
3823 if (Vars.empty())
3824 return nullptr;
3825
3826 return OMPReductionClause::Create(
3827 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3828 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3829}
3830
Alexander Musman8dba6642014-04-22 13:09:42 +00003831OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3832 SourceLocation StartLoc,
3833 SourceLocation LParenLoc,
3834 SourceLocation ColonLoc,
3835 SourceLocation EndLoc) {
3836 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003837 for (auto &RefExpr : VarList) {
3838 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3839 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003840 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003841 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003842 continue;
3843 }
3844
3845 // OpenMP [2.14.3.7, linear clause]
3846 // A list item that appears in a linear clause is subject to the private
3847 // clause semantics described in Section 2.14.3.3 on page 159 except as
3848 // noted. In addition, the value of the new list item on each iteration
3849 // of the associated loop(s) corresponds to the value of the original
3850 // list item before entering the construct plus the logical number of
3851 // the iteration times linear-step.
3852
Alexey Bataeved09d242014-05-28 05:53:51 +00003853 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003854 // OpenMP [2.1, C/C++]
3855 // A list item is a variable name.
3856 // OpenMP [2.14.3.3, Restrictions, p.1]
3857 // A variable that is part of another variable (as an array or
3858 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003859 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003860 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003861 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003862 continue;
3863 }
3864
3865 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3866
3867 // OpenMP [2.14.3.7, linear clause]
3868 // A list-item cannot appear in more than one linear clause.
3869 // A list-item that appears in a linear clause cannot appear in any
3870 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003871 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003872 if (DVar.RefExpr) {
3873 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3874 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003875 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003876 continue;
3877 }
3878
3879 QualType QType = VD->getType();
3880 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3881 // It will be analyzed later.
3882 Vars.push_back(DE);
3883 continue;
3884 }
3885
3886 // A variable must not have an incomplete type or a reference type.
3887 if (RequireCompleteType(ELoc, QType,
3888 diag::err_omp_linear_incomplete_type)) {
3889 continue;
3890 }
3891 if (QType->isReferenceType()) {
3892 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3893 << getOpenMPClauseName(OMPC_linear) << QType;
3894 bool IsDecl =
3895 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3896 Diag(VD->getLocation(),
3897 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3898 << VD;
3899 continue;
3900 }
3901
3902 // A list item must not be const-qualified.
3903 if (QType.isConstant(Context)) {
3904 Diag(ELoc, diag::err_omp_const_variable)
3905 << getOpenMPClauseName(OMPC_linear);
3906 bool IsDecl =
3907 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3908 Diag(VD->getLocation(),
3909 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3910 << VD;
3911 continue;
3912 }
3913
3914 // A list item must be of integral or pointer type.
3915 QType = QType.getUnqualifiedType().getCanonicalType();
3916 const Type *Ty = QType.getTypePtrOrNull();
3917 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3918 !Ty->isPointerType())) {
3919 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3920 bool IsDecl =
3921 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3922 Diag(VD->getLocation(),
3923 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3924 << VD;
3925 continue;
3926 }
3927
3928 DSAStack->addDSA(VD, DE, OMPC_linear);
3929 Vars.push_back(DE);
3930 }
3931
3932 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003933 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003934
3935 Expr *StepExpr = Step;
3936 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3937 !Step->isInstantiationDependent() &&
3938 !Step->containsUnexpandedParameterPack()) {
3939 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003941 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003942 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003943 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003944
3945 // Warn about zero linear step (it would be probably better specified as
3946 // making corresponding variables 'const').
3947 llvm::APSInt Result;
3948 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3949 !Result.isNegative() && !Result.isStrictlyPositive())
3950 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3951 << (Vars.size() > 1);
3952 }
3953
3954 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3955 Vars, StepExpr);
3956}
3957
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003958OMPClause *Sema::ActOnOpenMPAlignedClause(
3959 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3960 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3961
3962 SmallVector<Expr *, 8> Vars;
3963 for (auto &RefExpr : VarList) {
3964 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3965 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3966 // It will be analyzed later.
3967 Vars.push_back(RefExpr);
3968 continue;
3969 }
3970
3971 SourceLocation ELoc = RefExpr->getExprLoc();
3972 // OpenMP [2.1, C/C++]
3973 // A list item is a variable name.
3974 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3975 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3976 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3977 continue;
3978 }
3979
3980 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3981
3982 // OpenMP [2.8.1, simd construct, Restrictions]
3983 // The type of list items appearing in the aligned clause must be
3984 // array, pointer, reference to array, or reference to pointer.
3985 QualType QType = DE->getType()
3986 .getNonReferenceType()
3987 .getUnqualifiedType()
3988 .getCanonicalType();
3989 const Type *Ty = QType.getTypePtrOrNull();
3990 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3991 !Ty->isPointerType())) {
3992 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3993 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3994 bool IsDecl =
3995 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3996 Diag(VD->getLocation(),
3997 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3998 << VD;
3999 continue;
4000 }
4001
4002 // OpenMP [2.8.1, simd construct, Restrictions]
4003 // A list-item cannot appear in more than one aligned clause.
4004 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4005 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4006 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4007 << getOpenMPClauseName(OMPC_aligned);
4008 continue;
4009 }
4010
4011 Vars.push_back(DE);
4012 }
4013
4014 // OpenMP [2.8.1, simd construct, Description]
4015 // The parameter of the aligned clause, alignment, must be a constant
4016 // positive integer expression.
4017 // If no optional parameter is specified, implementation-defined default
4018 // alignments for SIMD instructions on the target platforms are assumed.
4019 if (Alignment != nullptr) {
4020 ExprResult AlignResult =
4021 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
4022 if (AlignResult.isInvalid())
4023 return nullptr;
4024 Alignment = AlignResult.get();
4025 }
4026 if (Vars.empty())
4027 return nullptr;
4028
4029 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
4030 EndLoc, Vars, Alignment);
4031}
4032
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004033OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
4034 SourceLocation StartLoc,
4035 SourceLocation LParenLoc,
4036 SourceLocation EndLoc) {
4037 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004038 for (auto &RefExpr : VarList) {
4039 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
4040 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004041 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004042 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004043 continue;
4044 }
4045
Alexey Bataeved09d242014-05-28 05:53:51 +00004046 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004047 // OpenMP [2.1, C/C++]
4048 // A list item is a variable name.
4049 // OpenMP [2.14.4.1, Restrictions, p.1]
4050 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00004051 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004052 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004053 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004054 continue;
4055 }
4056
4057 Decl *D = DE->getDecl();
4058 VarDecl *VD = cast<VarDecl>(D);
4059
4060 QualType Type = VD->getType();
4061 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4062 // It will be analyzed later.
4063 Vars.push_back(DE);
4064 continue;
4065 }
4066
4067 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
4068 // A list item that appears in a copyin clause must be threadprivate.
4069 if (!DSAStack->isThreadPrivate(VD)) {
4070 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00004071 << getOpenMPClauseName(OMPC_copyin)
4072 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004073 continue;
4074 }
4075
4076 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4077 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00004078 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004079 // operator for the class type.
4080 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004081 CXXRecordDecl *RD =
4082 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004083 // FIXME This code must be replaced by actual assignment of the
4084 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004085 if (RD) {
4086 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4087 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004088 if (MD) {
4089 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4090 MD->isDeleted()) {
4091 Diag(ELoc, diag::err_omp_required_method)
4092 << getOpenMPClauseName(OMPC_copyin) << 2;
4093 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4094 VarDecl::DeclarationOnly;
4095 Diag(VD->getLocation(),
4096 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4097 << VD;
4098 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4099 continue;
4100 }
4101 MarkFunctionReferenced(ELoc, MD);
4102 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004103 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004104 }
4105
4106 DSAStack->addDSA(VD, DE, OMPC_copyin);
4107 Vars.push_back(DE);
4108 }
4109
Alexey Bataeved09d242014-05-28 05:53:51 +00004110 if (Vars.empty())
4111 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004112
4113 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4114}
4115
Alexey Bataevbae9a792014-06-27 10:37:06 +00004116OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
4117 SourceLocation StartLoc,
4118 SourceLocation LParenLoc,
4119 SourceLocation EndLoc) {
4120 SmallVector<Expr *, 8> Vars;
4121 for (auto &RefExpr : VarList) {
4122 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
4123 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4124 // It will be analyzed later.
4125 Vars.push_back(RefExpr);
4126 continue;
4127 }
4128
4129 SourceLocation ELoc = RefExpr->getExprLoc();
4130 // OpenMP [2.1, C/C++]
4131 // A list item is a variable name.
4132 // OpenMP [2.14.4.1, Restrictions, p.1]
4133 // A list item that appears in a copyin clause must be threadprivate.
4134 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4135 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4136 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4137 continue;
4138 }
4139
4140 Decl *D = DE->getDecl();
4141 VarDecl *VD = cast<VarDecl>(D);
4142
4143 QualType Type = VD->getType();
4144 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4145 // It will be analyzed later.
4146 Vars.push_back(DE);
4147 continue;
4148 }
4149
4150 // OpenMP [2.14.4.2, Restrictions, p.2]
4151 // A list item that appears in a copyprivate clause may not appear in a
4152 // private or firstprivate clause on the single construct.
4153 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004154 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004155 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
4156 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
4157 Diag(ELoc, diag::err_omp_wrong_dsa)
4158 << getOpenMPClauseName(DVar.CKind)
4159 << getOpenMPClauseName(OMPC_copyprivate);
4160 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4161 continue;
4162 }
4163
4164 // OpenMP [2.11.4.2, Restrictions, p.1]
4165 // All list items that appear in a copyprivate clause must be either
4166 // threadprivate or private in the enclosing context.
4167 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004168 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004169 if (DVar.CKind == OMPC_shared) {
4170 Diag(ELoc, diag::err_omp_required_access)
4171 << getOpenMPClauseName(OMPC_copyprivate)
4172 << "threadprivate or private in the enclosing context";
4173 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4174 continue;
4175 }
4176 }
4177 }
4178
4179 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4180 // A variable of class type (or array thereof) that appears in a
4181 // copyin clause requires an accessible, unambiguous copy assignment
4182 // operator for the class type.
4183 Type = Context.getBaseElementType(Type);
4184 CXXRecordDecl *RD =
4185 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
4186 // FIXME This code must be replaced by actual assignment of the
4187 // threadprivate variable.
4188 if (RD) {
4189 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4190 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
4191 if (MD) {
4192 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4193 MD->isDeleted()) {
4194 Diag(ELoc, diag::err_omp_required_method)
4195 << getOpenMPClauseName(OMPC_copyprivate) << 2;
4196 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4197 VarDecl::DeclarationOnly;
4198 Diag(VD->getLocation(),
4199 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4200 << VD;
4201 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4202 continue;
4203 }
4204 MarkFunctionReferenced(ELoc, MD);
4205 DiagnoseUseOfDecl(MD, ELoc);
4206 }
4207 }
4208
4209 // No need to mark vars as copyprivate, they are already threadprivate or
4210 // implicitly private.
4211 Vars.push_back(DE);
4212 }
4213
4214 if (Vars.empty())
4215 return nullptr;
4216
4217 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4218}
4219
Alexey Bataev6125da92014-07-21 11:26:11 +00004220OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
4221 SourceLocation StartLoc,
4222 SourceLocation LParenLoc,
4223 SourceLocation EndLoc) {
4224 if (VarList.empty())
4225 return nullptr;
4226
4227 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
4228}
4229
Alexey Bataev758e55e2013-09-06 18:03:48 +00004230#undef DSAStack