blob: 8d6b93c8fc604c854f34227f975abf5ab3b87196 [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};
42
43/// \brief Stack for tracking declarations used in OpenMP directives and
44/// clauses and their data-sharing attributes.
45class DSAStackTy {
46public:
47 struct DSAVarData {
48 OpenMPDirectiveKind DKind;
49 OpenMPClauseKind CKind;
50 DeclRefExpr *RefExpr;
Alexander Musmancb7f9c42014-05-15 13:04:49 +000051 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000052 };
Alexey Bataeved09d242014-05-28 05:53:51 +000053
Alexey Bataev758e55e2013-09-06 18:03:48 +000054private:
55 struct DSAInfo {
56 OpenMPClauseKind Attributes;
57 DeclRefExpr *RefExpr;
58 };
59 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000060 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000061
62 struct SharingMapTy {
63 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000064 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065 DefaultDataSharingAttributes DefaultAttr;
66 OpenMPDirectiveKind Directive;
67 DeclarationNameInfo DirectiveName;
68 Scope *CurScope;
Alexey Bataeved09d242014-05-28 05:53:51 +000069 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 Scope *CurScope)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000071 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
72 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope) {
73 }
Alexey Bataev758e55e2013-09-06 18:03:48 +000074 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +000075 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
76 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000077 };
78
79 typedef SmallVector<SharingMapTy, 64> StackTy;
80
81 /// \brief Stack of used declaration and their data-sharing attributes.
82 StackTy Stack;
83 Sema &Actions;
84
85 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
86
87 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +000088
89 /// \brief Checks if the variable is a local for OpenMP region.
90 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +000091
Alexey Bataev758e55e2013-09-06 18:03:48 +000092public:
Alexey Bataeved09d242014-05-28 05:53:51 +000093 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000094
95 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
96 Scope *CurScope) {
97 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
98 }
99
100 void pop() {
101 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
102 Stack.pop_back();
103 }
104
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000105 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000106 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000107 /// for diagnostics.
108 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
109
Alexey Bataev758e55e2013-09-06 18:03:48 +0000110 /// \brief Adds explicit data sharing attribute to the specified declaration.
111 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
112
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113 /// \brief Returns data sharing attributes from top of the stack for the
114 /// specified declaration.
115 DSAVarData getTopDSA(VarDecl *D);
116 /// \brief Returns data-sharing attributes for the specified declaration.
117 DSAVarData getImplicitDSA(VarDecl *D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000118 /// \brief Checks if the specified variables has \a CKind data-sharing
119 /// attribute in \a DKind directive.
120 DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
121 OpenMPDirectiveKind DKind = OMPD_unknown);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000122 /// \brief Checks if the specified variables has \a CKind data-sharing
123 /// attribute in an innermost \a DKind directive.
124 DSAVarData hasInnermostDSA(VarDecl *D, OpenMPClauseKind CKind,
125 OpenMPDirectiveKind DKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000126
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 /// \brief Returns currently analyzed directive.
128 OpenMPDirectiveKind getCurrentDirective() const {
129 return Stack.back().Directive;
130 }
131
132 /// \brief Set default data sharing attribute to none.
133 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
134 /// \brief Set default data sharing attribute to shared.
135 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
136
137 DefaultDataSharingAttributes getDefaultDSA() const {
138 return Stack.back().DefaultAttr;
139 }
140
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000141 /// \brief Checks if the spewcified variable is threadprivate.
142 bool isThreadPrivate(VarDecl *D) {
143 DSAVarData DVar = getTopDSA(D);
144 return (DVar.CKind == OMPC_threadprivate || DVar.CKind == OMPC_copyin);
145 }
146
147 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000148 Scope *getCurScope() { return Stack.back().CurScope; }
149};
Alexey Bataeved09d242014-05-28 05:53:51 +0000150} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151
152DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
153 VarDecl *D) {
154 DSAVarData DVar;
155 if (Iter == Stack.rend() - 1) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000156 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
157 // in a region but not in construct]
158 // File-scope or namespace-scope variables referenced in called routines
159 // in the region are shared unless they appear in a threadprivate
160 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000161 if (!D->isFunctionOrMethodVarDecl())
162 DVar.CKind = OMPC_shared;
163
164 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
165 // in a region but not in construct]
166 // Variables with static storage duration that are declared in called
167 // routines in the region are shared.
168 if (D->hasGlobalStorage())
169 DVar.CKind = OMPC_shared;
170
Alexey Bataev758e55e2013-09-06 18:03:48 +0000171 return DVar;
172 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000173
Alexey Bataev758e55e2013-09-06 18:03:48 +0000174 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000175 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
176 // in a Construct, C/C++, predetermined, p.1]
177 // Variables with automatic storage duration that are declared in a scope
178 // inside the construct are private.
179 if (DVar.DKind != OMPD_parallel) {
180 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000181 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000182 DVar.CKind = OMPC_private;
183 return DVar;
184 }
185 }
186
Alexey Bataev758e55e2013-09-06 18:03:48 +0000187 // Explicitly specified attributes and local variables with predetermined
188 // attributes.
189 if (Iter->SharingMap.count(D)) {
190 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
191 DVar.CKind = Iter->SharingMap[D].Attributes;
192 return DVar;
193 }
194
195 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
196 // in a Construct, C/C++, implicitly determined, p.1]
197 // In a parallel or task construct, the data-sharing attributes of these
198 // variables are determined by the default clause, if present.
199 switch (Iter->DefaultAttr) {
200 case DSA_shared:
201 DVar.CKind = OMPC_shared;
202 return DVar;
203 case DSA_none:
204 return DVar;
205 case DSA_unspecified:
206 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
207 // in a Construct, implicitly determined, p.2]
208 // In a parallel construct, if no default clause is present, these
209 // variables are shared.
210 if (DVar.DKind == OMPD_parallel) {
211 DVar.CKind = OMPC_shared;
212 return DVar;
213 }
214
215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
216 // in a Construct, implicitly determined, p.4]
217 // In a task construct, if no default clause is present, a variable that in
218 // the enclosing context is determined to be shared by all implicit tasks
219 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000220 if (DVar.DKind == OMPD_task) {
221 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000222 for (StackTy::reverse_iterator I = std::next(Iter),
223 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000224 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000225 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
226 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000227 // in a Construct, implicitly determined, p.6]
228 // In a task construct, if no default clause is present, a variable
229 // whose data-sharing attribute is not determined by the rules above is
230 // firstprivate.
231 DVarTemp = getDSA(I, D);
232 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000233 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000234 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000235 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000236 return DVar;
237 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000238 if (I->Directive == OMPD_parallel)
239 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240 }
241 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000242 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000243 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000244 return DVar;
245 }
246 }
247 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
248 // in a Construct, implicitly determined, p.3]
249 // For constructs other than task, if no default clause is present, these
250 // variables inherit their data-sharing attributes from the enclosing
251 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000252 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253}
254
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000255DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
256 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
257 auto It = Stack.back().AlignedMap.find(D);
258 if (It == Stack.back().AlignedMap.end()) {
259 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
260 Stack.back().AlignedMap[D] = NewDE;
261 return nullptr;
262 } else {
263 assert(It->second && "Unexpected nullptr expr in the aligned map");
264 return It->second;
265 }
266 return nullptr;
267}
268
Alexey Bataev758e55e2013-09-06 18:03:48 +0000269void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
270 if (A == OMPC_threadprivate) {
271 Stack[0].SharingMap[D].Attributes = A;
272 Stack[0].SharingMap[D].RefExpr = E;
273 } else {
274 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
275 Stack.back().SharingMap[D].Attributes = A;
276 Stack.back().SharingMap[D].RefExpr = E;
277 }
278}
279
Alexey Bataeved09d242014-05-28 05:53:51 +0000280bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000281 if (Stack.size() > 2) {
282 reverse_iterator I = Iter, E = Stack.rend() - 1;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000283 Scope *TopScope = nullptr;
Fraser Cormack111023c2014-04-15 08:59:09 +0000284 while (I != E && I->Directive != OMPD_parallel) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000285 ++I;
286 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000287 if (I == E)
288 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000289 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000290 Scope *CurScope = getCurScope();
291 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000292 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000293 }
294 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000295 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000296 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000297}
298
299DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
300 DSAVarData DVar;
301
302 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
303 // in a Construct, C/C++, predetermined, p.1]
304 // Variables appearing in threadprivate directives are threadprivate.
305 if (D->getTLSKind() != VarDecl::TLS_None) {
306 DVar.CKind = OMPC_threadprivate;
307 return DVar;
308 }
309 if (Stack[0].SharingMap.count(D)) {
310 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
311 DVar.CKind = OMPC_threadprivate;
312 return DVar;
313 }
314
315 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
316 // in a Construct, C/C++, predetermined, p.1]
317 // Variables with automatic storage duration that are declared in a scope
318 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000319 OpenMPDirectiveKind Kind = getCurrentDirective();
320 if (Kind != OMPD_parallel) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000321 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000322 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000323 DVar.CKind = OMPC_private;
324 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000325 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000326 }
327
328 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
329 // in a Construct, C/C++, predetermined, p.4]
330 // Static data memebers are shared.
331 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000332 // Variables with const-qualified type having no mutable member may be
333 // listed
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000334 // in a firstprivate clause, even if they are static data members.
335 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
336 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
337 return DVar;
338
Alexey Bataev758e55e2013-09-06 18:03:48 +0000339 DVar.CKind = OMPC_shared;
340 return DVar;
341 }
342
343 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
344 bool IsConstant = Type.isConstant(Actions.getASTContext());
345 while (Type->isArrayType()) {
346 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
347 Type = ElemType.getNonReferenceType().getCanonicalType();
348 }
349 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
350 // in a Construct, C/C++, predetermined, p.6]
351 // Variables with const qualified type having no mutable member are
352 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000353 CXXRecordDecl *RD =
354 Actions.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000355 if (IsConstant &&
356 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
357 // Variables with const-qualified type having no mutable member may be
358 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000359 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
360 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
361 return DVar;
362
Alexey Bataev758e55e2013-09-06 18:03:48 +0000363 DVar.CKind = OMPC_shared;
364 return DVar;
365 }
366
367 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
368 // in a Construct, C/C++, predetermined, p.7]
369 // Variables with static storage duration that are declared in a scope
370 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000371 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000372 DVar.CKind = OMPC_shared;
373 return DVar;
374 }
375
376 // Explicitly specified attributes and local variables with predetermined
377 // attributes.
378 if (Stack.back().SharingMap.count(D)) {
379 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
380 DVar.CKind = Stack.back().SharingMap[D].Attributes;
381 }
382
383 return DVar;
384}
385
386DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000387 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000388}
389
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000390DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
391 OpenMPDirectiveKind DKind) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000392 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
393 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000394 I != E; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000395 if (DKind != OMPD_unknown && DKind != I->Directive)
396 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000397 DSAVarData DVar = getDSA(I, D);
398 if (DVar.CKind == CKind)
399 return DVar;
400 }
401 return DSAVarData();
402}
403
Alexey Bataevc5e02582014-06-16 07:08:35 +0000404DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(VarDecl *D,
405 OpenMPClauseKind CKind,
406 OpenMPDirectiveKind DKind) {
407 assert(DKind != OMPD_unknown && "Directive must be specified explicitly");
408 for (auto I = Stack.rbegin(), EE = std::prev(Stack.rend()); I != EE; ++I) {
409 if (DKind != I->Directive)
410 continue;
411 DSAVarData DVar = getDSA(I, D);
412 if (DVar.CKind == CKind)
413 return DVar;
414 return DSAVarData();
415 }
416 return DSAVarData();
417}
418
Alexey Bataev758e55e2013-09-06 18:03:48 +0000419void Sema::InitDataSharingAttributesStack() {
420 VarDataSharingAttributesStack = new DSAStackTy(*this);
421}
422
423#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
424
Alexey Bataeved09d242014-05-28 05:53:51 +0000425void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000426
427void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
428 const DeclarationNameInfo &DirName,
429 Scope *CurScope) {
430 DSAStack->push(DKind, DirName, CurScope);
431 PushExpressionEvaluationContext(PotentiallyEvaluated);
432}
433
434void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
435 DSAStack->pop();
436 DiscardCleanupsInEvaluationContext();
437 PopExpressionEvaluationContext();
438}
439
Alexey Bataeva769e072013-03-22 06:34:35 +0000440namespace {
441
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000442class VarDeclFilterCCC : public CorrectionCandidateCallback {
443private:
444 Sema &Actions;
Alexey Bataeved09d242014-05-28 05:53:51 +0000445
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000446public:
Alexey Bataeved09d242014-05-28 05:53:51 +0000447 VarDeclFilterCCC(Sema &S) : Actions(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000448 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000449 NamedDecl *ND = Candidate.getCorrectionDecl();
450 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
451 return VD->hasGlobalStorage() &&
452 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
453 Actions.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000454 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000455 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000456 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000457};
Alexey Bataeved09d242014-05-28 05:53:51 +0000458} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000459
460ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
461 CXXScopeSpec &ScopeSpec,
462 const DeclarationNameInfo &Id) {
463 LookupResult Lookup(*this, Id, LookupOrdinaryName);
464 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
465
466 if (Lookup.isAmbiguous())
467 return ExprError();
468
469 VarDecl *VD;
470 if (!Lookup.isSingleResult()) {
471 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000472 if (TypoCorrection Corrected =
473 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
474 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000475 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000476 PDiag(Lookup.empty()
477 ? diag::err_undeclared_var_use_suggest
478 : diag::err_omp_expected_var_arg_suggest)
479 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000480 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000481 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000482 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
483 : diag::err_omp_expected_var_arg)
484 << Id.getName();
485 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000486 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000487 } else {
488 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000489 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000490 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
491 return ExprError();
492 }
493 }
494 Lookup.suppressDiagnostics();
495
496 // OpenMP [2.9.2, Syntax, C/C++]
497 // Variables must be file-scope, namespace-scope, or static block-scope.
498 if (!VD->hasGlobalStorage()) {
499 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000500 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
501 bool IsDecl =
502 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000503 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000504 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
505 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000506 return ExprError();
507 }
508
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000509 VarDecl *CanonicalVD = VD->getCanonicalDecl();
510 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000511 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
512 // A threadprivate directive for file-scope variables must appear outside
513 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000514 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
515 !getCurLexicalContext()->isTranslationUnit()) {
516 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000517 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
518 bool IsDecl =
519 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
520 Diag(VD->getLocation(),
521 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
522 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000523 return ExprError();
524 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000525 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
526 // A threadprivate directive for static class member variables must appear
527 // in the class definition, in the same scope in which the member
528 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000529 if (CanonicalVD->isStaticDataMember() &&
530 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
531 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000532 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
533 bool IsDecl =
534 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
535 Diag(VD->getLocation(),
536 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
537 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000538 return ExprError();
539 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000540 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
541 // A threadprivate directive for namespace-scope variables must appear
542 // outside any definition or declaration other than the namespace
543 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000544 if (CanonicalVD->getDeclContext()->isNamespace() &&
545 (!getCurLexicalContext()->isFileContext() ||
546 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
547 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000548 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
549 bool IsDecl =
550 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
551 Diag(VD->getLocation(),
552 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
553 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000554 return ExprError();
555 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000556 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
557 // A threadprivate directive for static block-scope variables must appear
558 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000559 if (CanonicalVD->isStaticLocal() && CurScope &&
560 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000561 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000562 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
563 bool IsDecl =
564 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
565 Diag(VD->getLocation(),
566 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
567 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000568 return ExprError();
569 }
570
571 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
572 // A threadprivate directive must lexically precede all references to any
573 // of the variables in its list.
574 if (VD->isUsed()) {
575 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000576 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000577 return ExprError();
578 }
579
580 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000581 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000582 return DE;
583}
584
Alexey Bataeved09d242014-05-28 05:53:51 +0000585Sema::DeclGroupPtrTy
586Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
587 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000588 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000589 CurContext->addDecl(D);
590 return DeclGroupPtrTy::make(DeclGroupRef(D));
591 }
592 return DeclGroupPtrTy();
593}
594
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000595namespace {
596class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
597 Sema &SemaRef;
598
599public:
600 bool VisitDeclRefExpr(const DeclRefExpr *E) {
601 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
602 if (VD->hasLocalStorage()) {
603 SemaRef.Diag(E->getLocStart(),
604 diag::err_omp_local_var_in_threadprivate_init)
605 << E->getSourceRange();
606 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
607 << VD << VD->getSourceRange();
608 return true;
609 }
610 }
611 return false;
612 }
613 bool VisitStmt(const Stmt *S) {
614 for (auto Child : S->children()) {
615 if (Child && Visit(Child))
616 return true;
617 }
618 return false;
619 }
620 LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
621};
622} // namespace
623
Alexey Bataeved09d242014-05-28 05:53:51 +0000624OMPThreadPrivateDecl *
625Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000626 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000627 for (auto &RefExpr : VarList) {
628 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000629 VarDecl *VD = cast<VarDecl>(DE->getDecl());
630 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000631
632 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
633 // A threadprivate variable must not have an incomplete type.
634 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000635 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000636 continue;
637 }
638
639 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
640 // A threadprivate variable must not have a reference type.
641 if (VD->getType()->isReferenceType()) {
642 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000643 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
644 bool IsDecl =
645 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
646 Diag(VD->getLocation(),
647 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
648 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000649 continue;
650 }
651
Richard Smithfd3834f2013-04-13 02:43:54 +0000652 // Check if this is a TLS variable.
653 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000654 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000655 bool IsDecl =
656 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
657 Diag(VD->getLocation(),
658 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
659 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000660 continue;
661 }
662
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000663 // Check if initial value of threadprivate variable reference variable with
664 // local storage (it is not supported by runtime).
665 if (auto Init = VD->getAnyInitializer()) {
666 LocalVarRefChecker Checker(*this);
667 if (Checker.Visit(Init)) continue;
668 }
669
Alexey Bataeved09d242014-05-28 05:53:51 +0000670 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000671 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000672 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000673 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000674 if (!Vars.empty()) {
675 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
676 Vars);
677 D->setAccess(AS_public);
678 }
679 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000680}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000681
Alexey Bataev758e55e2013-09-06 18:03:48 +0000682namespace {
683class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
684 DSAStackTy *Stack;
685 Sema &Actions;
686 bool ErrorFound;
687 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000688 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataeved09d242014-05-28 05:53:51 +0000689
Alexey Bataev758e55e2013-09-06 18:03:48 +0000690public:
691 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000692 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000694 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
695 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000696
697 SourceLocation ELoc = E->getExprLoc();
698
699 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
700 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
701 if (DVar.CKind != OMPC_unknown) {
702 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000703 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000704 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705 return;
706 }
707 // The default(none) clause requires that each variable that is referenced
708 // in the construct, and does not have a predetermined data-sharing
709 // attribute, must have its data-sharing attribute explicitly determined
710 // by being listed in a data-sharing attribute clause.
711 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
712 (DKind == OMPD_parallel || DKind == OMPD_task)) {
713 ErrorFound = true;
714 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
715 return;
716 }
717
718 // OpenMP [2.9.3.6, Restrictions, p.2]
719 // A list item that appears in a reduction clause of the innermost
720 // enclosing worksharing or parallel construct may not be accessed in an
721 // explicit task.
Alexey Bataevc5e02582014-06-16 07:08:35 +0000722 DVar = Stack->hasInnermostDSA(VD, OMPC_reduction, OMPD_parallel);
723 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
724 ErrorFound = true;
725 Actions.Diag(ELoc, diag::err_omp_reduction_in_task);
726 if (DVar.RefExpr) {
727 Actions.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
728 << getOpenMPClauseName(OMPC_reduction);
729 }
730 return;
731 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000732
733 // Define implicit data-sharing attributes for task.
734 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000735 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
736 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000737 }
738 }
739 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000740 for (auto C : S->clauses())
741 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000742 for (StmtRange R = C->children(); R; ++R)
743 if (Stmt *Child = *R)
744 Visit(Child);
745 }
746 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000747 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
748 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000749 if (Stmt *Child = *I)
750 if (!isa<OMPExecutableDirective>(Child))
751 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000752 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000753
754 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000755 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000756
757 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
Alexey Bataeved09d242014-05-28 05:53:51 +0000758 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000759};
Alexey Bataeved09d242014-05-28 05:53:51 +0000760} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000761
Alexey Bataev9959db52014-05-06 10:08:46 +0000762void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
763 Scope *CurScope) {
764 switch (DKind) {
765 case OMPD_parallel: {
766 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
767 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
768 Sema::CapturedParamNameType Params[3] = {
769 std::make_pair(".global_tid.", KmpInt32PtrTy),
770 std::make_pair(".bound_tid.", KmpInt32PtrTy),
771 std::make_pair(StringRef(), QualType()) // __context with shared vars
772 };
773 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
774 break;
775 }
776 case OMPD_simd: {
777 Sema::CapturedParamNameType Params[1] = {
778 std::make_pair(StringRef(), QualType()) // __context with shared vars
779 };
780 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
781 break;
782 }
783 case OMPD_threadprivate:
784 case OMPD_task:
785 llvm_unreachable("OpenMP Directive is not allowed");
786 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000787 llvm_unreachable("Unknown OpenMP directive");
788 }
789}
790
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000791StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
792 ArrayRef<OMPClause *> Clauses,
793 Stmt *AStmt,
794 SourceLocation StartLoc,
795 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000796 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
797
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000798 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000799
800 // Check default data sharing attributes for referenced variables.
801 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
802 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
803 if (DSAChecker.isErrorFound())
804 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000805 // Generate list of implicitly defined firstprivate variables.
806 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
807 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
808
809 bool ErrorFound = false;
810 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000811 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
812 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
813 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000814 ClausesWithImplicit.push_back(Implicit);
815 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +0000816 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000817 } else
818 ErrorFound = true;
819 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000820
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000821 switch (Kind) {
822 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +0000823 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
824 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000825 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000826 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +0000827 Res =
828 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000829 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000830 case OMPD_threadprivate:
831 case OMPD_task:
832 llvm_unreachable("OpenMP Directive is not allowed");
833 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000834 llvm_unreachable("Unknown OpenMP directive");
835 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000836
Alexey Bataeved09d242014-05-28 05:53:51 +0000837 if (ErrorFound)
838 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000839 return Res;
840}
841
842StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
843 Stmt *AStmt,
844 SourceLocation StartLoc,
845 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000846 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
847 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
848 // 1.2.2 OpenMP Language Terminology
849 // Structured block - An executable statement with a single entry at the
850 // top and a single exit at the bottom.
851 // The point of exit cannot be a branch out of the structured block.
852 // longjmp() and throw() must not violate the entry/exit criteria.
853 CS->getCapturedDecl()->setNothrow();
854
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000855 getCurFunction()->setHasBranchProtectedScope();
856
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000857 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
858 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000859}
860
Alexander Musman1bb328c2014-06-04 13:06:39 +0000861static bool isSimdDirective(OpenMPDirectiveKind DKind) {
862 return DKind == OMPD_simd; // FIXME: || DKind == OMPD_for_simd || ...
863}
864
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000865namespace {
866/// \brief Helper class for checking canonical form of the OpenMP loops and
867/// extracting iteration space of each loop in the loop nest, that will be used
868/// for IR generation.
869class OpenMPIterationSpaceChecker {
870 /// \brief Reference to Sema.
871 Sema &SemaRef;
872 /// \brief A location for diagnostics (when there is no some better location).
873 SourceLocation DefaultLoc;
874 /// \brief A location for diagnostics (when increment is not compatible).
875 SourceLocation ConditionLoc;
876 /// \brief A source location for referring to condition later.
877 SourceRange ConditionSrcRange;
878 /// \brief Loop variable.
879 VarDecl *Var;
880 /// \brief Lower bound (initializer for the var).
881 Expr *LB;
882 /// \brief Upper bound.
883 Expr *UB;
884 /// \brief Loop step (increment).
885 Expr *Step;
886 /// \brief This flag is true when condition is one of:
887 /// Var < UB
888 /// Var <= UB
889 /// UB > Var
890 /// UB >= Var
891 bool TestIsLessOp;
892 /// \brief This flag is true when condition is strict ( < or > ).
893 bool TestIsStrictOp;
894 /// \brief This flag is true when step is subtracted on each iteration.
895 bool SubtractStep;
896
897public:
898 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
899 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
900 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
901 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
902 SubtractStep(false) {}
903 /// \brief Check init-expr for canonical loop form and save loop counter
904 /// variable - #Var and its initialization value - #LB.
905 bool CheckInit(Stmt *S);
906 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
907 /// for less/greater and for strict/non-strict comparison.
908 bool CheckCond(Expr *S);
909 /// \brief Check incr-expr for canonical loop form and return true if it
910 /// does not conform, otherwise save loop step (#Step).
911 bool CheckInc(Expr *S);
912 /// \brief Return the loop counter variable.
913 VarDecl *GetLoopVar() const { return Var; }
914 /// \brief Return true if any expression is dependent.
915 bool Dependent() const;
916
917private:
918 /// \brief Check the right-hand side of an assignment in the increment
919 /// expression.
920 bool CheckIncRHS(Expr *RHS);
921 /// \brief Helper to set loop counter variable and its initializer.
922 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
923 /// \brief Helper to set upper bound.
924 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
925 const SourceLocation &SL);
926 /// \brief Helper to set loop increment.
927 bool SetStep(Expr *NewStep, bool Subtract);
928};
929
930bool OpenMPIterationSpaceChecker::Dependent() const {
931 if (!Var) {
932 assert(!LB && !UB && !Step);
933 return false;
934 }
935 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
936 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
937}
938
939bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
940 // State consistency checking to ensure correct usage.
941 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
942 !TestIsLessOp && !TestIsStrictOp);
943 if (!NewVar || !NewLB)
944 return true;
945 Var = NewVar;
946 LB = NewLB;
947 return false;
948}
949
950bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
951 const SourceRange &SR,
952 const SourceLocation &SL) {
953 // State consistency checking to ensure correct usage.
954 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
955 !TestIsLessOp && !TestIsStrictOp);
956 if (!NewUB)
957 return true;
958 UB = NewUB;
959 TestIsLessOp = LessOp;
960 TestIsStrictOp = StrictOp;
961 ConditionSrcRange = SR;
962 ConditionLoc = SL;
963 return false;
964}
965
966bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
967 // State consistency checking to ensure correct usage.
968 assert(Var != nullptr && LB != nullptr && Step == nullptr);
969 if (!NewStep)
970 return true;
971 if (!NewStep->isValueDependent()) {
972 // Check that the step is integer expression.
973 SourceLocation StepLoc = NewStep->getLocStart();
974 ExprResult Val =
975 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
976 if (Val.isInvalid())
977 return true;
978 NewStep = Val.get();
979
980 // OpenMP [2.6, Canonical Loop Form, Restrictions]
981 // If test-expr is of form var relational-op b and relational-op is < or
982 // <= then incr-expr must cause var to increase on each iteration of the
983 // loop. If test-expr is of form var relational-op b and relational-op is
984 // > or >= then incr-expr must cause var to decrease on each iteration of
985 // the loop.
986 // If test-expr is of form b relational-op var and relational-op is < or
987 // <= then incr-expr must cause var to decrease on each iteration of the
988 // loop. If test-expr is of form b relational-op var and relational-op is
989 // > or >= then incr-expr must cause var to increase on each iteration of
990 // the loop.
991 llvm::APSInt Result;
992 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
993 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
994 bool IsConstNeg =
995 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
996 bool IsConstZero = IsConstant && !Result.getBoolValue();
997 if (UB && (IsConstZero ||
998 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
999 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1000 SemaRef.Diag(NewStep->getExprLoc(),
1001 diag::err_omp_loop_incr_not_compatible)
1002 << Var << TestIsLessOp << NewStep->getSourceRange();
1003 SemaRef.Diag(ConditionLoc,
1004 diag::note_omp_loop_cond_requres_compatible_incr)
1005 << TestIsLessOp << ConditionSrcRange;
1006 return true;
1007 }
1008 }
1009
1010 Step = NewStep;
1011 SubtractStep = Subtract;
1012 return false;
1013}
1014
1015bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1016 // Check init-expr for canonical loop form and save loop counter
1017 // variable - #Var and its initialization value - #LB.
1018 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1019 // var = lb
1020 // integer-type var = lb
1021 // random-access-iterator-type var = lb
1022 // pointer-type var = lb
1023 //
1024 if (!S) {
1025 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1026 return true;
1027 }
1028 if (Expr *E = dyn_cast<Expr>(S))
1029 S = E->IgnoreParens();
1030 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1031 if (BO->getOpcode() == BO_Assign)
1032 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1033 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1034 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1035 if (DS->isSingleDecl()) {
1036 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1037 if (Var->hasInit()) {
1038 // Accept non-canonical init form here but emit ext. warning.
1039 if (Var->getInitStyle() != VarDecl::CInit)
1040 SemaRef.Diag(S->getLocStart(),
1041 diag::ext_omp_loop_not_canonical_init)
1042 << S->getSourceRange();
1043 return SetVarAndLB(Var, Var->getInit());
1044 }
1045 }
1046 }
1047 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1048 if (CE->getOperator() == OO_Equal)
1049 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1050 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1051
1052 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1053 << S->getSourceRange();
1054 return true;
1055}
1056
1057/// \brief Ignore parenthesises, implicit casts, copy constructor and return the
1058/// variable (which may be the loop variable) if possible.
1059static const VarDecl *GetInitVarDecl(const Expr *E) {
1060 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001061 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001062 E = E->IgnoreParenImpCasts();
1063 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1064 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1065 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1066 CE->getArg(0) != nullptr)
1067 E = CE->getArg(0)->IgnoreParenImpCasts();
1068 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1069 if (!DRE)
1070 return nullptr;
1071 return dyn_cast<VarDecl>(DRE->getDecl());
1072}
1073
1074bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1075 // Check test-expr for canonical form, save upper-bound UB, flags for
1076 // less/greater and for strict/non-strict comparison.
1077 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1078 // var relational-op b
1079 // b relational-op var
1080 //
1081 if (!S) {
1082 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1083 return true;
1084 }
1085 S = S->IgnoreParenImpCasts();
1086 SourceLocation CondLoc = S->getLocStart();
1087 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1088 if (BO->isRelationalOp()) {
1089 if (GetInitVarDecl(BO->getLHS()) == Var)
1090 return SetUB(BO->getRHS(),
1091 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1092 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1093 BO->getSourceRange(), BO->getOperatorLoc());
1094 if (GetInitVarDecl(BO->getRHS()) == Var)
1095 return SetUB(BO->getLHS(),
1096 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1097 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1098 BO->getSourceRange(), BO->getOperatorLoc());
1099 }
1100 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1101 if (CE->getNumArgs() == 2) {
1102 auto Op = CE->getOperator();
1103 switch (Op) {
1104 case OO_Greater:
1105 case OO_GreaterEqual:
1106 case OO_Less:
1107 case OO_LessEqual:
1108 if (GetInitVarDecl(CE->getArg(0)) == Var)
1109 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1110 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1111 CE->getOperatorLoc());
1112 if (GetInitVarDecl(CE->getArg(1)) == Var)
1113 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1114 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1115 CE->getOperatorLoc());
1116 break;
1117 default:
1118 break;
1119 }
1120 }
1121 }
1122 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1123 << S->getSourceRange() << Var;
1124 return true;
1125}
1126
1127bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1128 // RHS of canonical loop form increment can be:
1129 // var + incr
1130 // incr + var
1131 // var - incr
1132 //
1133 RHS = RHS->IgnoreParenImpCasts();
1134 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1135 if (BO->isAdditiveOp()) {
1136 bool IsAdd = BO->getOpcode() == BO_Add;
1137 if (GetInitVarDecl(BO->getLHS()) == Var)
1138 return SetStep(BO->getRHS(), !IsAdd);
1139 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1140 return SetStep(BO->getLHS(), false);
1141 }
1142 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1143 bool IsAdd = CE->getOperator() == OO_Plus;
1144 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1145 if (GetInitVarDecl(CE->getArg(0)) == Var)
1146 return SetStep(CE->getArg(1), !IsAdd);
1147 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1148 return SetStep(CE->getArg(0), false);
1149 }
1150 }
1151 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1152 << RHS->getSourceRange() << Var;
1153 return true;
1154}
1155
1156bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1157 // Check incr-expr for canonical loop form and return true if it
1158 // does not conform.
1159 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1160 // ++var
1161 // var++
1162 // --var
1163 // var--
1164 // var += incr
1165 // var -= incr
1166 // var = var + incr
1167 // var = incr + var
1168 // var = var - incr
1169 //
1170 if (!S) {
1171 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1172 return true;
1173 }
1174 S = S->IgnoreParens();
1175 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1176 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1177 return SetStep(
1178 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1179 (UO->isDecrementOp() ? -1 : 1)).get(),
1180 false);
1181 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1182 switch (BO->getOpcode()) {
1183 case BO_AddAssign:
1184 case BO_SubAssign:
1185 if (GetInitVarDecl(BO->getLHS()) == Var)
1186 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1187 break;
1188 case BO_Assign:
1189 if (GetInitVarDecl(BO->getLHS()) == Var)
1190 return CheckIncRHS(BO->getRHS());
1191 break;
1192 default:
1193 break;
1194 }
1195 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1196 switch (CE->getOperator()) {
1197 case OO_PlusPlus:
1198 case OO_MinusMinus:
1199 if (GetInitVarDecl(CE->getArg(0)) == Var)
1200 return SetStep(
1201 SemaRef.ActOnIntegerConstant(
1202 CE->getLocStart(),
1203 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1204 false);
1205 break;
1206 case OO_PlusEqual:
1207 case OO_MinusEqual:
1208 if (GetInitVarDecl(CE->getArg(0)) == Var)
1209 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1210 break;
1211 case OO_Equal:
1212 if (GetInitVarDecl(CE->getArg(0)) == Var)
1213 return CheckIncRHS(CE->getArg(1));
1214 break;
1215 default:
1216 break;
1217 }
1218 }
1219 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1220 << S->getSourceRange() << Var;
1221 return true;
1222}
1223}
1224
1225/// \brief Called on a for stmt to check and extract its iteration space
1226/// for further processing (such as collapsing).
1227static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
1228 Sema &SemaRef, DSAStackTy &DSA) {
1229 // OpenMP [2.6, Canonical Loop Form]
1230 // for (init-expr; test-expr; incr-expr) structured-block
1231 auto For = dyn_cast_or_null<ForStmt>(S);
1232 if (!For) {
1233 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
1234 << getOpenMPDirectiveName(DKind);
1235 return true;
1236 }
1237 assert(For->getBody());
1238
1239 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1240
1241 // Check init.
1242 Stmt *Init = For->getInit();
1243 if (ISC.CheckInit(Init)) {
1244 return true;
1245 }
1246
1247 bool HasErrors = false;
1248
1249 // Check loop variable's type.
1250 VarDecl *Var = ISC.GetLoopVar();
1251
1252 // OpenMP [2.6, Canonical Loop Form]
1253 // Var is one of the following:
1254 // A variable of signed or unsigned integer type.
1255 // For C++, a variable of a random access iterator type.
1256 // For C, a variable of a pointer type.
1257 QualType VarType = Var->getType();
1258 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1259 !VarType->isPointerType() &&
1260 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1261 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1262 << SemaRef.getLangOpts().CPlusPlus;
1263 HasErrors = true;
1264 }
1265
1266 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1267 // a Construct, C/C++].
1268 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1269 // parallel for construct may be listed in a private or lastprivate clause.
1270 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexander Musman1bb328c2014-06-04 13:06:39 +00001271 if (isSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1272 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate &&
1273 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001274 // The loop iteration variable in the associated for-loop of a simd
1275 // construct with just one associated for-loop may be listed in a linear
1276 // clause with a constant-linear-step that is the increment of the
1277 // associated for-loop.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001278 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1279 << getOpenMPClauseName(DVar.CKind);
1280 if (DVar.RefExpr)
1281 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1282 << getOpenMPClauseName(DVar.CKind);
1283 else
1284 SemaRef.Diag(Var->getLocation(), diag::note_omp_predetermined_dsa)
1285 << getOpenMPClauseName(DVar.CKind);
1286 HasErrors = true;
1287 } else {
1288 // Make the loop iteration variable private by default.
1289 DSA.addDSA(Var, nullptr, OMPC_private);
1290 }
1291
Alexander Musman1bb328c2014-06-04 13:06:39 +00001292 assert(isSimdDirective(DKind) && "DSA for non-simd loop vars");
1293
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001294 // Check test-expr.
1295 HasErrors |= ISC.CheckCond(For->getCond());
1296
1297 // Check incr-expr.
1298 HasErrors |= ISC.CheckInc(For->getInc());
1299
1300 if (ISC.Dependent())
1301 return HasErrors;
1302
1303 // FIXME: Build loop's iteration space representation.
1304 return HasErrors;
1305}
1306
1307/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1308/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1309/// to get the first for loop.
1310static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1311 if (IgnoreCaptured)
1312 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1313 S = CapS->getCapturedStmt();
1314 // OpenMP [2.8.1, simd construct, Restrictions]
1315 // All loops associated with the construct must be perfectly nested; that is,
1316 // there must be no intervening code nor any OpenMP directive between any two
1317 // loops.
1318 while (true) {
1319 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1320 S = AS->getSubStmt();
1321 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1322 if (CS->size() != 1)
1323 break;
1324 S = CS->body_back();
1325 } else
1326 break;
1327 }
1328 return S;
1329}
1330
1331/// \brief Called on a for stmt to check itself and nested loops (if any).
1332static bool CheckOpenMPLoop(OpenMPDirectiveKind DKind, unsigned NestedLoopCount,
1333 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA) {
1334 // This is helper routine for loop directives (e.g., 'for', 'simd',
1335 // 'for simd', etc.).
1336 assert(NestedLoopCount == 1);
1337 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1338 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
1339 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA))
1340 return true;
1341 // Move on to the next nested for loop, or to the loop body.
1342 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1343 }
1344
1345 // FIXME: Build resulting iteration space for IR generation (collapsing
1346 // iteration spaces when loop count > 1 ('collapse' clause)).
1347 return false;
1348}
1349
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001350StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001351 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001352 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001353 // In presence of clause 'collapse', it will define the nested loops number.
1354 // For now, pass default value of 1.
1355 if (CheckOpenMPLoop(OMPD_simd, 1, AStmt, *this, *DSAStack))
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001356 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001357
1358 getCurFunction()->setHasBranchProtectedScope();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001359 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001360}
1361
Alexey Bataeved09d242014-05-28 05:53:51 +00001362OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001363 SourceLocation StartLoc,
1364 SourceLocation LParenLoc,
1365 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001366 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001367 switch (Kind) {
1368 case OMPC_if:
1369 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1370 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001371 case OMPC_num_threads:
1372 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1373 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001374 case OMPC_safelen:
1375 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1376 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001377 case OMPC_collapse:
1378 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1379 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001380 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001381 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001382 case OMPC_private:
1383 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001384 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001385 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001386 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001387 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001388 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001389 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001390 case OMPC_threadprivate:
1391 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001392 llvm_unreachable("Clause is not allowed.");
1393 }
1394 return Res;
1395}
1396
Alexey Bataeved09d242014-05-28 05:53:51 +00001397OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001398 SourceLocation LParenLoc,
1399 SourceLocation EndLoc) {
1400 Expr *ValExpr = Condition;
1401 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1402 !Condition->isInstantiationDependent() &&
1403 !Condition->containsUnexpandedParameterPack()) {
1404 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001405 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001406 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001407 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001408
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001409 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001410 }
1411
1412 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1413}
1414
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001415ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1416 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001417 if (!Op)
1418 return ExprError();
1419
1420 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1421 public:
1422 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001423 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001424 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1425 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001426 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1427 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001428 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1429 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001430 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1431 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001432 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1433 QualType T,
1434 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001435 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1436 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001437 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1438 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001439 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001440 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001441 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001442 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1443 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001444 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1445 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001446 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1447 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001448 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001449 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001450 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001451 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1452 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001453 llvm_unreachable("conversion functions are permitted");
1454 }
1455 } ConvertDiagnoser;
1456 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1457}
1458
1459OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1460 SourceLocation StartLoc,
1461 SourceLocation LParenLoc,
1462 SourceLocation EndLoc) {
1463 Expr *ValExpr = NumThreads;
1464 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1465 !NumThreads->isInstantiationDependent() &&
1466 !NumThreads->containsUnexpandedParameterPack()) {
1467 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1468 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001469 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001470 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001471 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001472
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001473 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001474
1475 // OpenMP [2.5, Restrictions]
1476 // The num_threads expression must evaluate to a positive integer value.
1477 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001478 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1479 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001480 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1481 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001482 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001483 }
1484 }
1485
Alexey Bataeved09d242014-05-28 05:53:51 +00001486 return new (Context)
1487 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001488}
1489
Alexey Bataev62c87d22014-03-21 04:51:18 +00001490ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1491 OpenMPClauseKind CKind) {
1492 if (!E)
1493 return ExprError();
1494 if (E->isValueDependent() || E->isTypeDependent() ||
1495 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001496 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001497 llvm::APSInt Result;
1498 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1499 if (ICE.isInvalid())
1500 return ExprError();
1501 if (!Result.isStrictlyPositive()) {
1502 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1503 << getOpenMPClauseName(CKind) << E->getSourceRange();
1504 return ExprError();
1505 }
1506 return ICE;
1507}
1508
1509OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1510 SourceLocation LParenLoc,
1511 SourceLocation EndLoc) {
1512 // OpenMP [2.8.1, simd construct, Description]
1513 // The parameter of the safelen clause must be a constant
1514 // positive integer expression.
1515 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1516 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001517 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001518 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001519 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001520}
1521
Alexander Musman64d33f12014-06-04 07:53:32 +00001522OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1523 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001524 SourceLocation LParenLoc,
1525 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001526 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001527 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001528 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001529 // The parameter of the collapse clause must be a constant
1530 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001531 ExprResult NumForLoopsResult =
1532 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1533 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001534 return nullptr;
1535 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001536 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001537}
1538
Alexey Bataeved09d242014-05-28 05:53:51 +00001539OMPClause *Sema::ActOnOpenMPSimpleClause(
1540 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1541 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001542 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001543 switch (Kind) {
1544 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001545 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001546 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1547 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001548 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001549 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001550 Res = ActOnOpenMPProcBindClause(
1551 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1552 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001553 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001554 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001555 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001556 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001557 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001558 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001559 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001560 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001561 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001562 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001563 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001564 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001565 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001566 case OMPC_threadprivate:
1567 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001568 llvm_unreachable("Clause is not allowed.");
1569 }
1570 return Res;
1571}
1572
1573OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1574 SourceLocation KindKwLoc,
1575 SourceLocation StartLoc,
1576 SourceLocation LParenLoc,
1577 SourceLocation EndLoc) {
1578 if (Kind == OMPC_DEFAULT_unknown) {
1579 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001580 static_assert(OMPC_DEFAULT_unknown > 0,
1581 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001582 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001583 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001584 Values += "'";
1585 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1586 Values += "'";
1587 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001588 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001589 Values += " or ";
1590 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001591 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001592 break;
1593 default:
1594 Values += Sep;
1595 break;
1596 }
1597 }
1598 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001599 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001600 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001601 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001602 switch (Kind) {
1603 case OMPC_DEFAULT_none:
1604 DSAStack->setDefaultDSANone();
1605 break;
1606 case OMPC_DEFAULT_shared:
1607 DSAStack->setDefaultDSAShared();
1608 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001609 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001610 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001611 break;
1612 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001613 return new (Context)
1614 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001615}
1616
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001617OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1618 SourceLocation KindKwLoc,
1619 SourceLocation StartLoc,
1620 SourceLocation LParenLoc,
1621 SourceLocation EndLoc) {
1622 if (Kind == OMPC_PROC_BIND_unknown) {
1623 std::string Values;
1624 std::string Sep(", ");
1625 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1626 Values += "'";
1627 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1628 Values += "'";
1629 switch (i) {
1630 case OMPC_PROC_BIND_unknown - 2:
1631 Values += " or ";
1632 break;
1633 case OMPC_PROC_BIND_unknown - 1:
1634 break;
1635 default:
1636 Values += Sep;
1637 break;
1638 }
1639 }
1640 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001641 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001642 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001643 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001644 return new (Context)
1645 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001646}
1647
Alexey Bataevc5e02582014-06-16 07:08:35 +00001648OMPClause *Sema::ActOnOpenMPVarListClause(
1649 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
1650 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
1651 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
1652 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001653 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001654 switch (Kind) {
1655 case OMPC_private:
1656 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1657 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001658 case OMPC_firstprivate:
1659 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1660 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00001661 case OMPC_lastprivate:
1662 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1663 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001664 case OMPC_shared:
1665 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1666 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001667 case OMPC_reduction:
1668 Res =
1669 ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
1670 EndLoc, ReductionIdScopeSpec, ReductionId);
1671 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001672 case OMPC_linear:
1673 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1674 ColonLoc, EndLoc);
1675 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001676 case OMPC_aligned:
1677 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
1678 ColonLoc, EndLoc);
1679 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001680 case OMPC_copyin:
1681 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1682 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001683 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001684 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001685 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001686 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001687 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001688 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001689 case OMPC_threadprivate:
1690 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001691 llvm_unreachable("Clause is not allowed.");
1692 }
1693 return Res;
1694}
1695
1696OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1697 SourceLocation StartLoc,
1698 SourceLocation LParenLoc,
1699 SourceLocation EndLoc) {
1700 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001701 for (auto &RefExpr : VarList) {
1702 assert(RefExpr && "NULL expr in OpenMP private clause.");
1703 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001704 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001705 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001706 continue;
1707 }
1708
Alexey Bataeved09d242014-05-28 05:53:51 +00001709 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001710 // OpenMP [2.1, C/C++]
1711 // A list item is a variable name.
1712 // OpenMP [2.9.3.3, Restrictions, p.1]
1713 // A variable that is part of another variable (as an array or
1714 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001715 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001716 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001717 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001718 continue;
1719 }
1720 Decl *D = DE->getDecl();
1721 VarDecl *VD = cast<VarDecl>(D);
1722
1723 QualType Type = VD->getType();
1724 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1725 // It will be analyzed later.
1726 Vars.push_back(DE);
1727 continue;
1728 }
1729
1730 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1731 // A variable that appears in a private clause must not have an incomplete
1732 // type or a reference type.
1733 if (RequireCompleteType(ELoc, Type,
1734 diag::err_omp_private_incomplete_type)) {
1735 continue;
1736 }
1737 if (Type->isReferenceType()) {
1738 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001739 << getOpenMPClauseName(OMPC_private) << Type;
1740 bool IsDecl =
1741 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1742 Diag(VD->getLocation(),
1743 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1744 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001745 continue;
1746 }
1747
1748 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1749 // A variable of class type (or array thereof) that appears in a private
1750 // clause requires an accesible, unambiguous default constructor for the
1751 // class type.
1752 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001753 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
1754 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001755 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001756 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1757 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1758 : nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001759 if (RD) {
1760 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1761 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001762 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1763 if (!CD || CheckConstructorAccess(
1764 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1765 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001766 CD->isDeleted()) {
1767 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001768 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001769 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1770 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001771 Diag(VD->getLocation(),
1772 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1773 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001774 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1775 continue;
1776 }
1777 MarkFunctionReferenced(ELoc, CD);
1778 DiagnoseUseOfDecl(CD, ELoc);
1779
1780 CXXDestructorDecl *DD = RD->getDestructor();
1781 if (DD) {
1782 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1783 DD->isDeleted()) {
1784 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001785 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001786 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1787 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001788 Diag(VD->getLocation(),
1789 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1790 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001791 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1792 continue;
1793 }
1794 MarkFunctionReferenced(ELoc, DD);
1795 DiagnoseUseOfDecl(DD, ELoc);
1796 }
1797 }
1798
Alexey Bataev758e55e2013-09-06 18:03:48 +00001799 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1800 // in a Construct]
1801 // Variables with the predetermined data-sharing attributes may not be
1802 // listed in data-sharing attributes clauses, except for the cases
1803 // listed below. For these exceptions only, listing a predetermined
1804 // variable in a data-sharing attribute clause is allowed and overrides
1805 // the variable's predetermined data-sharing attributes.
1806 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1807 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001808 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1809 << getOpenMPClauseName(OMPC_private);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001810 if (DVar.RefExpr) {
1811 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001812 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001813 } else {
1814 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001815 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001816 }
1817 continue;
1818 }
1819
1820 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001821 Vars.push_back(DE);
1822 }
1823
Alexey Bataeved09d242014-05-28 05:53:51 +00001824 if (Vars.empty())
1825 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001826
1827 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1828}
1829
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001830OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1831 SourceLocation StartLoc,
1832 SourceLocation LParenLoc,
1833 SourceLocation EndLoc) {
1834 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001835 for (auto &RefExpr : VarList) {
1836 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
1837 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001838 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001839 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001840 continue;
1841 }
1842
Alexey Bataeved09d242014-05-28 05:53:51 +00001843 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001844 // OpenMP [2.1, C/C++]
1845 // A list item is a variable name.
1846 // OpenMP [2.9.3.3, Restrictions, p.1]
1847 // A variable that is part of another variable (as an array or
1848 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001849 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001850 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001851 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001852 continue;
1853 }
1854 Decl *D = DE->getDecl();
1855 VarDecl *VD = cast<VarDecl>(D);
1856
1857 QualType Type = VD->getType();
1858 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1859 // It will be analyzed later.
1860 Vars.push_back(DE);
1861 continue;
1862 }
1863
1864 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1865 // A variable that appears in a private clause must not have an incomplete
1866 // type or a reference type.
1867 if (RequireCompleteType(ELoc, Type,
1868 diag::err_omp_firstprivate_incomplete_type)) {
1869 continue;
1870 }
1871 if (Type->isReferenceType()) {
1872 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001873 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1874 bool IsDecl =
1875 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1876 Diag(VD->getLocation(),
1877 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1878 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001879 continue;
1880 }
1881
1882 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1883 // A variable of class type (or array thereof) that appears in a private
1884 // clause requires an accesible, unambiguous copy constructor for the
1885 // class type.
1886 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001887 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1888 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1889 : nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001890 if (RD) {
1891 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1892 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001893 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1894 if (!CD || CheckConstructorAccess(
1895 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1896 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001897 CD->isDeleted()) {
1898 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001899 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001900 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1901 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001902 Diag(VD->getLocation(),
1903 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1904 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001905 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1906 continue;
1907 }
1908 MarkFunctionReferenced(ELoc, CD);
1909 DiagnoseUseOfDecl(CD, ELoc);
1910
1911 CXXDestructorDecl *DD = RD->getDestructor();
1912 if (DD) {
1913 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1914 DD->isDeleted()) {
1915 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001916 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001917 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1918 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001919 Diag(VD->getLocation(),
1920 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1921 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001922 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1923 continue;
1924 }
1925 MarkFunctionReferenced(ELoc, DD);
1926 DiagnoseUseOfDecl(DD, ELoc);
1927 }
1928 }
1929
1930 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1931 // variable and it was checked already.
1932 if (StartLoc.isValid() && EndLoc.isValid()) {
1933 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1934 Type = Type.getNonReferenceType().getCanonicalType();
1935 bool IsConstant = Type.isConstant(Context);
1936 Type = Context.getBaseElementType(Type);
1937 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1938 // A list item that specifies a given variable may not appear in more
1939 // than one clause on the same directive, except that a variable may be
1940 // specified in both firstprivate and lastprivate clauses.
1941 // TODO: add processing for lastprivate.
1942 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1943 DVar.RefExpr) {
1944 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001945 << getOpenMPClauseName(DVar.CKind)
1946 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001947 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001948 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001949 continue;
1950 }
1951
1952 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1953 // in a Construct]
1954 // Variables with the predetermined data-sharing attributes may not be
1955 // listed in data-sharing attributes clauses, except for the cases
1956 // listed below. For these exceptions only, listing a predetermined
1957 // variable in a data-sharing attribute clause is allowed and overrides
1958 // the variable's predetermined data-sharing attributes.
1959 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1960 // in a Construct, C/C++, p.2]
1961 // Variables with const-qualified type having no mutable member may be
1962 // listed in a firstprivate clause, even if they are static data members.
1963 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1964 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1965 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001966 << getOpenMPClauseName(DVar.CKind)
1967 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001968 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001969 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001970 continue;
1971 }
1972
1973 // OpenMP [2.9.3.4, Restrictions, p.2]
1974 // A list item that is private within a parallel region must not appear
1975 // in a firstprivate clause on a worksharing construct if any of the
1976 // worksharing regions arising from the worksharing construct ever bind
1977 // to any of the parallel regions arising from the parallel construct.
1978 // OpenMP [2.9.3.4, Restrictions, p.3]
1979 // A list item that appears in a reduction clause of a parallel construct
1980 // must not appear in a firstprivate clause on a worksharing or task
1981 // construct if any of the worksharing or task regions arising from the
1982 // worksharing or task construct ever bind to any of the parallel regions
1983 // arising from the parallel construct.
1984 // OpenMP [2.9.3.4, Restrictions, p.4]
1985 // A list item that appears in a reduction clause in worksharing
1986 // construct must not appear in a firstprivate clause in a task construct
1987 // encountered during execution of any of the worksharing regions arising
1988 // from the worksharing construct.
1989 // TODO:
1990 }
1991
1992 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1993 Vars.push_back(DE);
1994 }
1995
Alexey Bataeved09d242014-05-28 05:53:51 +00001996 if (Vars.empty())
1997 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001998
1999 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2000 Vars);
2001}
2002
Alexander Musman1bb328c2014-06-04 13:06:39 +00002003OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2004 SourceLocation StartLoc,
2005 SourceLocation LParenLoc,
2006 SourceLocation EndLoc) {
2007 SmallVector<Expr *, 8> Vars;
2008 for (auto &RefExpr : VarList) {
2009 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2010 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2011 // It will be analyzed later.
2012 Vars.push_back(RefExpr);
2013 continue;
2014 }
2015
2016 SourceLocation ELoc = RefExpr->getExprLoc();
2017 // OpenMP [2.1, C/C++]
2018 // A list item is a variable name.
2019 // OpenMP [2.14.3.5, Restrictions, p.1]
2020 // A variable that is part of another variable (as an array or structure
2021 // element) cannot appear in a lastprivate clause.
2022 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2023 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2024 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2025 continue;
2026 }
2027 Decl *D = DE->getDecl();
2028 VarDecl *VD = cast<VarDecl>(D);
2029
2030 QualType Type = VD->getType();
2031 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2032 // It will be analyzed later.
2033 Vars.push_back(DE);
2034 continue;
2035 }
2036
2037 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2038 // A variable that appears in a lastprivate clause must not have an
2039 // incomplete type or a reference type.
2040 if (RequireCompleteType(ELoc, Type,
2041 diag::err_omp_lastprivate_incomplete_type)) {
2042 continue;
2043 }
2044 if (Type->isReferenceType()) {
2045 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2046 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2047 bool IsDecl =
2048 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2049 Diag(VD->getLocation(),
2050 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2051 << VD;
2052 continue;
2053 }
2054
2055 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2056 // in a Construct]
2057 // Variables with the predetermined data-sharing attributes may not be
2058 // listed in data-sharing attributes clauses, except for the cases
2059 // listed below.
2060 // A list item that is private within a parallel region, or that appears in
2061 // the reduction clause of a parallel construct, must not appear in a
2062 // lastprivate clause on a worksharing construct if any of the
2063 // corresponding worksharing regions ever binds to any of the corresponding
2064 // parallel regions.
2065 // TODO: Check implicit DSA for worksharing directives.
2066 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2067 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2068 DVar.CKind != OMPC_firstprivate &&
2069 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2070 Diag(ELoc, diag::err_omp_wrong_dsa)
2071 << getOpenMPClauseName(DVar.CKind)
2072 << getOpenMPClauseName(OMPC_lastprivate);
2073 if (DVar.RefExpr)
2074 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2075 << getOpenMPClauseName(DVar.CKind);
2076 else
2077 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
2078 << getOpenMPClauseName(DVar.CKind);
2079 continue;
2080 }
2081
2082 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
2083 // A variable of class type (or array thereof) that appears in a lastprivate
2084 // clause requires an accessible, unambiguous default constructor for the
2085 // class type, unless the list item is also specified in a firstprivate
2086 // clause.
2087 // A variable of class type (or array thereof) that appears in a
2088 // lastprivate clause requires an accessible, unambiguous copy assignment
2089 // operator for the class type.
2090 while (Type.getNonReferenceType()->isArrayType())
2091 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2092 ->getElementType();
2093 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2094 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2095 : nullptr;
2096 if (RD) {
2097 // FIXME: If a variable is also specified in a firstprivate clause, we may
2098 // not require default constructor. This can be fixed after adding some
2099 // directive allowing both firstprivate and lastprivate clauses (and this
2100 // should be probably checked after all clauses are processed).
2101 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2102 PartialDiagnostic PD =
2103 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
2104 if (!CD || CheckConstructorAccess(
2105 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
2106 CD->getAccess(), PD) == AR_inaccessible ||
2107 CD->isDeleted()) {
2108 Diag(ELoc, diag::err_omp_required_method)
2109 << getOpenMPClauseName(OMPC_lastprivate) << 0;
2110 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2111 VarDecl::DeclarationOnly;
2112 Diag(VD->getLocation(),
2113 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2114 << VD;
2115 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2116 continue;
2117 }
2118 MarkFunctionReferenced(ELoc, CD);
2119 DiagnoseUseOfDecl(CD, ELoc);
2120
2121 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2122 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
2123 if (!MD || CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2124 MD->isDeleted()) {
2125 Diag(ELoc, diag::err_omp_required_method)
2126 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2127 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2128 VarDecl::DeclarationOnly;
2129 Diag(VD->getLocation(),
2130 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2131 << VD;
2132 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2133 continue;
2134 }
2135 MarkFunctionReferenced(ELoc, MD);
2136 DiagnoseUseOfDecl(MD, ELoc);
2137
2138 CXXDestructorDecl *DD = RD->getDestructor();
2139 if (DD) {
2140 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2141 DD->isDeleted()) {
2142 Diag(ELoc, diag::err_omp_required_method)
2143 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2144 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2145 VarDecl::DeclarationOnly;
2146 Diag(VD->getLocation(),
2147 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2148 << VD;
2149 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2150 continue;
2151 }
2152 MarkFunctionReferenced(ELoc, DD);
2153 DiagnoseUseOfDecl(DD, ELoc);
2154 }
2155 }
2156
2157 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
2158 Vars.push_back(DE);
2159 }
2160
2161 if (Vars.empty())
2162 return nullptr;
2163
2164 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2165 Vars);
2166}
2167
Alexey Bataev758e55e2013-09-06 18:03:48 +00002168OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2169 SourceLocation StartLoc,
2170 SourceLocation LParenLoc,
2171 SourceLocation EndLoc) {
2172 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002173 for (auto &RefExpr : VarList) {
2174 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2175 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002176 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002177 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002178 continue;
2179 }
2180
Alexey Bataeved09d242014-05-28 05:53:51 +00002181 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002182 // OpenMP [2.1, C/C++]
2183 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002184 // OpenMP [2.14.3.2, Restrictions, p.1]
2185 // A variable that is part of another variable (as an array or structure
2186 // element) cannot appear in a shared unless it is a static data member
2187 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002188 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002189 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002190 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002191 continue;
2192 }
2193 Decl *D = DE->getDecl();
2194 VarDecl *VD = cast<VarDecl>(D);
2195
2196 QualType Type = VD->getType();
2197 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2198 // It will be analyzed later.
2199 Vars.push_back(DE);
2200 continue;
2201 }
2202
2203 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2204 // in a Construct]
2205 // Variables with the predetermined data-sharing attributes may not be
2206 // listed in data-sharing attributes clauses, except for the cases
2207 // listed below. For these exceptions only, listing a predetermined
2208 // variable in a data-sharing attribute clause is allowed and overrides
2209 // the variable's predetermined data-sharing attributes.
2210 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002211 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2212 DVar.RefExpr) {
2213 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2214 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002215 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002216 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002217 continue;
2218 }
2219
2220 DSAStack->addDSA(VD, DE, OMPC_shared);
2221 Vars.push_back(DE);
2222 }
2223
Alexey Bataeved09d242014-05-28 05:53:51 +00002224 if (Vars.empty())
2225 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002226
2227 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2228}
2229
Alexey Bataevc5e02582014-06-16 07:08:35 +00002230namespace {
2231class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2232 DSAStackTy *Stack;
2233
2234public:
2235 bool VisitDeclRefExpr(DeclRefExpr *E) {
2236 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2237 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2238 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2239 return false;
2240 if (DVar.CKind != OMPC_unknown)
2241 return true;
2242 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(VD, OMPC_private);
2243 DSAStackTy::DSAVarData DVarFirstprivate =
2244 Stack->hasDSA(VD, OMPC_firstprivate);
2245 DSAStackTy::DSAVarData DVarReduction = Stack->hasDSA(VD, OMPC_reduction);
2246 if (DVarPrivate.CKind != OMPC_unknown ||
2247 DVarFirstprivate.CKind != OMPC_unknown ||
2248 DVarReduction.CKind != OMPC_unknown)
2249 return true;
2250 return false;
2251 }
2252 return false;
2253 }
2254 bool VisitStmt(Stmt *S) {
2255 for (auto Child : S->children()) {
2256 if (Child && Visit(Child))
2257 return true;
2258 }
2259 return false;
2260 }
2261 DSARefChecker(DSAStackTy *S) : Stack(S) {}
2262};
2263}
2264
2265OMPClause *Sema::ActOnOpenMPReductionClause(
2266 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2267 SourceLocation ColonLoc, SourceLocation EndLoc,
2268 CXXScopeSpec &ReductionIdScopeSpec,
2269 const DeclarationNameInfo &ReductionId) {
2270 // TODO: Allow scope specification search when 'declare reduction' is
2271 // supported.
2272 assert(ReductionIdScopeSpec.isEmpty() &&
2273 "No support for scoped reduction identifiers yet.");
2274
2275 auto DN = ReductionId.getName();
2276 auto OOK = DN.getCXXOverloadedOperator();
2277 BinaryOperatorKind BOK = BO_Comma;
2278
2279 // OpenMP [2.14.3.6, reduction clause]
2280 // C
2281 // reduction-identifier is either an identifier or one of the following
2282 // operators: +, -, *, &, |, ^, && and ||
2283 // C++
2284 // reduction-identifier is either an id-expression or one of the following
2285 // operators: +, -, *, &, |, ^, && and ||
2286 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2287 switch (OOK) {
2288 case OO_Plus:
2289 case OO_Minus:
2290 BOK = BO_AddAssign;
2291 break;
2292 case OO_Star:
2293 BOK = BO_MulAssign;
2294 break;
2295 case OO_Amp:
2296 BOK = BO_AndAssign;
2297 break;
2298 case OO_Pipe:
2299 BOK = BO_OrAssign;
2300 break;
2301 case OO_Caret:
2302 BOK = BO_XorAssign;
2303 break;
2304 case OO_AmpAmp:
2305 BOK = BO_LAnd;
2306 break;
2307 case OO_PipePipe:
2308 BOK = BO_LOr;
2309 break;
2310 default:
2311 if (auto II = DN.getAsIdentifierInfo()) {
2312 if (II->isStr("max"))
2313 BOK = BO_GT;
2314 else if (II->isStr("min"))
2315 BOK = BO_LT;
2316 }
2317 break;
2318 }
2319 SourceRange ReductionIdRange;
2320 if (ReductionIdScopeSpec.isValid()) {
2321 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2322 }
2323 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2324 if (BOK == BO_Comma) {
2325 // Not allowed reduction identifier is found.
2326 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2327 << ReductionIdRange;
2328 return nullptr;
2329 }
2330
2331 SmallVector<Expr *, 8> Vars;
2332 for (auto RefExpr : VarList) {
2333 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2334 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2335 // It will be analyzed later.
2336 Vars.push_back(RefExpr);
2337 continue;
2338 }
2339
2340 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2341 RefExpr->isInstantiationDependent() ||
2342 RefExpr->containsUnexpandedParameterPack()) {
2343 // It will be analyzed later.
2344 Vars.push_back(RefExpr);
2345 continue;
2346 }
2347
2348 auto ELoc = RefExpr->getExprLoc();
2349 auto ERange = RefExpr->getSourceRange();
2350 // OpenMP [2.1, C/C++]
2351 // A list item is a variable or array section, subject to the restrictions
2352 // specified in Section 2.4 on page 42 and in each of the sections
2353 // describing clauses and directives for which a list appears.
2354 // OpenMP [2.14.3.3, Restrictions, p.1]
2355 // A variable that is part of another variable (as an array or
2356 // structure element) cannot appear in a private clause.
2357 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2358 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2359 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2360 continue;
2361 }
2362 auto D = DE->getDecl();
2363 auto VD = cast<VarDecl>(D);
2364 auto Type = VD->getType();
2365 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2366 // A variable that appears in a private clause must not have an incomplete
2367 // type or a reference type.
2368 if (RequireCompleteType(ELoc, Type,
2369 diag::err_omp_reduction_incomplete_type))
2370 continue;
2371 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2372 // Arrays may not appear in a reduction clause.
2373 if (Type.getNonReferenceType()->isArrayType()) {
2374 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2375 bool IsDecl =
2376 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2377 Diag(VD->getLocation(),
2378 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2379 << VD;
2380 continue;
2381 }
2382 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2383 // A list item that appears in a reduction clause must not be
2384 // const-qualified.
2385 if (Type.getNonReferenceType().isConstant(Context)) {
2386 Diag(ELoc, diag::err_omp_const_variable)
2387 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2388 bool IsDecl =
2389 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2390 Diag(VD->getLocation(),
2391 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2392 << VD;
2393 continue;
2394 }
2395 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2396 // If a list-item is a reference type then it must bind to the same object
2397 // for all threads of the team.
2398 VarDecl *VDDef = VD->getDefinition();
2399 if (Type->isReferenceType() && VDDef) {
2400 DSARefChecker Check(DSAStack);
2401 if (Check.Visit(VDDef->getInit())) {
2402 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2403 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2404 continue;
2405 }
2406 }
2407 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2408 // The type of a list item that appears in a reduction clause must be valid
2409 // for the reduction-identifier. For a max or min reduction in C, the type
2410 // of the list item must be an allowed arithmetic data type: char, int,
2411 // float, double, or _Bool, possibly modified with long, short, signed, or
2412 // unsigned. For a max or min reduction in C++, the type of the list item
2413 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2414 // double, or bool, possibly modified with long, short, signed, or unsigned.
2415 if ((BOK == BO_GT || BOK == BO_LT) &&
2416 !(Type->isScalarType() ||
2417 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2418 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2419 << getLangOpts().CPlusPlus;
2420 bool IsDecl =
2421 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2422 Diag(VD->getLocation(),
2423 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2424 << VD;
2425 continue;
2426 }
2427 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2428 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2429 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2430 bool IsDecl =
2431 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2432 Diag(VD->getLocation(),
2433 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2434 << VD;
2435 continue;
2436 }
2437 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2438 getDiagnostics().setSuppressAllDiagnostics(true);
2439 ExprResult ReductionOp =
2440 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2441 RefExpr, RefExpr);
2442 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2443 if (ReductionOp.isInvalid()) {
2444 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
2445 << ReductionIdRange;
2446 bool IsDecl =
2447 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2448 Diag(VD->getLocation(),
2449 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2450 << VD;
2451 continue;
2452 }
2453
2454 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2455 // in a Construct]
2456 // Variables with the predetermined data-sharing attributes may not be
2457 // listed in data-sharing attributes clauses, except for the cases
2458 // listed below. For these exceptions only, listing a predetermined
2459 // variable in a data-sharing attribute clause is allowed and overrides
2460 // the variable's predetermined data-sharing attributes.
2461 // OpenMP [2.14.3.6, Restrictions, p.3]
2462 // Any number of reduction clauses can be specified on the directive,
2463 // but a list item can appear only once in the reduction clauses for that
2464 // directive.
2465 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2466 if (DVar.CKind == OMPC_reduction) {
2467 Diag(ELoc, diag::err_omp_once_referenced)
2468 << getOpenMPClauseName(OMPC_reduction);
2469 if (DVar.RefExpr) {
2470 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2471 }
2472 } else if (DVar.CKind != OMPC_unknown) {
2473 Diag(ELoc, diag::err_omp_wrong_dsa)
2474 << getOpenMPClauseName(DVar.CKind)
2475 << getOpenMPClauseName(OMPC_reduction);
2476 if (DVar.RefExpr) {
2477 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2478 << getOpenMPClauseName(DVar.CKind);
2479 } else {
2480 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
2481 << getOpenMPClauseName(DVar.CKind);
2482 }
2483 continue;
2484 }
2485
2486 // OpenMP [2.14.3.6, Restrictions, p.1]
2487 // A list item that appears in a reduction clause of a worksharing
2488 // construct must be shared in the parallel regions to which any of the
2489 // worksharing regions arising from the worksharing construct bind.
2490 // TODO Implement it later.
2491
2492 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2493 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2494 : nullptr;
2495 if (RD) {
2496 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2497 PartialDiagnostic PD =
2498 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
2499 if (!CD || CheckConstructorAccess(
2500 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
2501 CD->getAccess(), PD) == AR_inaccessible ||
2502 CD->isDeleted()) {
2503 Diag(ELoc, diag::err_omp_required_method)
2504 << getOpenMPClauseName(OMPC_reduction) << 0;
2505 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2506 VarDecl::DeclarationOnly;
2507 Diag(VD->getLocation(),
2508 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2509 << VD;
2510 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2511 continue;
2512 }
2513 MarkFunctionReferenced(ELoc, CD);
2514 DiagnoseUseOfDecl(CD, ELoc);
2515
2516 CXXDestructorDecl *DD = RD->getDestructor();
2517 if (DD) {
2518 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2519 DD->isDeleted()) {
2520 Diag(ELoc, diag::err_omp_required_method)
2521 << getOpenMPClauseName(OMPC_reduction) << 4;
2522 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2523 VarDecl::DeclarationOnly;
2524 Diag(VD->getLocation(),
2525 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2526 << VD;
2527 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2528 continue;
2529 }
2530 MarkFunctionReferenced(ELoc, DD);
2531 DiagnoseUseOfDecl(DD, ELoc);
2532 }
2533 }
2534
2535 DSAStack->addDSA(VD, DE, OMPC_reduction);
2536 Vars.push_back(DE);
2537 }
2538
2539 if (Vars.empty())
2540 return nullptr;
2541
2542 return OMPReductionClause::Create(
2543 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
2544 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
2545}
2546
Alexander Musman8dba6642014-04-22 13:09:42 +00002547OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2548 SourceLocation StartLoc,
2549 SourceLocation LParenLoc,
2550 SourceLocation ColonLoc,
2551 SourceLocation EndLoc) {
2552 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002553 for (auto &RefExpr : VarList) {
2554 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2555 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002556 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002557 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002558 continue;
2559 }
2560
2561 // OpenMP [2.14.3.7, linear clause]
2562 // A list item that appears in a linear clause is subject to the private
2563 // clause semantics described in Section 2.14.3.3 on page 159 except as
2564 // noted. In addition, the value of the new list item on each iteration
2565 // of the associated loop(s) corresponds to the value of the original
2566 // list item before entering the construct plus the logical number of
2567 // the iteration times linear-step.
2568
Alexey Bataeved09d242014-05-28 05:53:51 +00002569 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00002570 // OpenMP [2.1, C/C++]
2571 // A list item is a variable name.
2572 // OpenMP [2.14.3.3, Restrictions, p.1]
2573 // A variable that is part of another variable (as an array or
2574 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002575 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002576 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002577 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00002578 continue;
2579 }
2580
2581 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2582
2583 // OpenMP [2.14.3.7, linear clause]
2584 // A list-item cannot appear in more than one linear clause.
2585 // A list-item that appears in a linear clause cannot appear in any
2586 // other data-sharing attribute clause.
2587 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2588 if (DVar.RefExpr) {
2589 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2590 << getOpenMPClauseName(OMPC_linear);
2591 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2592 << getOpenMPClauseName(DVar.CKind);
2593 continue;
2594 }
2595
2596 QualType QType = VD->getType();
2597 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2598 // It will be analyzed later.
2599 Vars.push_back(DE);
2600 continue;
2601 }
2602
2603 // A variable must not have an incomplete type or a reference type.
2604 if (RequireCompleteType(ELoc, QType,
2605 diag::err_omp_linear_incomplete_type)) {
2606 continue;
2607 }
2608 if (QType->isReferenceType()) {
2609 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2610 << getOpenMPClauseName(OMPC_linear) << QType;
2611 bool IsDecl =
2612 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2613 Diag(VD->getLocation(),
2614 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2615 << VD;
2616 continue;
2617 }
2618
2619 // A list item must not be const-qualified.
2620 if (QType.isConstant(Context)) {
2621 Diag(ELoc, diag::err_omp_const_variable)
2622 << getOpenMPClauseName(OMPC_linear);
2623 bool IsDecl =
2624 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2625 Diag(VD->getLocation(),
2626 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2627 << VD;
2628 continue;
2629 }
2630
2631 // A list item must be of integral or pointer type.
2632 QType = QType.getUnqualifiedType().getCanonicalType();
2633 const Type *Ty = QType.getTypePtrOrNull();
2634 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
2635 !Ty->isPointerType())) {
2636 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
2637 bool IsDecl =
2638 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2639 Diag(VD->getLocation(),
2640 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2641 << VD;
2642 continue;
2643 }
2644
2645 DSAStack->addDSA(VD, DE, OMPC_linear);
2646 Vars.push_back(DE);
2647 }
2648
2649 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002650 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00002651
2652 Expr *StepExpr = Step;
2653 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2654 !Step->isInstantiationDependent() &&
2655 !Step->containsUnexpandedParameterPack()) {
2656 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002657 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00002658 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002659 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002660 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00002661
2662 // Warn about zero linear step (it would be probably better specified as
2663 // making corresponding variables 'const').
2664 llvm::APSInt Result;
2665 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
2666 !Result.isNegative() && !Result.isStrictlyPositive())
2667 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
2668 << (Vars.size() > 1);
2669 }
2670
2671 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
2672 Vars, StepExpr);
2673}
2674
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002675OMPClause *Sema::ActOnOpenMPAlignedClause(
2676 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
2677 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
2678
2679 SmallVector<Expr *, 8> Vars;
2680 for (auto &RefExpr : VarList) {
2681 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
2682 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2683 // It will be analyzed later.
2684 Vars.push_back(RefExpr);
2685 continue;
2686 }
2687
2688 SourceLocation ELoc = RefExpr->getExprLoc();
2689 // OpenMP [2.1, C/C++]
2690 // A list item is a variable name.
2691 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
2692 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2693 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2694 continue;
2695 }
2696
2697 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2698
2699 // OpenMP [2.8.1, simd construct, Restrictions]
2700 // The type of list items appearing in the aligned clause must be
2701 // array, pointer, reference to array, or reference to pointer.
2702 QualType QType = DE->getType()
2703 .getNonReferenceType()
2704 .getUnqualifiedType()
2705 .getCanonicalType();
2706 const Type *Ty = QType.getTypePtrOrNull();
2707 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
2708 !Ty->isPointerType())) {
2709 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
2710 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
2711 bool IsDecl =
2712 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2713 Diag(VD->getLocation(),
2714 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2715 << VD;
2716 continue;
2717 }
2718
2719 // OpenMP [2.8.1, simd construct, Restrictions]
2720 // A list-item cannot appear in more than one aligned clause.
2721 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
2722 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
2723 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
2724 << getOpenMPClauseName(OMPC_aligned);
2725 continue;
2726 }
2727
2728 Vars.push_back(DE);
2729 }
2730
2731 // OpenMP [2.8.1, simd construct, Description]
2732 // The parameter of the aligned clause, alignment, must be a constant
2733 // positive integer expression.
2734 // If no optional parameter is specified, implementation-defined default
2735 // alignments for SIMD instructions on the target platforms are assumed.
2736 if (Alignment != nullptr) {
2737 ExprResult AlignResult =
2738 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
2739 if (AlignResult.isInvalid())
2740 return nullptr;
2741 Alignment = AlignResult.get();
2742 }
2743 if (Vars.empty())
2744 return nullptr;
2745
2746 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
2747 EndLoc, Vars, Alignment);
2748}
2749
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002750OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
2751 SourceLocation StartLoc,
2752 SourceLocation LParenLoc,
2753 SourceLocation EndLoc) {
2754 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002755 for (auto &RefExpr : VarList) {
2756 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
2757 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002758 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002759 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002760 continue;
2761 }
2762
Alexey Bataeved09d242014-05-28 05:53:51 +00002763 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002764 // OpenMP [2.1, C/C++]
2765 // A list item is a variable name.
2766 // OpenMP [2.14.4.1, Restrictions, p.1]
2767 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00002768 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002769 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002770 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002771 continue;
2772 }
2773
2774 Decl *D = DE->getDecl();
2775 VarDecl *VD = cast<VarDecl>(D);
2776
2777 QualType Type = VD->getType();
2778 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2779 // It will be analyzed later.
2780 Vars.push_back(DE);
2781 continue;
2782 }
2783
2784 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
2785 // A list item that appears in a copyin clause must be threadprivate.
2786 if (!DSAStack->isThreadPrivate(VD)) {
2787 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00002788 << getOpenMPClauseName(OMPC_copyin)
2789 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002790 continue;
2791 }
2792
2793 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
2794 // A variable of class type (or array thereof) that appears in a
2795 // copyin clause requires an accesible, unambiguous copy assignment
2796 // operator for the class type.
2797 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002798 CXXRecordDecl *RD =
2799 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002800 if (RD) {
2801 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2802 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataeved09d242014-05-28 05:53:51 +00002803 if (!MD || CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002804 MD->isDeleted()) {
2805 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002806 << getOpenMPClauseName(OMPC_copyin) << 2;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002807 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2808 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002809 Diag(VD->getLocation(),
2810 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2811 << VD;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002812 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2813 continue;
2814 }
2815 MarkFunctionReferenced(ELoc, MD);
2816 DiagnoseUseOfDecl(MD, ELoc);
2817 }
2818
2819 DSAStack->addDSA(VD, DE, OMPC_copyin);
2820 Vars.push_back(DE);
2821 }
2822
Alexey Bataeved09d242014-05-28 05:53:51 +00002823 if (Vars.empty())
2824 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002825
2826 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2827}
2828
Alexey Bataev758e55e2013-09-06 18:03:48 +00002829#undef DSAStack