blob: f4f764c1fba89df29fcbb8029e4cdacefdae7a6f [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;
60
61 struct SharingMapTy {
62 DeclSAMapTy SharingMap;
63 DefaultDataSharingAttributes DefaultAttr;
64 OpenMPDirectiveKind Directive;
65 DeclarationNameInfo DirectiveName;
66 Scope *CurScope;
Alexey Bataeved09d242014-05-28 05:53:51 +000067 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataev758e55e2013-09-06 18:03:48 +000068 Scope *CurScope)
Alexander Musmancb7f9c42014-05-15 13:04:49 +000069 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
Alexey Bataeved09d242014-05-28 05:53:51 +000070 DirectiveName(std::move(Name)), CurScope(CurScope) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000071 SharingMapTy()
Alexander Musmancb7f9c42014-05-15 13:04:49 +000072 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(OMPD_unknown),
73 DirectiveName(), CurScope(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000074 };
75
76 typedef SmallVector<SharingMapTy, 64> StackTy;
77
78 /// \brief Stack of used declaration and their data-sharing attributes.
79 StackTy Stack;
80 Sema &Actions;
81
82 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
83
84 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +000085
86 /// \brief Checks if the variable is a local for OpenMP region.
87 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +000088
Alexey Bataev758e55e2013-09-06 18:03:48 +000089public:
Alexey Bataeved09d242014-05-28 05:53:51 +000090 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000091
92 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
93 Scope *CurScope) {
94 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
95 }
96
97 void pop() {
98 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
99 Stack.pop_back();
100 }
101
102 /// \brief Adds explicit data sharing attribute to the specified declaration.
103 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
104
Alexey Bataev758e55e2013-09-06 18:03:48 +0000105 /// \brief Returns data sharing attributes from top of the stack for the
106 /// specified declaration.
107 DSAVarData getTopDSA(VarDecl *D);
108 /// \brief Returns data-sharing attributes for the specified declaration.
109 DSAVarData getImplicitDSA(VarDecl *D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000110 /// \brief Checks if the specified variables has \a CKind data-sharing
111 /// attribute in \a DKind directive.
112 DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
113 OpenMPDirectiveKind DKind = OMPD_unknown);
114
Alexey Bataev758e55e2013-09-06 18:03:48 +0000115 /// \brief Returns currently analyzed directive.
116 OpenMPDirectiveKind getCurrentDirective() const {
117 return Stack.back().Directive;
118 }
119
120 /// \brief Set default data sharing attribute to none.
121 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
122 /// \brief Set default data sharing attribute to shared.
123 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
124
125 DefaultDataSharingAttributes getDefaultDSA() const {
126 return Stack.back().DefaultAttr;
127 }
128
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000129 /// \brief Checks if the spewcified variable is threadprivate.
130 bool isThreadPrivate(VarDecl *D) {
131 DSAVarData DVar = getTopDSA(D);
132 return (DVar.CKind == OMPC_threadprivate || DVar.CKind == OMPC_copyin);
133 }
134
135 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136 Scope *getCurScope() { return Stack.back().CurScope; }
137};
Alexey Bataeved09d242014-05-28 05:53:51 +0000138} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000139
140DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
141 VarDecl *D) {
142 DSAVarData DVar;
143 if (Iter == Stack.rend() - 1) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000144 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
145 // in a region but not in construct]
146 // File-scope or namespace-scope variables referenced in called routines
147 // in the region are shared unless they appear in a threadprivate
148 // directive.
149 // TODO
150 if (!D->isFunctionOrMethodVarDecl())
151 DVar.CKind = OMPC_shared;
152
153 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
154 // in a region but not in construct]
155 // Variables with static storage duration that are declared in called
156 // routines in the region are shared.
157 if (D->hasGlobalStorage())
158 DVar.CKind = OMPC_shared;
159
Alexey Bataev758e55e2013-09-06 18:03:48 +0000160 return DVar;
161 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000162
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000164 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
165 // in a Construct, C/C++, predetermined, p.1]
166 // Variables with automatic storage duration that are declared in a scope
167 // inside the construct are private.
168 if (DVar.DKind != OMPD_parallel) {
169 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000170 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000171 DVar.CKind = OMPC_private;
172 return DVar;
173 }
174 }
175
Alexey Bataev758e55e2013-09-06 18:03:48 +0000176 // Explicitly specified attributes and local variables with predetermined
177 // attributes.
178 if (Iter->SharingMap.count(D)) {
179 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
180 DVar.CKind = Iter->SharingMap[D].Attributes;
181 return DVar;
182 }
183
184 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
185 // in a Construct, C/C++, implicitly determined, p.1]
186 // In a parallel or task construct, the data-sharing attributes of these
187 // variables are determined by the default clause, if present.
188 switch (Iter->DefaultAttr) {
189 case DSA_shared:
190 DVar.CKind = OMPC_shared;
191 return DVar;
192 case DSA_none:
193 return DVar;
194 case DSA_unspecified:
195 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
196 // in a Construct, implicitly determined, p.2]
197 // In a parallel construct, if no default clause is present, these
198 // variables are shared.
199 if (DVar.DKind == OMPD_parallel) {
200 DVar.CKind = OMPC_shared;
201 return DVar;
202 }
203
204 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
205 // in a Construct, implicitly determined, p.4]
206 // In a task construct, if no default clause is present, a variable that in
207 // the enclosing context is determined to be shared by all implicit tasks
208 // bound to the current team is shared.
209 // TODO
210 if (DVar.DKind == OMPD_task) {
211 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000212 for (StackTy::reverse_iterator I = std::next(Iter),
213 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000214 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
216 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000217 // in a Construct, implicitly determined, p.6]
218 // In a task construct, if no default clause is present, a variable
219 // whose data-sharing attribute is not determined by the rules above is
220 // firstprivate.
221 DVarTemp = getDSA(I, D);
222 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000223 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000224 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000225 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000226 return DVar;
227 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000228 if (I->Directive == OMPD_parallel)
229 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000230 }
231 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000232 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000233 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000234 return DVar;
235 }
236 }
237 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
238 // in a Construct, implicitly determined, p.3]
239 // For constructs other than task, if no default clause is present, these
240 // variables inherit their data-sharing attributes from the enclosing
241 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000242 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000243}
244
245void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
246 if (A == OMPC_threadprivate) {
247 Stack[0].SharingMap[D].Attributes = A;
248 Stack[0].SharingMap[D].RefExpr = E;
249 } else {
250 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
251 Stack.back().SharingMap[D].Attributes = A;
252 Stack.back().SharingMap[D].RefExpr = E;
253 }
254}
255
Alexey Bataeved09d242014-05-28 05:53:51 +0000256bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000257 if (Stack.size() > 2) {
258 reverse_iterator I = Iter, E = Stack.rend() - 1;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000259 Scope *TopScope = nullptr;
Fraser Cormack111023c2014-04-15 08:59:09 +0000260 while (I != E && I->Directive != OMPD_parallel) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000261 ++I;
262 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000263 if (I == E)
264 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000265 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000266 Scope *CurScope = getCurScope();
267 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000268 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000269 }
270 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000271 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000272 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000273}
274
275DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
276 DSAVarData DVar;
277
278 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
279 // in a Construct, C/C++, predetermined, p.1]
280 // Variables appearing in threadprivate directives are threadprivate.
281 if (D->getTLSKind() != VarDecl::TLS_None) {
282 DVar.CKind = OMPC_threadprivate;
283 return DVar;
284 }
285 if (Stack[0].SharingMap.count(D)) {
286 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
287 DVar.CKind = OMPC_threadprivate;
288 return DVar;
289 }
290
291 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
292 // in a Construct, C/C++, predetermined, p.1]
293 // Variables with automatic storage duration that are declared in a scope
294 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000295 OpenMPDirectiveKind Kind = getCurrentDirective();
296 if (Kind != OMPD_parallel) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000297 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000298 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000299 DVar.CKind = OMPC_private;
300 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000301 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000302 }
303
304 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
305 // in a Construct, C/C++, predetermined, p.4]
306 // Static data memebers are shared.
307 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000308 // Variables with const-qualified type having no mutable member may be
309 // listed
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000310 // in a firstprivate clause, even if they are static data members.
311 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
312 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
313 return DVar;
314
Alexey Bataev758e55e2013-09-06 18:03:48 +0000315 DVar.CKind = OMPC_shared;
316 return DVar;
317 }
318
319 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
320 bool IsConstant = Type.isConstant(Actions.getASTContext());
321 while (Type->isArrayType()) {
322 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
323 Type = ElemType.getNonReferenceType().getCanonicalType();
324 }
325 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
326 // in a Construct, C/C++, predetermined, p.6]
327 // Variables with const qualified type having no mutable member are
328 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000329 CXXRecordDecl *RD =
330 Actions.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000331 if (IsConstant &&
332 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
333 // Variables with const-qualified type having no mutable member may be
334 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000335 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 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
344 // in a Construct, C/C++, predetermined, p.7]
345 // Variables with static storage duration that are declared in a scope
346 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000347 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000348 DVar.CKind = OMPC_shared;
349 return DVar;
350 }
351
352 // Explicitly specified attributes and local variables with predetermined
353 // attributes.
354 if (Stack.back().SharingMap.count(D)) {
355 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
356 DVar.CKind = Stack.back().SharingMap[D].Attributes;
357 }
358
359 return DVar;
360}
361
362DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000363 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000364}
365
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000366DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
367 OpenMPDirectiveKind DKind) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000368 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
369 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000370 I != E; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000371 if (DKind != OMPD_unknown && DKind != I->Directive)
372 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000373 DSAVarData DVar = getDSA(I, D);
374 if (DVar.CKind == CKind)
375 return DVar;
376 }
377 return DSAVarData();
378}
379
Alexey Bataev758e55e2013-09-06 18:03:48 +0000380void Sema::InitDataSharingAttributesStack() {
381 VarDataSharingAttributesStack = new DSAStackTy(*this);
382}
383
384#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
385
Alexey Bataeved09d242014-05-28 05:53:51 +0000386void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000387
388void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
389 const DeclarationNameInfo &DirName,
390 Scope *CurScope) {
391 DSAStack->push(DKind, DirName, CurScope);
392 PushExpressionEvaluationContext(PotentiallyEvaluated);
393}
394
395void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
396 DSAStack->pop();
397 DiscardCleanupsInEvaluationContext();
398 PopExpressionEvaluationContext();
399}
400
Alexey Bataeva769e072013-03-22 06:34:35 +0000401namespace {
402
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000403class VarDeclFilterCCC : public CorrectionCandidateCallback {
404private:
405 Sema &Actions;
Alexey Bataeved09d242014-05-28 05:53:51 +0000406
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000407public:
Alexey Bataeved09d242014-05-28 05:53:51 +0000408 VarDeclFilterCCC(Sema &S) : Actions(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000409 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000410 NamedDecl *ND = Candidate.getCorrectionDecl();
411 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
412 return VD->hasGlobalStorage() &&
413 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
414 Actions.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000415 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000416 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000417 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000418};
Alexey Bataeved09d242014-05-28 05:53:51 +0000419} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000420
421ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
422 CXXScopeSpec &ScopeSpec,
423 const DeclarationNameInfo &Id) {
424 LookupResult Lookup(*this, Id, LookupOrdinaryName);
425 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
426
427 if (Lookup.isAmbiguous())
428 return ExprError();
429
430 VarDecl *VD;
431 if (!Lookup.isSingleResult()) {
432 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000433 if (TypoCorrection Corrected =
434 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
435 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000436 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000437 PDiag(Lookup.empty()
438 ? diag::err_undeclared_var_use_suggest
439 : diag::err_omp_expected_var_arg_suggest)
440 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000441 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000442 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000443 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
444 : diag::err_omp_expected_var_arg)
445 << Id.getName();
446 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000447 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000448 } else {
449 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000450 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000451 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
452 return ExprError();
453 }
454 }
455 Lookup.suppressDiagnostics();
456
457 // OpenMP [2.9.2, Syntax, C/C++]
458 // Variables must be file-scope, namespace-scope, or static block-scope.
459 if (!VD->hasGlobalStorage()) {
460 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000461 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
462 bool IsDecl =
463 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000464 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000465 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
466 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000467 return ExprError();
468 }
469
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000470 VarDecl *CanonicalVD = VD->getCanonicalDecl();
471 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000472 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
473 // A threadprivate directive for file-scope variables must appear outside
474 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000475 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
476 !getCurLexicalContext()->isTranslationUnit()) {
477 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000478 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
479 bool IsDecl =
480 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
481 Diag(VD->getLocation(),
482 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
483 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000484 return ExprError();
485 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000486 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
487 // A threadprivate directive for static class member variables must appear
488 // in the class definition, in the same scope in which the member
489 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000490 if (CanonicalVD->isStaticDataMember() &&
491 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
492 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000493 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
494 bool IsDecl =
495 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
496 Diag(VD->getLocation(),
497 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
498 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000499 return ExprError();
500 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000501 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
502 // A threadprivate directive for namespace-scope variables must appear
503 // outside any definition or declaration other than the namespace
504 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000505 if (CanonicalVD->getDeclContext()->isNamespace() &&
506 (!getCurLexicalContext()->isFileContext() ||
507 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
508 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000509 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
510 bool IsDecl =
511 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
512 Diag(VD->getLocation(),
513 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
514 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000515 return ExprError();
516 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000517 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
518 // A threadprivate directive for static block-scope variables must appear
519 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000520 if (CanonicalVD->isStaticLocal() && CurScope &&
521 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000522 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000523 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
524 bool IsDecl =
525 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
526 Diag(VD->getLocation(),
527 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
528 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000529 return ExprError();
530 }
531
532 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
533 // A threadprivate directive must lexically precede all references to any
534 // of the variables in its list.
535 if (VD->isUsed()) {
536 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000537 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000538 return ExprError();
539 }
540
541 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000542 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000543 return DE;
544}
545
Alexey Bataeved09d242014-05-28 05:53:51 +0000546Sema::DeclGroupPtrTy
547Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
548 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000549 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000550 CurContext->addDecl(D);
551 return DeclGroupPtrTy::make(DeclGroupRef(D));
552 }
553 return DeclGroupPtrTy();
554}
555
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000556namespace {
557class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
558 Sema &SemaRef;
559
560public:
561 bool VisitDeclRefExpr(const DeclRefExpr *E) {
562 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
563 if (VD->hasLocalStorage()) {
564 SemaRef.Diag(E->getLocStart(),
565 diag::err_omp_local_var_in_threadprivate_init)
566 << E->getSourceRange();
567 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
568 << VD << VD->getSourceRange();
569 return true;
570 }
571 }
572 return false;
573 }
574 bool VisitStmt(const Stmt *S) {
575 for (auto Child : S->children()) {
576 if (Child && Visit(Child))
577 return true;
578 }
579 return false;
580 }
581 LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
582};
583} // namespace
584
Alexey Bataeved09d242014-05-28 05:53:51 +0000585OMPThreadPrivateDecl *
586Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000587 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000588 for (auto &RefExpr : VarList) {
589 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000590 VarDecl *VD = cast<VarDecl>(DE->getDecl());
591 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000592
593 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
594 // A threadprivate variable must not have an incomplete type.
595 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000596 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000597 continue;
598 }
599
600 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
601 // A threadprivate variable must not have a reference type.
602 if (VD->getType()->isReferenceType()) {
603 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000604 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
605 bool IsDecl =
606 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
607 Diag(VD->getLocation(),
608 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
609 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000610 continue;
611 }
612
Richard Smithfd3834f2013-04-13 02:43:54 +0000613 // Check if this is a TLS variable.
614 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000615 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000616 bool IsDecl =
617 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
618 Diag(VD->getLocation(),
619 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
620 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000621 continue;
622 }
623
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000624 // Check if initial value of threadprivate variable reference variable with
625 // local storage (it is not supported by runtime).
626 if (auto Init = VD->getAnyInitializer()) {
627 LocalVarRefChecker Checker(*this);
628 if (Checker.Visit(Init)) continue;
629 }
630
Alexey Bataeved09d242014-05-28 05:53:51 +0000631 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000632 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000633 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000634 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000635 if (!Vars.empty()) {
636 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
637 Vars);
638 D->setAccess(AS_public);
639 }
640 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000641}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000642
Alexey Bataev758e55e2013-09-06 18:03:48 +0000643namespace {
644class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
645 DSAStackTy *Stack;
646 Sema &Actions;
647 bool ErrorFound;
648 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000649 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataeved09d242014-05-28 05:53:51 +0000650
Alexey Bataev758e55e2013-09-06 18:03:48 +0000651public:
652 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000653 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000654 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000655 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
656 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000657
658 SourceLocation ELoc = E->getExprLoc();
659
660 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
661 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
662 if (DVar.CKind != OMPC_unknown) {
663 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000664 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000665 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000666 return;
667 }
668 // The default(none) clause requires that each variable that is referenced
669 // in the construct, and does not have a predetermined data-sharing
670 // attribute, must have its data-sharing attribute explicitly determined
671 // by being listed in a data-sharing attribute clause.
672 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
673 (DKind == OMPD_parallel || DKind == OMPD_task)) {
674 ErrorFound = true;
675 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
676 return;
677 }
678
679 // OpenMP [2.9.3.6, Restrictions, p.2]
680 // A list item that appears in a reduction clause of the innermost
681 // enclosing worksharing or parallel construct may not be accessed in an
682 // explicit task.
683 // TODO:
684
685 // Define implicit data-sharing attributes for task.
686 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000687 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
688 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000689 }
690 }
691 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000692 for (auto C : S->clauses())
693 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000694 for (StmtRange R = C->children(); R; ++R)
695 if (Stmt *Child = *R)
696 Visit(Child);
697 }
698 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000699 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
700 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000701 if (Stmt *Child = *I)
702 if (!isa<OMPExecutableDirective>(Child))
703 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000704 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705
706 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000707 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000708
709 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
Alexey Bataeved09d242014-05-28 05:53:51 +0000710 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000711};
Alexey Bataeved09d242014-05-28 05:53:51 +0000712} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713
Alexey Bataev9959db52014-05-06 10:08:46 +0000714void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
715 Scope *CurScope) {
716 switch (DKind) {
717 case OMPD_parallel: {
718 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
719 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
720 Sema::CapturedParamNameType Params[3] = {
721 std::make_pair(".global_tid.", KmpInt32PtrTy),
722 std::make_pair(".bound_tid.", KmpInt32PtrTy),
723 std::make_pair(StringRef(), QualType()) // __context with shared vars
724 };
725 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
726 break;
727 }
728 case OMPD_simd: {
729 Sema::CapturedParamNameType Params[1] = {
730 std::make_pair(StringRef(), QualType()) // __context with shared vars
731 };
732 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
733 break;
734 }
735 case OMPD_threadprivate:
736 case OMPD_task:
737 llvm_unreachable("OpenMP Directive is not allowed");
738 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000739 llvm_unreachable("Unknown OpenMP directive");
740 }
741}
742
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000743StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
744 ArrayRef<OMPClause *> Clauses,
745 Stmt *AStmt,
746 SourceLocation StartLoc,
747 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000748 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
749
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000750 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000751
752 // Check default data sharing attributes for referenced variables.
753 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
754 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
755 if (DSAChecker.isErrorFound())
756 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000757 // Generate list of implicitly defined firstprivate variables.
758 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
759 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
760
761 bool ErrorFound = false;
762 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000763 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
764 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
765 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000766 ClausesWithImplicit.push_back(Implicit);
767 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +0000768 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000769 } else
770 ErrorFound = true;
771 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000772
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000773 switch (Kind) {
774 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +0000775 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
776 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000777 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000778 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +0000779 Res =
780 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000781 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000782 case OMPD_threadprivate:
783 case OMPD_task:
784 llvm_unreachable("OpenMP Directive is not allowed");
785 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000786 llvm_unreachable("Unknown OpenMP directive");
787 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000788
Alexey Bataeved09d242014-05-28 05:53:51 +0000789 if (ErrorFound)
790 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000791 return Res;
792}
793
794StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
795 Stmt *AStmt,
796 SourceLocation StartLoc,
797 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000798 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
799 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
800 // 1.2.2 OpenMP Language Terminology
801 // Structured block - An executable statement with a single entry at the
802 // top and a single exit at the bottom.
803 // The point of exit cannot be a branch out of the structured block.
804 // longjmp() and throw() must not violate the entry/exit criteria.
805 CS->getCapturedDecl()->setNothrow();
806
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000807 getCurFunction()->setHasBranchProtectedScope();
808
Alexey Bataeved09d242014-05-28 05:53:51 +0000809 return Owned(
810 OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000811}
812
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000813StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +0000814 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000815 SourceLocation EndLoc) {
816 Stmt *CStmt = AStmt;
817 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt))
818 CStmt = CS->getCapturedStmt();
819 while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt))
820 CStmt = AS->getSubStmt();
821 ForStmt *For = dyn_cast<ForStmt>(CStmt);
822 if (!For) {
823 Diag(CStmt->getLocStart(), diag::err_omp_not_for)
Alexey Bataeved09d242014-05-28 05:53:51 +0000824 << getOpenMPDirectiveName(OMPD_simd);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000825 return StmtError();
826 }
827
828 // FIXME: Checking loop canonical form, collapsing etc.
829
830 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataeved09d242014-05-28 05:53:51 +0000831 return Owned(
832 OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt));
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000833}
834
Alexey Bataeved09d242014-05-28 05:53:51 +0000835OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000836 SourceLocation StartLoc,
837 SourceLocation LParenLoc,
838 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000839 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000840 switch (Kind) {
841 case OMPC_if:
842 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
843 break;
Alexey Bataev568a8332014-03-06 06:15:19 +0000844 case OMPC_num_threads:
845 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
846 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000847 case OMPC_safelen:
848 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
849 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +0000850 case OMPC_collapse:
851 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
852 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000853 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000854 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000855 case OMPC_private:
856 case OMPC_firstprivate:
857 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000858 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000859 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000860 case OMPC_threadprivate:
861 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000862 llvm_unreachable("Clause is not allowed.");
863 }
864 return Res;
865}
866
Alexey Bataeved09d242014-05-28 05:53:51 +0000867OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000868 SourceLocation LParenLoc,
869 SourceLocation EndLoc) {
870 Expr *ValExpr = Condition;
871 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
872 !Condition->isInstantiationDependent() &&
873 !Condition->containsUnexpandedParameterPack()) {
874 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000875 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000876 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000877 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000878
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000879 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000880 }
881
882 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
883}
884
Alexey Bataev568a8332014-03-06 06:15:19 +0000885ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
886 Expr *Op) {
887 if (!Op)
888 return ExprError();
889
890 class IntConvertDiagnoser : public ICEConvertDiagnoser {
891 public:
892 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +0000893 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000894 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
895 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000896 return S.Diag(Loc, diag::err_omp_not_integral) << T;
897 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000898 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
899 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000900 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
901 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000902 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
903 QualType T,
904 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000905 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
906 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000907 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
908 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000909 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +0000910 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +0000911 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000912 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
913 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000914 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
915 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000916 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
917 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000918 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +0000919 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +0000920 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000921 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
922 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000923 llvm_unreachable("conversion functions are permitted");
924 }
925 } ConvertDiagnoser;
926 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
927}
928
929OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
930 SourceLocation StartLoc,
931 SourceLocation LParenLoc,
932 SourceLocation EndLoc) {
933 Expr *ValExpr = NumThreads;
934 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
935 !NumThreads->isInstantiationDependent() &&
936 !NumThreads->containsUnexpandedParameterPack()) {
937 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
938 ExprResult Val =
939 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
940 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000941 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000942
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000943 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +0000944
945 // OpenMP [2.5, Restrictions]
946 // The num_threads expression must evaluate to a positive integer value.
947 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +0000948 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
949 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +0000950 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
951 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000952 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000953 }
954 }
955
Alexey Bataeved09d242014-05-28 05:53:51 +0000956 return new (Context)
957 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +0000958}
959
Alexey Bataev62c87d22014-03-21 04:51:18 +0000960ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
961 OpenMPClauseKind CKind) {
962 if (!E)
963 return ExprError();
964 if (E->isValueDependent() || E->isTypeDependent() ||
965 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
966 return Owned(E);
967 llvm::APSInt Result;
968 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
969 if (ICE.isInvalid())
970 return ExprError();
971 if (!Result.isStrictlyPositive()) {
972 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
973 << getOpenMPClauseName(CKind) << E->getSourceRange();
974 return ExprError();
975 }
976 return ICE;
977}
978
979OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
980 SourceLocation LParenLoc,
981 SourceLocation EndLoc) {
982 // OpenMP [2.8.1, simd construct, Description]
983 // The parameter of the safelen clause must be a constant
984 // positive integer expression.
985 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
986 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000987 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000988 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000989 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +0000990}
991
Alexander Musman8bd31e62014-05-27 15:12:19 +0000992OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *Num, SourceLocation StartLoc,
993 SourceLocation LParenLoc,
994 SourceLocation EndLoc) {
995 // OpenMP [2.8.1, simd construct, Description]
996 // The parameter of the collapse clause must be a constant
997 // positive integer expression.
998 ExprResult NumForLoops =
999 VerifyPositiveIntegerConstantInClause(Num, OMPC_collapse);
1000 if (NumForLoops.isInvalid())
1001 return nullptr;
1002 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001003 OMPCollapseClause(NumForLoops.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001004}
1005
Alexey Bataeved09d242014-05-28 05:53:51 +00001006OMPClause *Sema::ActOnOpenMPSimpleClause(
1007 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1008 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001009 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001010 switch (Kind) {
1011 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001012 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001013 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1014 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001015 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001016 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001017 Res = ActOnOpenMPProcBindClause(
1018 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1019 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001020 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001021 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001022 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001023 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001024 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001025 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001026 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001027 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +00001028 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001029 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001030 case OMPC_threadprivate:
1031 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001032 llvm_unreachable("Clause is not allowed.");
1033 }
1034 return Res;
1035}
1036
1037OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1038 SourceLocation KindKwLoc,
1039 SourceLocation StartLoc,
1040 SourceLocation LParenLoc,
1041 SourceLocation EndLoc) {
1042 if (Kind == OMPC_DEFAULT_unknown) {
1043 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001044 static_assert(OMPC_DEFAULT_unknown > 0,
1045 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001046 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001047 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001048 Values += "'";
1049 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1050 Values += "'";
1051 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001052 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001053 Values += " or ";
1054 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001055 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001056 break;
1057 default:
1058 Values += Sep;
1059 break;
1060 }
1061 }
1062 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001063 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001064 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001065 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001066 switch (Kind) {
1067 case OMPC_DEFAULT_none:
1068 DSAStack->setDefaultDSANone();
1069 break;
1070 case OMPC_DEFAULT_shared:
1071 DSAStack->setDefaultDSAShared();
1072 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001073 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001074 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001075 break;
1076 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001077 return new (Context)
1078 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001079}
1080
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001081OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1082 SourceLocation KindKwLoc,
1083 SourceLocation StartLoc,
1084 SourceLocation LParenLoc,
1085 SourceLocation EndLoc) {
1086 if (Kind == OMPC_PROC_BIND_unknown) {
1087 std::string Values;
1088 std::string Sep(", ");
1089 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1090 Values += "'";
1091 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1092 Values += "'";
1093 switch (i) {
1094 case OMPC_PROC_BIND_unknown - 2:
1095 Values += " or ";
1096 break;
1097 case OMPC_PROC_BIND_unknown - 1:
1098 break;
1099 default:
1100 Values += Sep;
1101 break;
1102 }
1103 }
1104 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001105 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001106 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001107 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001108 return new (Context)
1109 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001110}
1111
Alexey Bataeved09d242014-05-28 05:53:51 +00001112OMPClause *
1113Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, ArrayRef<Expr *> VarList,
1114 Expr *TailExpr, SourceLocation StartLoc,
1115 SourceLocation LParenLoc,
1116 SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001117 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001118 switch (Kind) {
1119 case OMPC_private:
1120 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1121 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001122 case OMPC_firstprivate:
1123 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1124 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001125 case OMPC_shared:
1126 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1127 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001128 case OMPC_linear:
1129 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1130 ColonLoc, EndLoc);
1131 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001132 case OMPC_copyin:
1133 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1134 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001135 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001136 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001137 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001138 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001139 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001140 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001141 case OMPC_threadprivate:
1142 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001143 llvm_unreachable("Clause is not allowed.");
1144 }
1145 return Res;
1146}
1147
1148OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1149 SourceLocation StartLoc,
1150 SourceLocation LParenLoc,
1151 SourceLocation EndLoc) {
1152 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001153 for (auto &RefExpr : VarList) {
1154 assert(RefExpr && "NULL expr in OpenMP private clause.");
1155 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001156 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001157 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001158 continue;
1159 }
1160
Alexey Bataeved09d242014-05-28 05:53:51 +00001161 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001162 // OpenMP [2.1, C/C++]
1163 // A list item is a variable name.
1164 // OpenMP [2.9.3.3, Restrictions, p.1]
1165 // A variable that is part of another variable (as an array or
1166 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001167 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001168 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001169 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001170 continue;
1171 }
1172 Decl *D = DE->getDecl();
1173 VarDecl *VD = cast<VarDecl>(D);
1174
1175 QualType Type = VD->getType();
1176 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1177 // It will be analyzed later.
1178 Vars.push_back(DE);
1179 continue;
1180 }
1181
1182 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1183 // A variable that appears in a private clause must not have an incomplete
1184 // type or a reference type.
1185 if (RequireCompleteType(ELoc, Type,
1186 diag::err_omp_private_incomplete_type)) {
1187 continue;
1188 }
1189 if (Type->isReferenceType()) {
1190 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001191 << getOpenMPClauseName(OMPC_private) << Type;
1192 bool IsDecl =
1193 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1194 Diag(VD->getLocation(),
1195 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1196 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001197 continue;
1198 }
1199
1200 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1201 // A variable of class type (or array thereof) that appears in a private
1202 // clause requires an accesible, unambiguous default constructor for the
1203 // class type.
1204 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001205 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
1206 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001207 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001208 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1209 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1210 : nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001211 if (RD) {
1212 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1213 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001214 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1215 if (!CD || CheckConstructorAccess(
1216 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1217 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001218 CD->isDeleted()) {
1219 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001220 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001221 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1222 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001223 Diag(VD->getLocation(),
1224 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1225 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001226 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1227 continue;
1228 }
1229 MarkFunctionReferenced(ELoc, CD);
1230 DiagnoseUseOfDecl(CD, ELoc);
1231
1232 CXXDestructorDecl *DD = RD->getDestructor();
1233 if (DD) {
1234 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1235 DD->isDeleted()) {
1236 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001237 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001238 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1239 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001240 Diag(VD->getLocation(),
1241 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1242 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001243 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1244 continue;
1245 }
1246 MarkFunctionReferenced(ELoc, DD);
1247 DiagnoseUseOfDecl(DD, ELoc);
1248 }
1249 }
1250
Alexey Bataev758e55e2013-09-06 18:03:48 +00001251 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1252 // in a Construct]
1253 // Variables with the predetermined data-sharing attributes may not be
1254 // listed in data-sharing attributes clauses, except for the cases
1255 // listed below. For these exceptions only, listing a predetermined
1256 // variable in a data-sharing attribute clause is allowed and overrides
1257 // the variable's predetermined data-sharing attributes.
1258 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1259 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001260 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1261 << getOpenMPClauseName(OMPC_private);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001262 if (DVar.RefExpr) {
1263 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001264 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001265 } else {
1266 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001267 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001268 }
1269 continue;
1270 }
1271
1272 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001273 Vars.push_back(DE);
1274 }
1275
Alexey Bataeved09d242014-05-28 05:53:51 +00001276 if (Vars.empty())
1277 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001278
1279 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1280}
1281
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001282OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1283 SourceLocation StartLoc,
1284 SourceLocation LParenLoc,
1285 SourceLocation EndLoc) {
1286 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001287 for (auto &RefExpr : VarList) {
1288 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
1289 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001290 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001291 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001292 continue;
1293 }
1294
Alexey Bataeved09d242014-05-28 05:53:51 +00001295 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001296 // OpenMP [2.1, C/C++]
1297 // A list item is a variable name.
1298 // OpenMP [2.9.3.3, Restrictions, p.1]
1299 // A variable that is part of another variable (as an array or
1300 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001301 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001302 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001303 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001304 continue;
1305 }
1306 Decl *D = DE->getDecl();
1307 VarDecl *VD = cast<VarDecl>(D);
1308
1309 QualType Type = VD->getType();
1310 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1311 // It will be analyzed later.
1312 Vars.push_back(DE);
1313 continue;
1314 }
1315
1316 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1317 // A variable that appears in a private clause must not have an incomplete
1318 // type or a reference type.
1319 if (RequireCompleteType(ELoc, Type,
1320 diag::err_omp_firstprivate_incomplete_type)) {
1321 continue;
1322 }
1323 if (Type->isReferenceType()) {
1324 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001325 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1326 bool IsDecl =
1327 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1328 Diag(VD->getLocation(),
1329 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1330 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001331 continue;
1332 }
1333
1334 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1335 // A variable of class type (or array thereof) that appears in a private
1336 // clause requires an accesible, unambiguous copy constructor for the
1337 // class type.
1338 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001339 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1340 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1341 : nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001342 if (RD) {
1343 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1344 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001345 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1346 if (!CD || CheckConstructorAccess(
1347 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1348 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001349 CD->isDeleted()) {
1350 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001351 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001352 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1353 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001354 Diag(VD->getLocation(),
1355 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1356 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001357 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1358 continue;
1359 }
1360 MarkFunctionReferenced(ELoc, CD);
1361 DiagnoseUseOfDecl(CD, ELoc);
1362
1363 CXXDestructorDecl *DD = RD->getDestructor();
1364 if (DD) {
1365 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1366 DD->isDeleted()) {
1367 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001368 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001369 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1370 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001371 Diag(VD->getLocation(),
1372 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1373 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001374 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1375 continue;
1376 }
1377 MarkFunctionReferenced(ELoc, DD);
1378 DiagnoseUseOfDecl(DD, ELoc);
1379 }
1380 }
1381
1382 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1383 // variable and it was checked already.
1384 if (StartLoc.isValid() && EndLoc.isValid()) {
1385 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1386 Type = Type.getNonReferenceType().getCanonicalType();
1387 bool IsConstant = Type.isConstant(Context);
1388 Type = Context.getBaseElementType(Type);
1389 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1390 // A list item that specifies a given variable may not appear in more
1391 // than one clause on the same directive, except that a variable may be
1392 // specified in both firstprivate and lastprivate clauses.
1393 // TODO: add processing for lastprivate.
1394 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1395 DVar.RefExpr) {
1396 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001397 << getOpenMPClauseName(DVar.CKind)
1398 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001399 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001400 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001401 continue;
1402 }
1403
1404 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1405 // in a Construct]
1406 // Variables with the predetermined data-sharing attributes may not be
1407 // listed in data-sharing attributes clauses, except for the cases
1408 // listed below. For these exceptions only, listing a predetermined
1409 // variable in a data-sharing attribute clause is allowed and overrides
1410 // the variable's predetermined data-sharing attributes.
1411 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1412 // in a Construct, C/C++, p.2]
1413 // Variables with const-qualified type having no mutable member may be
1414 // listed in a firstprivate clause, even if they are static data members.
1415 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1416 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1417 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001418 << getOpenMPClauseName(DVar.CKind)
1419 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001420 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001421 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001422 continue;
1423 }
1424
1425 // OpenMP [2.9.3.4, Restrictions, p.2]
1426 // A list item that is private within a parallel region must not appear
1427 // in a firstprivate clause on a worksharing construct if any of the
1428 // worksharing regions arising from the worksharing construct ever bind
1429 // to any of the parallel regions arising from the parallel construct.
1430 // OpenMP [2.9.3.4, Restrictions, p.3]
1431 // A list item that appears in a reduction clause of a parallel construct
1432 // must not appear in a firstprivate clause on a worksharing or task
1433 // construct if any of the worksharing or task regions arising from the
1434 // worksharing or task construct ever bind to any of the parallel regions
1435 // arising from the parallel construct.
1436 // OpenMP [2.9.3.4, Restrictions, p.4]
1437 // A list item that appears in a reduction clause in worksharing
1438 // construct must not appear in a firstprivate clause in a task construct
1439 // encountered during execution of any of the worksharing regions arising
1440 // from the worksharing construct.
1441 // TODO:
1442 }
1443
1444 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1445 Vars.push_back(DE);
1446 }
1447
Alexey Bataeved09d242014-05-28 05:53:51 +00001448 if (Vars.empty())
1449 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001450
1451 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1452 Vars);
1453}
1454
Alexey Bataev758e55e2013-09-06 18:03:48 +00001455OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1456 SourceLocation StartLoc,
1457 SourceLocation LParenLoc,
1458 SourceLocation EndLoc) {
1459 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001460 for (auto &RefExpr : VarList) {
1461 assert(RefExpr && "NULL expr in OpenMP shared clause.");
1462 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001463 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001464 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001465 continue;
1466 }
1467
Alexey Bataeved09d242014-05-28 05:53:51 +00001468 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001469 // OpenMP [2.1, C/C++]
1470 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001471 // OpenMP [2.14.3.2, Restrictions, p.1]
1472 // A variable that is part of another variable (as an array or structure
1473 // element) cannot appear in a shared unless it is a static data member
1474 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00001475 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001477 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001478 continue;
1479 }
1480 Decl *D = DE->getDecl();
1481 VarDecl *VD = cast<VarDecl>(D);
1482
1483 QualType Type = VD->getType();
1484 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1485 // It will be analyzed later.
1486 Vars.push_back(DE);
1487 continue;
1488 }
1489
1490 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1491 // in a Construct]
1492 // Variables with the predetermined data-sharing attributes may not be
1493 // listed in data-sharing attributes clauses, except for the cases
1494 // listed below. For these exceptions only, listing a predetermined
1495 // variable in a data-sharing attribute clause is allowed and overrides
1496 // the variable's predetermined data-sharing attributes.
1497 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00001498 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
1499 DVar.RefExpr) {
1500 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1501 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001502 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001503 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001504 continue;
1505 }
1506
1507 DSAStack->addDSA(VD, DE, OMPC_shared);
1508 Vars.push_back(DE);
1509 }
1510
Alexey Bataeved09d242014-05-28 05:53:51 +00001511 if (Vars.empty())
1512 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001513
1514 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1515}
1516
Alexander Musman8dba6642014-04-22 13:09:42 +00001517OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1518 SourceLocation StartLoc,
1519 SourceLocation LParenLoc,
1520 SourceLocation ColonLoc,
1521 SourceLocation EndLoc) {
1522 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001523 for (auto &RefExpr : VarList) {
1524 assert(RefExpr && "NULL expr in OpenMP linear clause.");
1525 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001526 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001527 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00001528 continue;
1529 }
1530
1531 // OpenMP [2.14.3.7, linear clause]
1532 // A list item that appears in a linear clause is subject to the private
1533 // clause semantics described in Section 2.14.3.3 on page 159 except as
1534 // noted. In addition, the value of the new list item on each iteration
1535 // of the associated loop(s) corresponds to the value of the original
1536 // list item before entering the construct plus the logical number of
1537 // the iteration times linear-step.
1538
Alexey Bataeved09d242014-05-28 05:53:51 +00001539 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00001540 // OpenMP [2.1, C/C++]
1541 // A list item is a variable name.
1542 // OpenMP [2.14.3.3, Restrictions, p.1]
1543 // A variable that is part of another variable (as an array or
1544 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001545 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00001546 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001547 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00001548 continue;
1549 }
1550
1551 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1552
1553 // OpenMP [2.14.3.7, linear clause]
1554 // A list-item cannot appear in more than one linear clause.
1555 // A list-item that appears in a linear clause cannot appear in any
1556 // other data-sharing attribute clause.
1557 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1558 if (DVar.RefExpr) {
1559 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1560 << getOpenMPClauseName(OMPC_linear);
1561 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1562 << getOpenMPClauseName(DVar.CKind);
1563 continue;
1564 }
1565
1566 QualType QType = VD->getType();
1567 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1568 // It will be analyzed later.
1569 Vars.push_back(DE);
1570 continue;
1571 }
1572
1573 // A variable must not have an incomplete type or a reference type.
1574 if (RequireCompleteType(ELoc, QType,
1575 diag::err_omp_linear_incomplete_type)) {
1576 continue;
1577 }
1578 if (QType->isReferenceType()) {
1579 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1580 << getOpenMPClauseName(OMPC_linear) << QType;
1581 bool IsDecl =
1582 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1583 Diag(VD->getLocation(),
1584 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1585 << VD;
1586 continue;
1587 }
1588
1589 // A list item must not be const-qualified.
1590 if (QType.isConstant(Context)) {
1591 Diag(ELoc, diag::err_omp_const_variable)
1592 << getOpenMPClauseName(OMPC_linear);
1593 bool IsDecl =
1594 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1595 Diag(VD->getLocation(),
1596 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1597 << VD;
1598 continue;
1599 }
1600
1601 // A list item must be of integral or pointer type.
1602 QType = QType.getUnqualifiedType().getCanonicalType();
1603 const Type *Ty = QType.getTypePtrOrNull();
1604 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1605 !Ty->isPointerType())) {
1606 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1607 bool IsDecl =
1608 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1609 Diag(VD->getLocation(),
1610 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1611 << VD;
1612 continue;
1613 }
1614
1615 DSAStack->addDSA(VD, DE, OMPC_linear);
1616 Vars.push_back(DE);
1617 }
1618
1619 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001620 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001621
1622 Expr *StepExpr = Step;
1623 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1624 !Step->isInstantiationDependent() &&
1625 !Step->containsUnexpandedParameterPack()) {
1626 SourceLocation StepLoc = Step->getLocStart();
1627 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1628 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001629 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001630 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001631
1632 // Warn about zero linear step (it would be probably better specified as
1633 // making corresponding variables 'const').
1634 llvm::APSInt Result;
1635 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1636 !Result.isNegative() && !Result.isStrictlyPositive())
1637 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1638 << (Vars.size() > 1);
1639 }
1640
1641 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1642 Vars, StepExpr);
1643}
1644
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001645OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1646 SourceLocation StartLoc,
1647 SourceLocation LParenLoc,
1648 SourceLocation EndLoc) {
1649 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001650 for (auto &RefExpr : VarList) {
1651 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
1652 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001653 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001654 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001655 continue;
1656 }
1657
Alexey Bataeved09d242014-05-28 05:53:51 +00001658 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001659 // OpenMP [2.1, C/C++]
1660 // A list item is a variable name.
1661 // OpenMP [2.14.4.1, Restrictions, p.1]
1662 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00001663 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001664 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001665 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001666 continue;
1667 }
1668
1669 Decl *D = DE->getDecl();
1670 VarDecl *VD = cast<VarDecl>(D);
1671
1672 QualType Type = VD->getType();
1673 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1674 // It will be analyzed later.
1675 Vars.push_back(DE);
1676 continue;
1677 }
1678
1679 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1680 // A list item that appears in a copyin clause must be threadprivate.
1681 if (!DSAStack->isThreadPrivate(VD)) {
1682 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00001683 << getOpenMPClauseName(OMPC_copyin)
1684 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001685 continue;
1686 }
1687
1688 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1689 // A variable of class type (or array thereof) that appears in a
1690 // copyin clause requires an accesible, unambiguous copy assignment
1691 // operator for the class type.
1692 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001693 CXXRecordDecl *RD =
1694 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001695 if (RD) {
1696 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1697 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataeved09d242014-05-28 05:53:51 +00001698 if (!MD || CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001699 MD->isDeleted()) {
1700 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001701 << getOpenMPClauseName(OMPC_copyin) << 2;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001702 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1703 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001704 Diag(VD->getLocation(),
1705 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1706 << VD;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001707 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1708 continue;
1709 }
1710 MarkFunctionReferenced(ELoc, MD);
1711 DiagnoseUseOfDecl(MD, ELoc);
1712 }
1713
1714 DSAStack->addDSA(VD, DE, OMPC_copyin);
1715 Vars.push_back(DE);
1716 }
1717
Alexey Bataeved09d242014-05-28 05:53:51 +00001718 if (Vars.empty())
1719 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001720
1721 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1722}
1723
Alexey Bataev758e55e2013-09-06 18:03:48 +00001724#undef DSAStack