blob: 603dd56c4fe88b544cedf41bb57cabbf198a3848 [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 Bataeved09d242014-05-28 05:53:51 +0000556OMPThreadPrivateDecl *
557Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000558 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000559 for (auto &RefExpr : VarList) {
560 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000561 VarDecl *VD = cast<VarDecl>(DE->getDecl());
562 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000563
564 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
565 // A threadprivate variable must not have an incomplete type.
566 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000567 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000568 continue;
569 }
570
571 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
572 // A threadprivate variable must not have a reference type.
573 if (VD->getType()->isReferenceType()) {
574 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000575 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
576 bool IsDecl =
577 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
578 Diag(VD->getLocation(),
579 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
580 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000581 continue;
582 }
583
Richard Smithfd3834f2013-04-13 02:43:54 +0000584 // Check if this is a TLS variable.
585 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000586 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000587 bool IsDecl =
588 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
589 Diag(VD->getLocation(),
590 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
591 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000592 continue;
593 }
594
Alexey Bataeved09d242014-05-28 05:53:51 +0000595 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000596 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000597 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000598 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000599 if (!Vars.empty()) {
600 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
601 Vars);
602 D->setAccess(AS_public);
603 }
604 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000605}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000606
Alexey Bataev758e55e2013-09-06 18:03:48 +0000607namespace {
608class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
609 DSAStackTy *Stack;
610 Sema &Actions;
611 bool ErrorFound;
612 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000613 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataeved09d242014-05-28 05:53:51 +0000614
Alexey Bataev758e55e2013-09-06 18:03:48 +0000615public:
616 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000617 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000618 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000619 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
620 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000621
622 SourceLocation ELoc = E->getExprLoc();
623
624 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
625 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
626 if (DVar.CKind != OMPC_unknown) {
627 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000628 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000629 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000630 return;
631 }
632 // The default(none) clause requires that each variable that is referenced
633 // in the construct, and does not have a predetermined data-sharing
634 // attribute, must have its data-sharing attribute explicitly determined
635 // by being listed in a data-sharing attribute clause.
636 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
637 (DKind == OMPD_parallel || DKind == OMPD_task)) {
638 ErrorFound = true;
639 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
640 return;
641 }
642
643 // OpenMP [2.9.3.6, Restrictions, p.2]
644 // A list item that appears in a reduction clause of the innermost
645 // enclosing worksharing or parallel construct may not be accessed in an
646 // explicit task.
647 // TODO:
648
649 // Define implicit data-sharing attributes for task.
650 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000651 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
652 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000653 }
654 }
655 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000656 for (auto C : S->clauses())
657 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000658 for (StmtRange R = C->children(); R; ++R)
659 if (Stmt *Child = *R)
660 Visit(Child);
661 }
662 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000663 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
664 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000665 if (Stmt *Child = *I)
666 if (!isa<OMPExecutableDirective>(Child))
667 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000668 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000669
670 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000671 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000672
673 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
Alexey Bataeved09d242014-05-28 05:53:51 +0000674 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000675};
Alexey Bataeved09d242014-05-28 05:53:51 +0000676} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000677
Alexey Bataev9959db52014-05-06 10:08:46 +0000678void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
679 Scope *CurScope) {
680 switch (DKind) {
681 case OMPD_parallel: {
682 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
683 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
684 Sema::CapturedParamNameType Params[3] = {
685 std::make_pair(".global_tid.", KmpInt32PtrTy),
686 std::make_pair(".bound_tid.", KmpInt32PtrTy),
687 std::make_pair(StringRef(), QualType()) // __context with shared vars
688 };
689 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
690 break;
691 }
692 case OMPD_simd: {
693 Sema::CapturedParamNameType Params[1] = {
694 std::make_pair(StringRef(), QualType()) // __context with shared vars
695 };
696 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
697 break;
698 }
699 case OMPD_threadprivate:
700 case OMPD_task:
701 llvm_unreachable("OpenMP Directive is not allowed");
702 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000703 llvm_unreachable("Unknown OpenMP directive");
704 }
705}
706
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000707StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
708 ArrayRef<OMPClause *> Clauses,
709 Stmt *AStmt,
710 SourceLocation StartLoc,
711 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000712 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
713
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000714 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000715
716 // Check default data sharing attributes for referenced variables.
717 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
718 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
719 if (DSAChecker.isErrorFound())
720 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000721 // Generate list of implicitly defined firstprivate variables.
722 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
723 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
724
725 bool ErrorFound = false;
726 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000727 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
728 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
729 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000730 ClausesWithImplicit.push_back(Implicit);
731 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +0000732 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000733 } else
734 ErrorFound = true;
735 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000736
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000737 switch (Kind) {
738 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +0000739 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
740 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000741 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000742 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +0000743 Res =
744 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000745 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000746 case OMPD_threadprivate:
747 case OMPD_task:
748 llvm_unreachable("OpenMP Directive is not allowed");
749 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000750 llvm_unreachable("Unknown OpenMP directive");
751 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000752
Alexey Bataeved09d242014-05-28 05:53:51 +0000753 if (ErrorFound)
754 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000755 return Res;
756}
757
758StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
759 Stmt *AStmt,
760 SourceLocation StartLoc,
761 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000762 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
763 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
764 // 1.2.2 OpenMP Language Terminology
765 // Structured block - An executable statement with a single entry at the
766 // top and a single exit at the bottom.
767 // The point of exit cannot be a branch out of the structured block.
768 // longjmp() and throw() must not violate the entry/exit criteria.
769 CS->getCapturedDecl()->setNothrow();
770
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000771 getCurFunction()->setHasBranchProtectedScope();
772
Alexey Bataeved09d242014-05-28 05:53:51 +0000773 return Owned(
774 OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt));
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000775}
776
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000777StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +0000778 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000779 SourceLocation EndLoc) {
780 Stmt *CStmt = AStmt;
781 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt))
782 CStmt = CS->getCapturedStmt();
783 while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt))
784 CStmt = AS->getSubStmt();
785 ForStmt *For = dyn_cast<ForStmt>(CStmt);
786 if (!For) {
787 Diag(CStmt->getLocStart(), diag::err_omp_not_for)
Alexey Bataeved09d242014-05-28 05:53:51 +0000788 << getOpenMPDirectiveName(OMPD_simd);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000789 return StmtError();
790 }
791
792 // FIXME: Checking loop canonical form, collapsing etc.
793
794 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataeved09d242014-05-28 05:53:51 +0000795 return Owned(
796 OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt));
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000797}
798
Alexey Bataeved09d242014-05-28 05:53:51 +0000799OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000800 SourceLocation StartLoc,
801 SourceLocation LParenLoc,
802 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000803 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000804 switch (Kind) {
805 case OMPC_if:
806 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
807 break;
Alexey Bataev568a8332014-03-06 06:15:19 +0000808 case OMPC_num_threads:
809 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
810 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000811 case OMPC_safelen:
812 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
813 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +0000814 case OMPC_collapse:
815 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
816 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000817 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000818 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000819 case OMPC_private:
820 case OMPC_firstprivate:
821 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000822 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000823 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000824 case OMPC_threadprivate:
825 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000826 llvm_unreachable("Clause is not allowed.");
827 }
828 return Res;
829}
830
Alexey Bataeved09d242014-05-28 05:53:51 +0000831OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000832 SourceLocation LParenLoc,
833 SourceLocation EndLoc) {
834 Expr *ValExpr = Condition;
835 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
836 !Condition->isInstantiationDependent() &&
837 !Condition->containsUnexpandedParameterPack()) {
838 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000839 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000840 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000841 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000842
843 ValExpr = Val.take();
844 }
845
846 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
847}
848
Alexey Bataev568a8332014-03-06 06:15:19 +0000849ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
850 Expr *Op) {
851 if (!Op)
852 return ExprError();
853
854 class IntConvertDiagnoser : public ICEConvertDiagnoser {
855 public:
856 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +0000857 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000858 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
859 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000860 return S.Diag(Loc, diag::err_omp_not_integral) << T;
861 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000862 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
863 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000864 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
865 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000866 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
867 QualType T,
868 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000869 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
870 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000871 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
872 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000873 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +0000874 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +0000875 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000876 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
877 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000878 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
879 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000880 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
881 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000882 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +0000883 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +0000884 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000885 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
886 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000887 llvm_unreachable("conversion functions are permitted");
888 }
889 } ConvertDiagnoser;
890 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
891}
892
893OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
894 SourceLocation StartLoc,
895 SourceLocation LParenLoc,
896 SourceLocation EndLoc) {
897 Expr *ValExpr = NumThreads;
898 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
899 !NumThreads->isInstantiationDependent() &&
900 !NumThreads->containsUnexpandedParameterPack()) {
901 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
902 ExprResult Val =
903 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
904 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000905 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000906
907 ValExpr = Val.take();
908
909 // OpenMP [2.5, Restrictions]
910 // The num_threads expression must evaluate to a positive integer value.
911 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +0000912 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
913 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +0000914 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
915 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000916 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000917 }
918 }
919
Alexey Bataeved09d242014-05-28 05:53:51 +0000920 return new (Context)
921 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +0000922}
923
Alexey Bataev62c87d22014-03-21 04:51:18 +0000924ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
925 OpenMPClauseKind CKind) {
926 if (!E)
927 return ExprError();
928 if (E->isValueDependent() || E->isTypeDependent() ||
929 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
930 return Owned(E);
931 llvm::APSInt Result;
932 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
933 if (ICE.isInvalid())
934 return ExprError();
935 if (!Result.isStrictlyPositive()) {
936 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
937 << getOpenMPClauseName(CKind) << E->getSourceRange();
938 return ExprError();
939 }
940 return ICE;
941}
942
943OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
944 SourceLocation LParenLoc,
945 SourceLocation EndLoc) {
946 // OpenMP [2.8.1, simd construct, Description]
947 // The parameter of the safelen clause must be a constant
948 // positive integer expression.
949 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
950 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000951 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000952 return new (Context)
953 OMPSafelenClause(Safelen.take(), StartLoc, LParenLoc, EndLoc);
954}
955
Alexander Musman8bd31e62014-05-27 15:12:19 +0000956OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *Num, SourceLocation StartLoc,
957 SourceLocation LParenLoc,
958 SourceLocation EndLoc) {
959 // OpenMP [2.8.1, simd construct, Description]
960 // The parameter of the collapse clause must be a constant
961 // positive integer expression.
962 ExprResult NumForLoops =
963 VerifyPositiveIntegerConstantInClause(Num, OMPC_collapse);
964 if (NumForLoops.isInvalid())
965 return nullptr;
966 return new (Context)
967 OMPCollapseClause(NumForLoops.take(), StartLoc, LParenLoc, EndLoc);
968}
969
Alexey Bataeved09d242014-05-28 05:53:51 +0000970OMPClause *Sema::ActOnOpenMPSimpleClause(
971 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
972 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000973 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000974 switch (Kind) {
975 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000976 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +0000977 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
978 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000979 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000980 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +0000981 Res = ActOnOpenMPProcBindClause(
982 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
983 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000984 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000985 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000986 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000987 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000988 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000989 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000990 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000991 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000992 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000993 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000994 case OMPC_threadprivate:
995 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000996 llvm_unreachable("Clause is not allowed.");
997 }
998 return Res;
999}
1000
1001OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1002 SourceLocation KindKwLoc,
1003 SourceLocation StartLoc,
1004 SourceLocation LParenLoc,
1005 SourceLocation EndLoc) {
1006 if (Kind == OMPC_DEFAULT_unknown) {
1007 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001008 static_assert(OMPC_DEFAULT_unknown > 0,
1009 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001010 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001011 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001012 Values += "'";
1013 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1014 Values += "'";
1015 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001016 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001017 Values += " or ";
1018 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001019 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001020 break;
1021 default:
1022 Values += Sep;
1023 break;
1024 }
1025 }
1026 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001027 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001028 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001029 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001030 switch (Kind) {
1031 case OMPC_DEFAULT_none:
1032 DSAStack->setDefaultDSANone();
1033 break;
1034 case OMPC_DEFAULT_shared:
1035 DSAStack->setDefaultDSAShared();
1036 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001037 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001038 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001039 break;
1040 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001041 return new (Context)
1042 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001043}
1044
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001045OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1046 SourceLocation KindKwLoc,
1047 SourceLocation StartLoc,
1048 SourceLocation LParenLoc,
1049 SourceLocation EndLoc) {
1050 if (Kind == OMPC_PROC_BIND_unknown) {
1051 std::string Values;
1052 std::string Sep(", ");
1053 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1054 Values += "'";
1055 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1056 Values += "'";
1057 switch (i) {
1058 case OMPC_PROC_BIND_unknown - 2:
1059 Values += " or ";
1060 break;
1061 case OMPC_PROC_BIND_unknown - 1:
1062 break;
1063 default:
1064 Values += Sep;
1065 break;
1066 }
1067 }
1068 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001069 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001070 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001071 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001072 return new (Context)
1073 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001074}
1075
Alexey Bataeved09d242014-05-28 05:53:51 +00001076OMPClause *
1077Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, ArrayRef<Expr *> VarList,
1078 Expr *TailExpr, SourceLocation StartLoc,
1079 SourceLocation LParenLoc,
1080 SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001081 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001082 switch (Kind) {
1083 case OMPC_private:
1084 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1085 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001086 case OMPC_firstprivate:
1087 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1088 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001089 case OMPC_shared:
1090 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1091 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001092 case OMPC_linear:
1093 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1094 ColonLoc, EndLoc);
1095 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001096 case OMPC_copyin:
1097 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1098 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001099 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001100 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001101 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001102 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001103 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001104 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001105 case OMPC_threadprivate:
1106 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001107 llvm_unreachable("Clause is not allowed.");
1108 }
1109 return Res;
1110}
1111
1112OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1113 SourceLocation StartLoc,
1114 SourceLocation LParenLoc,
1115 SourceLocation EndLoc) {
1116 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001117 for (auto &RefExpr : VarList) {
1118 assert(RefExpr && "NULL expr in OpenMP private clause.");
1119 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001120 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001121 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001122 continue;
1123 }
1124
Alexey Bataeved09d242014-05-28 05:53:51 +00001125 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001126 // OpenMP [2.1, C/C++]
1127 // A list item is a variable name.
1128 // OpenMP [2.9.3.3, Restrictions, p.1]
1129 // A variable that is part of another variable (as an array or
1130 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001131 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001132 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001133 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001134 continue;
1135 }
1136 Decl *D = DE->getDecl();
1137 VarDecl *VD = cast<VarDecl>(D);
1138
1139 QualType Type = VD->getType();
1140 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1141 // It will be analyzed later.
1142 Vars.push_back(DE);
1143 continue;
1144 }
1145
1146 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1147 // A variable that appears in a private clause must not have an incomplete
1148 // type or a reference type.
1149 if (RequireCompleteType(ELoc, Type,
1150 diag::err_omp_private_incomplete_type)) {
1151 continue;
1152 }
1153 if (Type->isReferenceType()) {
1154 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001155 << getOpenMPClauseName(OMPC_private) << Type;
1156 bool IsDecl =
1157 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1158 Diag(VD->getLocation(),
1159 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1160 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001161 continue;
1162 }
1163
1164 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1165 // A variable of class type (or array thereof) that appears in a private
1166 // clause requires an accesible, unambiguous default constructor for the
1167 // class type.
1168 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001169 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
1170 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001171 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001172 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1173 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1174 : nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001175 if (RD) {
1176 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1177 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001178 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1179 if (!CD || CheckConstructorAccess(
1180 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1181 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001182 CD->isDeleted()) {
1183 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001184 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001185 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1186 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001187 Diag(VD->getLocation(),
1188 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1189 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001190 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1191 continue;
1192 }
1193 MarkFunctionReferenced(ELoc, CD);
1194 DiagnoseUseOfDecl(CD, ELoc);
1195
1196 CXXDestructorDecl *DD = RD->getDestructor();
1197 if (DD) {
1198 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1199 DD->isDeleted()) {
1200 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001201 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001202 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1203 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001204 Diag(VD->getLocation(),
1205 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1206 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001207 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1208 continue;
1209 }
1210 MarkFunctionReferenced(ELoc, DD);
1211 DiagnoseUseOfDecl(DD, ELoc);
1212 }
1213 }
1214
Alexey Bataev758e55e2013-09-06 18:03:48 +00001215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1216 // in a Construct]
1217 // Variables with the predetermined data-sharing attributes may not be
1218 // listed in data-sharing attributes clauses, except for the cases
1219 // listed below. For these exceptions only, listing a predetermined
1220 // variable in a data-sharing attribute clause is allowed and overrides
1221 // the variable's predetermined data-sharing attributes.
1222 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1223 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001224 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1225 << getOpenMPClauseName(OMPC_private);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001226 if (DVar.RefExpr) {
1227 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001228 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001229 } else {
1230 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001231 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001232 }
1233 continue;
1234 }
1235
1236 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001237 Vars.push_back(DE);
1238 }
1239
Alexey Bataeved09d242014-05-28 05:53:51 +00001240 if (Vars.empty())
1241 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001242
1243 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1244}
1245
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001246OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1247 SourceLocation StartLoc,
1248 SourceLocation LParenLoc,
1249 SourceLocation EndLoc) {
1250 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001251 for (auto &RefExpr : VarList) {
1252 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
1253 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001254 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001255 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001256 continue;
1257 }
1258
Alexey Bataeved09d242014-05-28 05:53:51 +00001259 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001260 // OpenMP [2.1, C/C++]
1261 // A list item is a variable name.
1262 // OpenMP [2.9.3.3, Restrictions, p.1]
1263 // A variable that is part of another variable (as an array or
1264 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001265 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001266 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001267 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001268 continue;
1269 }
1270 Decl *D = DE->getDecl();
1271 VarDecl *VD = cast<VarDecl>(D);
1272
1273 QualType Type = VD->getType();
1274 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1275 // It will be analyzed later.
1276 Vars.push_back(DE);
1277 continue;
1278 }
1279
1280 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1281 // A variable that appears in a private clause must not have an incomplete
1282 // type or a reference type.
1283 if (RequireCompleteType(ELoc, Type,
1284 diag::err_omp_firstprivate_incomplete_type)) {
1285 continue;
1286 }
1287 if (Type->isReferenceType()) {
1288 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001289 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1290 bool IsDecl =
1291 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1292 Diag(VD->getLocation(),
1293 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1294 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001295 continue;
1296 }
1297
1298 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1299 // A variable of class type (or array thereof) that appears in a private
1300 // clause requires an accesible, unambiguous copy constructor for the
1301 // class type.
1302 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001303 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1304 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1305 : nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001306 if (RD) {
1307 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1308 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001309 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1310 if (!CD || CheckConstructorAccess(
1311 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1312 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001313 CD->isDeleted()) {
1314 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001315 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001316 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1317 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001318 Diag(VD->getLocation(),
1319 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1320 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001321 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1322 continue;
1323 }
1324 MarkFunctionReferenced(ELoc, CD);
1325 DiagnoseUseOfDecl(CD, ELoc);
1326
1327 CXXDestructorDecl *DD = RD->getDestructor();
1328 if (DD) {
1329 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1330 DD->isDeleted()) {
1331 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001332 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001333 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1334 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001335 Diag(VD->getLocation(),
1336 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1337 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001338 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1339 continue;
1340 }
1341 MarkFunctionReferenced(ELoc, DD);
1342 DiagnoseUseOfDecl(DD, ELoc);
1343 }
1344 }
1345
1346 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1347 // variable and it was checked already.
1348 if (StartLoc.isValid() && EndLoc.isValid()) {
1349 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1350 Type = Type.getNonReferenceType().getCanonicalType();
1351 bool IsConstant = Type.isConstant(Context);
1352 Type = Context.getBaseElementType(Type);
1353 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1354 // A list item that specifies a given variable may not appear in more
1355 // than one clause on the same directive, except that a variable may be
1356 // specified in both firstprivate and lastprivate clauses.
1357 // TODO: add processing for lastprivate.
1358 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1359 DVar.RefExpr) {
1360 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001361 << getOpenMPClauseName(DVar.CKind)
1362 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001363 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001364 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001365 continue;
1366 }
1367
1368 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1369 // in a Construct]
1370 // Variables with the predetermined data-sharing attributes may not be
1371 // listed in data-sharing attributes clauses, except for the cases
1372 // listed below. For these exceptions only, listing a predetermined
1373 // variable in a data-sharing attribute clause is allowed and overrides
1374 // the variable's predetermined data-sharing attributes.
1375 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1376 // in a Construct, C/C++, p.2]
1377 // Variables with const-qualified type having no mutable member may be
1378 // listed in a firstprivate clause, even if they are static data members.
1379 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1380 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1381 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001382 << getOpenMPClauseName(DVar.CKind)
1383 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001384 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001385 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001386 continue;
1387 }
1388
1389 // OpenMP [2.9.3.4, Restrictions, p.2]
1390 // A list item that is private within a parallel region must not appear
1391 // in a firstprivate clause on a worksharing construct if any of the
1392 // worksharing regions arising from the worksharing construct ever bind
1393 // to any of the parallel regions arising from the parallel construct.
1394 // OpenMP [2.9.3.4, Restrictions, p.3]
1395 // A list item that appears in a reduction clause of a parallel construct
1396 // must not appear in a firstprivate clause on a worksharing or task
1397 // construct if any of the worksharing or task regions arising from the
1398 // worksharing or task construct ever bind to any of the parallel regions
1399 // arising from the parallel construct.
1400 // OpenMP [2.9.3.4, Restrictions, p.4]
1401 // A list item that appears in a reduction clause in worksharing
1402 // construct must not appear in a firstprivate clause in a task construct
1403 // encountered during execution of any of the worksharing regions arising
1404 // from the worksharing construct.
1405 // TODO:
1406 }
1407
1408 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1409 Vars.push_back(DE);
1410 }
1411
Alexey Bataeved09d242014-05-28 05:53:51 +00001412 if (Vars.empty())
1413 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001414
1415 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1416 Vars);
1417}
1418
Alexey Bataev758e55e2013-09-06 18:03:48 +00001419OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1420 SourceLocation StartLoc,
1421 SourceLocation LParenLoc,
1422 SourceLocation EndLoc) {
1423 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001424 for (auto &RefExpr : VarList) {
1425 assert(RefExpr && "NULL expr in OpenMP shared clause.");
1426 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001427 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001428 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001429 continue;
1430 }
1431
Alexey Bataeved09d242014-05-28 05:53:51 +00001432 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001433 // OpenMP [2.1, C/C++]
1434 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001435 // OpenMP [2.14.3.2, Restrictions, p.1]
1436 // A variable that is part of another variable (as an array or structure
1437 // element) cannot appear in a shared unless it is a static data member
1438 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00001439 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001440 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001441 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001442 continue;
1443 }
1444 Decl *D = DE->getDecl();
1445 VarDecl *VD = cast<VarDecl>(D);
1446
1447 QualType Type = VD->getType();
1448 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1449 // It will be analyzed later.
1450 Vars.push_back(DE);
1451 continue;
1452 }
1453
1454 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1455 // in a Construct]
1456 // Variables with the predetermined data-sharing attributes may not be
1457 // listed in data-sharing attributes clauses, except for the cases
1458 // listed below. For these exceptions only, listing a predetermined
1459 // variable in a data-sharing attribute clause is allowed and overrides
1460 // the variable's predetermined data-sharing attributes.
1461 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00001462 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
1463 DVar.RefExpr) {
1464 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1465 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001466 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001467 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 continue;
1469 }
1470
1471 DSAStack->addDSA(VD, DE, OMPC_shared);
1472 Vars.push_back(DE);
1473 }
1474
Alexey Bataeved09d242014-05-28 05:53:51 +00001475 if (Vars.empty())
1476 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001477
1478 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1479}
1480
Alexander Musman8dba6642014-04-22 13:09:42 +00001481OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1482 SourceLocation StartLoc,
1483 SourceLocation LParenLoc,
1484 SourceLocation ColonLoc,
1485 SourceLocation EndLoc) {
1486 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001487 for (auto &RefExpr : VarList) {
1488 assert(RefExpr && "NULL expr in OpenMP linear clause.");
1489 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001490 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001491 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00001492 continue;
1493 }
1494
1495 // OpenMP [2.14.3.7, linear clause]
1496 // A list item that appears in a linear clause is subject to the private
1497 // clause semantics described in Section 2.14.3.3 on page 159 except as
1498 // noted. In addition, the value of the new list item on each iteration
1499 // of the associated loop(s) corresponds to the value of the original
1500 // list item before entering the construct plus the logical number of
1501 // the iteration times linear-step.
1502
Alexey Bataeved09d242014-05-28 05:53:51 +00001503 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00001504 // OpenMP [2.1, C/C++]
1505 // A list item is a variable name.
1506 // OpenMP [2.14.3.3, Restrictions, p.1]
1507 // A variable that is part of another variable (as an array or
1508 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001509 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00001510 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001511 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00001512 continue;
1513 }
1514
1515 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1516
1517 // OpenMP [2.14.3.7, linear clause]
1518 // A list-item cannot appear in more than one linear clause.
1519 // A list-item that appears in a linear clause cannot appear in any
1520 // other data-sharing attribute clause.
1521 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1522 if (DVar.RefExpr) {
1523 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1524 << getOpenMPClauseName(OMPC_linear);
1525 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1526 << getOpenMPClauseName(DVar.CKind);
1527 continue;
1528 }
1529
1530 QualType QType = VD->getType();
1531 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1532 // It will be analyzed later.
1533 Vars.push_back(DE);
1534 continue;
1535 }
1536
1537 // A variable must not have an incomplete type or a reference type.
1538 if (RequireCompleteType(ELoc, QType,
1539 diag::err_omp_linear_incomplete_type)) {
1540 continue;
1541 }
1542 if (QType->isReferenceType()) {
1543 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1544 << getOpenMPClauseName(OMPC_linear) << QType;
1545 bool IsDecl =
1546 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1547 Diag(VD->getLocation(),
1548 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1549 << VD;
1550 continue;
1551 }
1552
1553 // A list item must not be const-qualified.
1554 if (QType.isConstant(Context)) {
1555 Diag(ELoc, diag::err_omp_const_variable)
1556 << getOpenMPClauseName(OMPC_linear);
1557 bool IsDecl =
1558 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1559 Diag(VD->getLocation(),
1560 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1561 << VD;
1562 continue;
1563 }
1564
1565 // A list item must be of integral or pointer type.
1566 QType = QType.getUnqualifiedType().getCanonicalType();
1567 const Type *Ty = QType.getTypePtrOrNull();
1568 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1569 !Ty->isPointerType())) {
1570 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1571 bool IsDecl =
1572 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1573 Diag(VD->getLocation(),
1574 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1575 << VD;
1576 continue;
1577 }
1578
1579 DSAStack->addDSA(VD, DE, OMPC_linear);
1580 Vars.push_back(DE);
1581 }
1582
1583 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001584 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001585
1586 Expr *StepExpr = Step;
1587 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1588 !Step->isInstantiationDependent() &&
1589 !Step->containsUnexpandedParameterPack()) {
1590 SourceLocation StepLoc = Step->getLocStart();
1591 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1592 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001593 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001594 StepExpr = Val.take();
1595
1596 // Warn about zero linear step (it would be probably better specified as
1597 // making corresponding variables 'const').
1598 llvm::APSInt Result;
1599 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1600 !Result.isNegative() && !Result.isStrictlyPositive())
1601 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1602 << (Vars.size() > 1);
1603 }
1604
1605 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1606 Vars, StepExpr);
1607}
1608
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001609OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1610 SourceLocation StartLoc,
1611 SourceLocation LParenLoc,
1612 SourceLocation EndLoc) {
1613 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001614 for (auto &RefExpr : VarList) {
1615 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
1616 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001617 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001618 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001619 continue;
1620 }
1621
Alexey Bataeved09d242014-05-28 05:53:51 +00001622 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001623 // OpenMP [2.1, C/C++]
1624 // A list item is a variable name.
1625 // OpenMP [2.14.4.1, Restrictions, p.1]
1626 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00001627 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001628 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001629 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001630 continue;
1631 }
1632
1633 Decl *D = DE->getDecl();
1634 VarDecl *VD = cast<VarDecl>(D);
1635
1636 QualType Type = VD->getType();
1637 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1638 // It will be analyzed later.
1639 Vars.push_back(DE);
1640 continue;
1641 }
1642
1643 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1644 // A list item that appears in a copyin clause must be threadprivate.
1645 if (!DSAStack->isThreadPrivate(VD)) {
1646 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00001647 << getOpenMPClauseName(OMPC_copyin)
1648 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001649 continue;
1650 }
1651
1652 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1653 // A variable of class type (or array thereof) that appears in a
1654 // copyin clause requires an accesible, unambiguous copy assignment
1655 // operator for the class type.
1656 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001657 CXXRecordDecl *RD =
1658 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001659 if (RD) {
1660 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1661 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataeved09d242014-05-28 05:53:51 +00001662 if (!MD || CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001663 MD->isDeleted()) {
1664 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001665 << getOpenMPClauseName(OMPC_copyin) << 2;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001666 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1667 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001668 Diag(VD->getLocation(),
1669 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1670 << VD;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001671 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1672 continue;
1673 }
1674 MarkFunctionReferenced(ELoc, MD);
1675 DiagnoseUseOfDecl(MD, ELoc);
1676 }
1677
1678 DSAStack->addDSA(VD, DE, OMPC_copyin);
1679 Vars.push_back(DE);
1680 }
1681
Alexey Bataeved09d242014-05-28 05:53:51 +00001682 if (Vars.empty())
1683 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001684
1685 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1686}
1687
Alexey Bataev758e55e2013-09-06 18:03:48 +00001688#undef DSAStack