blob: 54aef9df4f4db688201efe497b612260b4f448b4 [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 {
38 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'.
41};
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;
51 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(0) { }
52 };
53private:
54 struct DSAInfo {
55 OpenMPClauseKind Attributes;
56 DeclRefExpr *RefExpr;
57 };
58 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
59
60 struct SharingMapTy {
61 DeclSAMapTy SharingMap;
62 DefaultDataSharingAttributes DefaultAttr;
63 OpenMPDirectiveKind Directive;
64 DeclarationNameInfo DirectiveName;
65 Scope *CurScope;
66 SharingMapTy(OpenMPDirectiveKind DKind,
67 const DeclarationNameInfo &Name,
68 Scope *CurScope)
69 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
70 DirectiveName(Name), CurScope(CurScope) { }
71 SharingMapTy()
72 : SharingMap(), DefaultAttr(DSA_unspecified),
73 Directive(OMPD_unknown), DirectiveName(),
74 CurScope(0) { }
75 };
76
77 typedef SmallVector<SharingMapTy, 64> StackTy;
78
79 /// \brief Stack of used declaration and their data-sharing attributes.
80 StackTy Stack;
81 Sema &Actions;
82
83 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
84
85 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +000086
87 /// \brief Checks if the variable is a local for OpenMP region.
88 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataev758e55e2013-09-06 18:03:48 +000089public:
90 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) { }
91
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};
138} // end anonymous namespace.
139
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() &&
170 (D->getStorageClass() == SC_Auto ||
171 D->getStorageClass() == SC_None)) {
172 DVar.CKind = OMPC_private;
173 return DVar;
174 }
175 }
176
Alexey Bataev758e55e2013-09-06 18:03:48 +0000177 // Explicitly specified attributes and local variables with predetermined
178 // attributes.
179 if (Iter->SharingMap.count(D)) {
180 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
181 DVar.CKind = Iter->SharingMap[D].Attributes;
182 return DVar;
183 }
184
185 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
186 // in a Construct, C/C++, implicitly determined, p.1]
187 // In a parallel or task construct, the data-sharing attributes of these
188 // variables are determined by the default clause, if present.
189 switch (Iter->DefaultAttr) {
190 case DSA_shared:
191 DVar.CKind = OMPC_shared;
192 return DVar;
193 case DSA_none:
194 return DVar;
195 case DSA_unspecified:
196 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
197 // in a Construct, implicitly determined, p.2]
198 // In a parallel construct, if no default clause is present, these
199 // variables are shared.
200 if (DVar.DKind == OMPD_parallel) {
201 DVar.CKind = OMPC_shared;
202 return DVar;
203 }
204
205 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
206 // in a Construct, implicitly determined, p.4]
207 // In a task construct, if no default clause is present, a variable that in
208 // the enclosing context is determined to be shared by all implicit tasks
209 // bound to the current team is shared.
210 // TODO
211 if (DVar.DKind == OMPD_task) {
212 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000213 for (StackTy::reverse_iterator I = std::next(Iter),
214 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000215 I != EE; ++I) {
216 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
217 // 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) {
223 DVar.RefExpr = 0;
224 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 }
228 if (I->Directive == OMPD_parallel) break;
229 }
230 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000231 DVar.CKind =
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000232 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000233 return DVar;
234 }
235 }
236 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
237 // in a Construct, implicitly determined, p.3]
238 // For constructs other than task, if no default clause is present, these
239 // variables inherit their data-sharing attributes from the enclosing
240 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000241 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000242}
243
244void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
245 if (A == OMPC_threadprivate) {
246 Stack[0].SharingMap[D].Attributes = A;
247 Stack[0].SharingMap[D].RefExpr = E;
248 } else {
249 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
250 Stack.back().SharingMap[D].Attributes = A;
251 Stack.back().SharingMap[D].RefExpr = E;
252 }
253}
254
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000255bool
256DSAStackTy::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;
259 Scope *TopScope = 0;
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 }
263 if (I == E) return false;
264 TopScope = I->CurScope ? I->CurScope->getParent() : 0;
265 Scope *CurScope = getCurScope();
266 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000267 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000268 }
269 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000271 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000272}
273
274DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
275 DSAVarData DVar;
276
277 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
278 // in a Construct, C/C++, predetermined, p.1]
279 // Variables appearing in threadprivate directives are threadprivate.
280 if (D->getTLSKind() != VarDecl::TLS_None) {
281 DVar.CKind = OMPC_threadprivate;
282 return DVar;
283 }
284 if (Stack[0].SharingMap.count(D)) {
285 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
286 DVar.CKind = OMPC_threadprivate;
287 return DVar;
288 }
289
290 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
291 // in a Construct, C/C++, predetermined, p.1]
292 // Variables with automatic storage duration that are declared in a scope
293 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000294 OpenMPDirectiveKind Kind = getCurrentDirective();
295 if (Kind != OMPD_parallel) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000296 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataevec3da872014-01-31 05:15:34 +0000297 (D->getStorageClass() == SC_Auto ||
Alexander Musman8dba6642014-04-22 13:09:42 +0000298 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 Bataevd5af8e42013-10-01 05:32:34 +0000308 // Variables with const-qualified type having no mutable member may be listed
309 // in a firstprivate clause, even if they are static data members.
310 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
311 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
312 return DVar;
313
Alexey Bataev758e55e2013-09-06 18:03:48 +0000314 DVar.CKind = OMPC_shared;
315 return DVar;
316 }
317
318 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
319 bool IsConstant = Type.isConstant(Actions.getASTContext());
320 while (Type->isArrayType()) {
321 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
322 Type = ElemType.getNonReferenceType().getCanonicalType();
323 }
324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
325 // in a Construct, C/C++, predetermined, p.6]
326 // Variables with const qualified type having no mutable member are
327 // shared.
328 CXXRecordDecl *RD = Actions.getLangOpts().CPlusPlus ?
329 Type->getAsCXXRecordDecl() : 0;
330 if (IsConstant &&
331 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
332 // Variables with const-qualified type having no mutable member may be
333 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000334 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
335 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
336 return DVar;
337
Alexey Bataev758e55e2013-09-06 18:03:48 +0000338 DVar.CKind = OMPC_shared;
339 return DVar;
340 }
341
342 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
343 // in a Construct, C/C++, predetermined, p.7]
344 // Variables with static storage duration that are declared in a scope
345 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000346 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000347 DVar.CKind = OMPC_shared;
348 return DVar;
349 }
350
351 // Explicitly specified attributes and local variables with predetermined
352 // attributes.
353 if (Stack.back().SharingMap.count(D)) {
354 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
355 DVar.CKind = Stack.back().SharingMap[D].Attributes;
356 }
357
358 return DVar;
359}
360
361DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000362 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000363}
364
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000365DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
366 OpenMPDirectiveKind DKind) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000367 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
368 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000369 I != E; ++I) {
370 if (DKind != OMPD_unknown && DKind != I->Directive) continue;
371 DSAVarData DVar = getDSA(I, D);
372 if (DVar.CKind == CKind)
373 return DVar;
374 }
375 return DSAVarData();
376}
377
Alexey Bataev758e55e2013-09-06 18:03:48 +0000378void Sema::InitDataSharingAttributesStack() {
379 VarDataSharingAttributesStack = new DSAStackTy(*this);
380}
381
382#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
383
384void Sema::DestroyDataSharingAttributesStack() {
385 delete DSAStack;
386}
387
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;
406public:
407 VarDeclFilterCCC(Sema &S) : Actions(S) { }
Craig Toppere14c0f82014-03-12 04:55:44 +0000408 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000409 NamedDecl *ND = Candidate.getCorrectionDecl();
410 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
411 return VD->hasGlobalStorage() &&
412 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
413 Actions.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000414 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000415 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000416 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000417};
418}
419
420ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
421 CXXScopeSpec &ScopeSpec,
422 const DeclarationNameInfo &Id) {
423 LookupResult Lookup(*this, Id, LookupOrdinaryName);
424 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
425
426 if (Lookup.isAmbiguous())
427 return ExprError();
428
429 VarDecl *VD;
430 if (!Lookup.isSingleResult()) {
431 VarDeclFilterCCC Validator(*this);
Richard Smithf9b15102013-08-17 00:46:16 +0000432 if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope,
John Thompson2255f2c2014-04-23 12:57:01 +0000433 0, Validator, CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000434 diagnoseTypo(Corrected,
435 PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest
436 : diag::err_omp_expected_var_arg_suggest)
437 << Id.getName());
438 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000439 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000440 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
441 : diag::err_omp_expected_var_arg)
442 << Id.getName();
443 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000444 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000445 } else {
446 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Richard Smithf9b15102013-08-17 00:46:16 +0000447 Diag(Id.getLoc(), diag::err_omp_expected_var_arg)
448 << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000449 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
450 return ExprError();
451 }
452 }
453 Lookup.suppressDiagnostics();
454
455 // OpenMP [2.9.2, Syntax, C/C++]
456 // Variables must be file-scope, namespace-scope, or static block-scope.
457 if (!VD->hasGlobalStorage()) {
458 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
459 << getOpenMPDirectiveName(OMPD_threadprivate)
460 << !VD->isStaticLocal();
461 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
462 VarDecl::DeclarationOnly;
463 Diag(VD->getLocation(),
464 IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD;
465 return ExprError();
466 }
467
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000468 VarDecl *CanonicalVD = VD->getCanonicalDecl();
469 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000470 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
471 // A threadprivate directive for file-scope variables must appear outside
472 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000473 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
474 !getCurLexicalContext()->isTranslationUnit()) {
475 Diag(Id.getLoc(), diag::err_omp_var_scope)
476 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
477 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
478 VarDecl::DeclarationOnly;
479 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
480 diag::note_defined_here) << VD;
481 return ExprError();
482 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000483 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
484 // A threadprivate directive for static class member variables must appear
485 // in the class definition, in the same scope in which the member
486 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000487 if (CanonicalVD->isStaticDataMember() &&
488 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
489 Diag(Id.getLoc(), diag::err_omp_var_scope)
490 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
491 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
492 VarDecl::DeclarationOnly;
493 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
494 diag::note_defined_here) << VD;
495 return ExprError();
496 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000497 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
498 // A threadprivate directive for namespace-scope variables must appear
499 // outside any definition or declaration other than the namespace
500 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000501 if (CanonicalVD->getDeclContext()->isNamespace() &&
502 (!getCurLexicalContext()->isFileContext() ||
503 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
504 Diag(Id.getLoc(), diag::err_omp_var_scope)
505 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
506 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
507 VarDecl::DeclarationOnly;
508 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
509 diag::note_defined_here) << VD;
510 return ExprError();
511 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000512 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
513 // A threadprivate directive for static block-scope variables must appear
514 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000515 if (CanonicalVD->isStaticLocal() && CurScope &&
516 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000517 Diag(Id.getLoc(), diag::err_omp_var_scope)
518 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
519 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
520 VarDecl::DeclarationOnly;
521 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
522 diag::note_defined_here) << VD;
523 return ExprError();
524 }
525
526 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
527 // A threadprivate directive must lexically precede all references to any
528 // of the variables in its list.
529 if (VD->isUsed()) {
530 Diag(Id.getLoc(), diag::err_omp_var_used)
531 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
532 return ExprError();
533 }
534
535 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000536 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000537 return DE;
538}
539
540Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective(
541 SourceLocation Loc,
542 ArrayRef<Expr *> VarList) {
543 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000544 CurContext->addDecl(D);
545 return DeclGroupPtrTy::make(DeclGroupRef(D));
546 }
547 return DeclGroupPtrTy();
548}
549
550OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl(
551 SourceLocation Loc,
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000552 ArrayRef<Expr *> VarList) {
553 SmallVector<Expr *, 8> Vars;
554 for (ArrayRef<Expr *>::iterator I = VarList.begin(),
Alexey Bataeva769e072013-03-22 06:34:35 +0000555 E = VarList.end();
556 I != E; ++I) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000557 DeclRefExpr *DE = cast<DeclRefExpr>(*I);
558 VarDecl *VD = cast<VarDecl>(DE->getDecl());
559 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000560
561 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
562 // A threadprivate variable must not have an incomplete type.
563 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000564 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000565 continue;
566 }
567
568 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
569 // A threadprivate variable must not have a reference type.
570 if (VD->getType()->isReferenceType()) {
571 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000572 << getOpenMPDirectiveName(OMPD_threadprivate)
573 << VD->getType();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000574 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
575 VarDecl::DeclarationOnly;
576 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
577 diag::note_defined_here) << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000578 continue;
579 }
580
Richard Smithfd3834f2013-04-13 02:43:54 +0000581 // Check if this is a TLS variable.
582 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000583 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000584 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
585 VarDecl::DeclarationOnly;
586 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
587 diag::note_defined_here) << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000588 continue;
589 }
590
591 Vars.push_back(*I);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000592 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000593 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000594 OMPThreadPrivateDecl *D = 0;
595 if (!Vars.empty()) {
596 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
597 Vars);
598 D->setAccess(AS_public);
599 }
600 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000601}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000602
Alexey Bataev758e55e2013-09-06 18:03:48 +0000603namespace {
604class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
605 DSAStackTy *Stack;
606 Sema &Actions;
607 bool ErrorFound;
608 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000609 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000610public:
611 void VisitDeclRefExpr(DeclRefExpr *E) {
612 if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000613 // Skip internally declared variables.
614 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) return;
615
616 SourceLocation ELoc = E->getExprLoc();
617
618 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
619 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
620 if (DVar.CKind != OMPC_unknown) {
621 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000622 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000623 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000624 return;
625 }
626 // The default(none) clause requires that each variable that is referenced
627 // in the construct, and does not have a predetermined data-sharing
628 // attribute, must have its data-sharing attribute explicitly determined
629 // by being listed in a data-sharing attribute clause.
630 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
631 (DKind == OMPD_parallel || DKind == OMPD_task)) {
632 ErrorFound = true;
633 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
634 return;
635 }
636
637 // OpenMP [2.9.3.6, Restrictions, p.2]
638 // A list item that appears in a reduction clause of the innermost
639 // enclosing worksharing or parallel construct may not be accessed in an
640 // explicit task.
641 // TODO:
642
643 // Define implicit data-sharing attributes for task.
644 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000645 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
646 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000647 }
648 }
649 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
650 for (ArrayRef<OMPClause *>::iterator I = S->clauses().begin(),
651 E = S->clauses().end();
652 I != E; ++I)
653 if (OMPClause *C = *I)
654 for (StmtRange R = C->children(); R; ++R)
655 if (Stmt *Child = *R)
656 Visit(Child);
657 }
658 void VisitStmt(Stmt *S) {
659 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
660 I != E; ++I)
661 if (Stmt *Child = *I)
662 if (!isa<OMPExecutableDirective>(Child))
663 Visit(Child);
664 }
665
666 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000667 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000668
669 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
670 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { }
671};
672}
673
Alexey Bataev9959db52014-05-06 10:08:46 +0000674void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
675 Scope *CurScope) {
676 switch (DKind) {
677 case OMPD_parallel: {
678 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
679 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
680 Sema::CapturedParamNameType Params[3] = {
681 std::make_pair(".global_tid.", KmpInt32PtrTy),
682 std::make_pair(".bound_tid.", KmpInt32PtrTy),
683 std::make_pair(StringRef(), QualType()) // __context with shared vars
684 };
685 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
686 break;
687 }
688 case OMPD_simd: {
689 Sema::CapturedParamNameType Params[1] = {
690 std::make_pair(StringRef(), QualType()) // __context with shared vars
691 };
692 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
693 break;
694 }
695 case OMPD_threadprivate:
696 case OMPD_task:
697 llvm_unreachable("OpenMP Directive is not allowed");
698 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000699 llvm_unreachable("Unknown OpenMP directive");
700 }
701}
702
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000703StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
704 ArrayRef<OMPClause *> Clauses,
705 Stmt *AStmt,
706 SourceLocation StartLoc,
707 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000708 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
709
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000710 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000711
712 // Check default data sharing attributes for referenced variables.
713 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
714 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
715 if (DSAChecker.isErrorFound())
716 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000717 // Generate list of implicitly defined firstprivate variables.
718 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
719 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
720
721 bool ErrorFound = false;
722 if (!DSAChecker.getImplicitFirstprivate().empty()) {
723 if (OMPClause *Implicit =
724 ActOnOpenMPFirstprivateClause(DSAChecker.getImplicitFirstprivate(),
725 SourceLocation(), SourceLocation(),
726 SourceLocation())) {
727 ClausesWithImplicit.push_back(Implicit);
728 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
729 DSAChecker.getImplicitFirstprivate().size();
730 } else
731 ErrorFound = true;
732 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000733
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000734 switch (Kind) {
735 case OMPD_parallel:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000736 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt,
737 StartLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000738 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000739 case OMPD_simd:
740 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt,
741 StartLoc, EndLoc);
742 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000743 case OMPD_threadprivate:
744 case OMPD_task:
745 llvm_unreachable("OpenMP Directive is not allowed");
746 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000747 llvm_unreachable("Unknown OpenMP directive");
748 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000749
750 if (ErrorFound) return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000751 return Res;
752}
753
754StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
755 Stmt *AStmt,
756 SourceLocation StartLoc,
757 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000758 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
759 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
760 // 1.2.2 OpenMP Language Terminology
761 // Structured block - An executable statement with a single entry at the
762 // top and a single exit at the bottom.
763 // The point of exit cannot be a branch out of the structured block.
764 // longjmp() and throw() must not violate the entry/exit criteria.
765 CS->getCapturedDecl()->setNothrow();
766
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000767 getCurFunction()->setHasBranchProtectedScope();
768
769 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
770 Clauses, AStmt));
771}
772
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000773StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
774 Stmt *AStmt,
775 SourceLocation StartLoc,
776 SourceLocation EndLoc) {
777 Stmt *CStmt = AStmt;
778 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt))
779 CStmt = CS->getCapturedStmt();
780 while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt))
781 CStmt = AS->getSubStmt();
782 ForStmt *For = dyn_cast<ForStmt>(CStmt);
783 if (!For) {
784 Diag(CStmt->getLocStart(), diag::err_omp_not_for)
785 << getOpenMPDirectiveName(OMPD_simd);
786 return StmtError();
787 }
788
789 // FIXME: Checking loop canonical form, collapsing etc.
790
791 getCurFunction()->setHasBranchProtectedScope();
792 return Owned(OMPSimdDirective::Create(Context, StartLoc, EndLoc,
793 Clauses, AStmt));
794}
795
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000796OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
797 Expr *Expr,
798 SourceLocation StartLoc,
799 SourceLocation LParenLoc,
800 SourceLocation EndLoc) {
801 OMPClause *Res = 0;
802 switch (Kind) {
803 case OMPC_if:
804 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
805 break;
Alexey Bataev568a8332014-03-06 06:15:19 +0000806 case OMPC_num_threads:
807 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
808 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000809 case OMPC_safelen:
810 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
811 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000812 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000813 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000814 case OMPC_private:
815 case OMPC_firstprivate:
816 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000817 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000818 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000819 case OMPC_threadprivate:
820 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000821 llvm_unreachable("Clause is not allowed.");
822 }
823 return Res;
824}
825
826OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition,
827 SourceLocation StartLoc,
828 SourceLocation LParenLoc,
829 SourceLocation EndLoc) {
830 Expr *ValExpr = Condition;
831 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
832 !Condition->isInstantiationDependent() &&
833 !Condition->containsUnexpandedParameterPack()) {
834 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
835 Condition->getExprLoc(),
836 Condition);
837 if (Val.isInvalid())
838 return 0;
839
840 ValExpr = Val.take();
841 }
842
843 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
844}
845
Alexey Bataev568a8332014-03-06 06:15:19 +0000846ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
847 Expr *Op) {
848 if (!Op)
849 return ExprError();
850
851 class IntConvertDiagnoser : public ICEConvertDiagnoser {
852 public:
853 IntConvertDiagnoser()
854 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false,
855 false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000856 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
857 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000858 return S.Diag(Loc, diag::err_omp_not_integral) << T;
859 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000860 SemaDiagnosticBuilder diagnoseIncomplete(
861 Sema &S, SourceLocation Loc, QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000862 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
863 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000864 SemaDiagnosticBuilder diagnoseExplicitConv(
865 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000866 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
867 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000868 SemaDiagnosticBuilder noteExplicitConv(
869 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000870 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
871 << ConvTy->isEnumeralType() << ConvTy;
872 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000873 SemaDiagnosticBuilder diagnoseAmbiguous(
874 Sema &S, SourceLocation Loc, QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000875 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
876 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000877 SemaDiagnosticBuilder noteAmbiguous(
878 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000879 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
880 << ConvTy->isEnumeralType() << ConvTy;
881 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000882 SemaDiagnosticBuilder diagnoseConversion(
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000883 Sema &, SourceLocation, QualType, QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000884 llvm_unreachable("conversion functions are permitted");
885 }
886 } ConvertDiagnoser;
887 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
888}
889
890OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
891 SourceLocation StartLoc,
892 SourceLocation LParenLoc,
893 SourceLocation EndLoc) {
894 Expr *ValExpr = NumThreads;
895 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
896 !NumThreads->isInstantiationDependent() &&
897 !NumThreads->containsUnexpandedParameterPack()) {
898 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
899 ExprResult Val =
900 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
901 if (Val.isInvalid())
902 return 0;
903
904 ValExpr = Val.take();
905
906 // OpenMP [2.5, Restrictions]
907 // The num_threads expression must evaluate to a positive integer value.
908 llvm::APSInt Result;
909 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
910 Result.isSigned() && !Result.isStrictlyPositive()) {
911 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
912 << "num_threads" << NumThreads->getSourceRange();
913 return 0;
914 }
915 }
916
917 return new (Context) OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc,
918 EndLoc);
919}
920
Alexey Bataev62c87d22014-03-21 04:51:18 +0000921ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
922 OpenMPClauseKind CKind) {
923 if (!E)
924 return ExprError();
925 if (E->isValueDependent() || E->isTypeDependent() ||
926 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
927 return Owned(E);
928 llvm::APSInt Result;
929 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
930 if (ICE.isInvalid())
931 return ExprError();
932 if (!Result.isStrictlyPositive()) {
933 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
934 << getOpenMPClauseName(CKind) << E->getSourceRange();
935 return ExprError();
936 }
937 return ICE;
938}
939
940OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
941 SourceLocation LParenLoc,
942 SourceLocation EndLoc) {
943 // OpenMP [2.8.1, simd construct, Description]
944 // The parameter of the safelen clause must be a constant
945 // positive integer expression.
946 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
947 if (Safelen.isInvalid())
948 return 0;
949 return new (Context)
950 OMPSafelenClause(Safelen.take(), StartLoc, LParenLoc, EndLoc);
951}
952
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000953OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
954 unsigned Argument,
955 SourceLocation ArgumentLoc,
956 SourceLocation StartLoc,
957 SourceLocation LParenLoc,
958 SourceLocation EndLoc) {
959 OMPClause *Res = 0;
960 switch (Kind) {
961 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000962 Res =
963 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
964 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000965 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000966 case OMPC_proc_bind:
967 Res =
968 ActOnOpenMPProcBindClause(static_cast<OpenMPProcBindClauseKind>(Argument),
969 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
970 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000971 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000972 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000973 case OMPC_safelen:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000974 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000975 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000976 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000977 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000978 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000979 case OMPC_threadprivate:
980 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000981 llvm_unreachable("Clause is not allowed.");
982 }
983 return Res;
984}
985
986OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
987 SourceLocation KindKwLoc,
988 SourceLocation StartLoc,
989 SourceLocation LParenLoc,
990 SourceLocation EndLoc) {
991 if (Kind == OMPC_DEFAULT_unknown) {
992 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000993 static_assert(OMPC_DEFAULT_unknown > 0,
994 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +0000995 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000996 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000997 Values += "'";
998 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
999 Values += "'";
1000 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001001 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001002 Values += " or ";
1003 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001004 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001005 break;
1006 default:
1007 Values += Sep;
1008 break;
1009 }
1010 }
1011 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
1012 << Values << getOpenMPClauseName(OMPC_default);
1013 return 0;
1014 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001015 switch (Kind) {
1016 case OMPC_DEFAULT_none:
1017 DSAStack->setDefaultDSANone();
1018 break;
1019 case OMPC_DEFAULT_shared:
1020 DSAStack->setDefaultDSAShared();
1021 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001022 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001023 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001024 break;
1025 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001026 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
1027 EndLoc);
1028}
1029
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001030OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1031 SourceLocation KindKwLoc,
1032 SourceLocation StartLoc,
1033 SourceLocation LParenLoc,
1034 SourceLocation EndLoc) {
1035 if (Kind == OMPC_PROC_BIND_unknown) {
1036 std::string Values;
1037 std::string Sep(", ");
1038 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1039 Values += "'";
1040 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1041 Values += "'";
1042 switch (i) {
1043 case OMPC_PROC_BIND_unknown - 2:
1044 Values += " or ";
1045 break;
1046 case OMPC_PROC_BIND_unknown - 1:
1047 break;
1048 default:
1049 Values += Sep;
1050 break;
1051 }
1052 }
1053 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
1054 << Values << getOpenMPClauseName(OMPC_proc_bind);
1055 return 0;
1056 }
1057 return new (Context) OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc,
1058 EndLoc);
1059}
1060
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001061OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
1062 ArrayRef<Expr *> VarList,
Alexander Musman8dba6642014-04-22 13:09:42 +00001063 Expr *TailExpr,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001064 SourceLocation StartLoc,
1065 SourceLocation LParenLoc,
Alexander Musman8dba6642014-04-22 13:09:42 +00001066 SourceLocation ColonLoc,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001067 SourceLocation EndLoc) {
1068 OMPClause *Res = 0;
1069 switch (Kind) {
1070 case OMPC_private:
1071 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1072 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001073 case OMPC_firstprivate:
1074 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1075 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001076 case OMPC_shared:
1077 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1078 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001079 case OMPC_linear:
1080 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1081 ColonLoc, EndLoc);
1082 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001083 case OMPC_copyin:
1084 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1085 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001086 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001087 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001088 case OMPC_safelen:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001089 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001090 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001091 case OMPC_threadprivate:
1092 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001093 llvm_unreachable("Clause is not allowed.");
1094 }
1095 return Res;
1096}
1097
1098OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1099 SourceLocation StartLoc,
1100 SourceLocation LParenLoc,
1101 SourceLocation EndLoc) {
1102 SmallVector<Expr *, 8> Vars;
1103 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1104 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001105 assert(*I && "NULL expr in OpenMP private clause.");
1106 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001107 // It will be analyzed later.
1108 Vars.push_back(*I);
1109 continue;
1110 }
1111
1112 SourceLocation ELoc = (*I)->getExprLoc();
1113 // OpenMP [2.1, C/C++]
1114 // A list item is a variable name.
1115 // OpenMP [2.9.3.3, Restrictions, p.1]
1116 // A variable that is part of another variable (as an array or
1117 // structure element) cannot appear in a private clause.
1118 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1119 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1120 Diag(ELoc, diag::err_omp_expected_var_name)
1121 << (*I)->getSourceRange();
1122 continue;
1123 }
1124 Decl *D = DE->getDecl();
1125 VarDecl *VD = cast<VarDecl>(D);
1126
1127 QualType Type = VD->getType();
1128 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1129 // It will be analyzed later.
1130 Vars.push_back(DE);
1131 continue;
1132 }
1133
1134 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1135 // A variable that appears in a private clause must not have an incomplete
1136 // type or a reference type.
1137 if (RequireCompleteType(ELoc, Type,
1138 diag::err_omp_private_incomplete_type)) {
1139 continue;
1140 }
1141 if (Type->isReferenceType()) {
1142 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1143 << getOpenMPClauseName(OMPC_private) << Type;
1144 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1145 VarDecl::DeclarationOnly;
1146 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1147 diag::note_defined_here) << VD;
1148 continue;
1149 }
1150
1151 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1152 // A variable of class type (or array thereof) that appears in a private
1153 // clause requires an accesible, unambiguous default constructor for the
1154 // class type.
1155 while (Type.getNonReferenceType()->isArrayType()) {
1156 Type = cast<ArrayType>(
1157 Type.getNonReferenceType().getTypePtr())->getElementType();
1158 }
1159 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1160 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
1161 if (RD) {
1162 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1163 PartialDiagnostic PD =
1164 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1165 if (!CD ||
1166 CheckConstructorAccess(ELoc, CD,
1167 InitializedEntity::InitializeTemporary(Type),
1168 CD->getAccess(), PD) == AR_inaccessible ||
1169 CD->isDeleted()) {
1170 Diag(ELoc, diag::err_omp_required_method)
1171 << getOpenMPClauseName(OMPC_private) << 0;
1172 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1173 VarDecl::DeclarationOnly;
1174 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1175 diag::note_defined_here) << VD;
1176 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1177 continue;
1178 }
1179 MarkFunctionReferenced(ELoc, CD);
1180 DiagnoseUseOfDecl(CD, ELoc);
1181
1182 CXXDestructorDecl *DD = RD->getDestructor();
1183 if (DD) {
1184 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1185 DD->isDeleted()) {
1186 Diag(ELoc, diag::err_omp_required_method)
1187 << getOpenMPClauseName(OMPC_private) << 4;
1188 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1189 VarDecl::DeclarationOnly;
1190 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1191 diag::note_defined_here) << VD;
1192 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1193 continue;
1194 }
1195 MarkFunctionReferenced(ELoc, DD);
1196 DiagnoseUseOfDecl(DD, ELoc);
1197 }
1198 }
1199
Alexey Bataev758e55e2013-09-06 18:03:48 +00001200 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1201 // in a Construct]
1202 // Variables with the predetermined data-sharing attributes may not be
1203 // listed in data-sharing attributes clauses, except for the cases
1204 // listed below. For these exceptions only, listing a predetermined
1205 // variable in a data-sharing attribute clause is allowed and overrides
1206 // the variable's predetermined data-sharing attributes.
1207 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1208 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
1209 Diag(ELoc, diag::err_omp_wrong_dsa)
1210 << getOpenMPClauseName(DVar.CKind)
1211 << getOpenMPClauseName(OMPC_private);
1212 if (DVar.RefExpr) {
1213 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1214 << getOpenMPClauseName(DVar.CKind);
1215 } else {
1216 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1217 << getOpenMPClauseName(DVar.CKind);
1218 }
1219 continue;
1220 }
1221
1222 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001223 Vars.push_back(DE);
1224 }
1225
1226 if (Vars.empty()) return 0;
1227
1228 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1229}
1230
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001231OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1232 SourceLocation StartLoc,
1233 SourceLocation LParenLoc,
1234 SourceLocation EndLoc) {
1235 SmallVector<Expr *, 8> Vars;
1236 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1237 I != E; ++I) {
1238 assert(*I && "NULL expr in OpenMP firstprivate clause.");
1239 if (isa<DependentScopeDeclRefExpr>(*I)) {
1240 // It will be analyzed later.
1241 Vars.push_back(*I);
1242 continue;
1243 }
1244
1245 SourceLocation ELoc = (*I)->getExprLoc();
1246 // OpenMP [2.1, C/C++]
1247 // A list item is a variable name.
1248 // OpenMP [2.9.3.3, Restrictions, p.1]
1249 // A variable that is part of another variable (as an array or
1250 // structure element) cannot appear in a private clause.
1251 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1252 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1253 Diag(ELoc, diag::err_omp_expected_var_name)
1254 << (*I)->getSourceRange();
1255 continue;
1256 }
1257 Decl *D = DE->getDecl();
1258 VarDecl *VD = cast<VarDecl>(D);
1259
1260 QualType Type = VD->getType();
1261 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1262 // It will be analyzed later.
1263 Vars.push_back(DE);
1264 continue;
1265 }
1266
1267 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1268 // A variable that appears in a private clause must not have an incomplete
1269 // type or a reference type.
1270 if (RequireCompleteType(ELoc, Type,
1271 diag::err_omp_firstprivate_incomplete_type)) {
1272 continue;
1273 }
1274 if (Type->isReferenceType()) {
1275 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1276 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1277 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1278 VarDecl::DeclarationOnly;
1279 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1280 diag::note_defined_here) << VD;
1281 continue;
1282 }
1283
1284 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1285 // A variable of class type (or array thereof) that appears in a private
1286 // clause requires an accesible, unambiguous copy constructor for the
1287 // class type.
1288 Type = Context.getBaseElementType(Type);
1289 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1290 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
1291 if (RD) {
1292 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1293 PartialDiagnostic PD =
1294 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1295 if (!CD ||
1296 CheckConstructorAccess(ELoc, CD,
1297 InitializedEntity::InitializeTemporary(Type),
1298 CD->getAccess(), PD) == AR_inaccessible ||
1299 CD->isDeleted()) {
1300 Diag(ELoc, diag::err_omp_required_method)
1301 << getOpenMPClauseName(OMPC_firstprivate) << 1;
1302 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1303 VarDecl::DeclarationOnly;
1304 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1305 diag::note_defined_here) << VD;
1306 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1307 continue;
1308 }
1309 MarkFunctionReferenced(ELoc, CD);
1310 DiagnoseUseOfDecl(CD, ELoc);
1311
1312 CXXDestructorDecl *DD = RD->getDestructor();
1313 if (DD) {
1314 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1315 DD->isDeleted()) {
1316 Diag(ELoc, diag::err_omp_required_method)
1317 << getOpenMPClauseName(OMPC_firstprivate) << 4;
1318 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1319 VarDecl::DeclarationOnly;
1320 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1321 diag::note_defined_here) << VD;
1322 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1323 continue;
1324 }
1325 MarkFunctionReferenced(ELoc, DD);
1326 DiagnoseUseOfDecl(DD, ELoc);
1327 }
1328 }
1329
1330 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1331 // variable and it was checked already.
1332 if (StartLoc.isValid() && EndLoc.isValid()) {
1333 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1334 Type = Type.getNonReferenceType().getCanonicalType();
1335 bool IsConstant = Type.isConstant(Context);
1336 Type = Context.getBaseElementType(Type);
1337 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1338 // A list item that specifies a given variable may not appear in more
1339 // than one clause on the same directive, except that a variable may be
1340 // specified in both firstprivate and lastprivate clauses.
1341 // TODO: add processing for lastprivate.
1342 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1343 DVar.RefExpr) {
1344 Diag(ELoc, diag::err_omp_wrong_dsa)
1345 << getOpenMPClauseName(DVar.CKind)
1346 << getOpenMPClauseName(OMPC_firstprivate);
1347 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1348 << getOpenMPClauseName(DVar.CKind);
1349 continue;
1350 }
1351
1352 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1353 // in a Construct]
1354 // Variables with the predetermined data-sharing attributes may not be
1355 // listed in data-sharing attributes clauses, except for the cases
1356 // listed below. For these exceptions only, listing a predetermined
1357 // variable in a data-sharing attribute clause is allowed and overrides
1358 // the variable's predetermined data-sharing attributes.
1359 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1360 // in a Construct, C/C++, p.2]
1361 // Variables with const-qualified type having no mutable member may be
1362 // listed in a firstprivate clause, even if they are static data members.
1363 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1364 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1365 Diag(ELoc, diag::err_omp_wrong_dsa)
1366 << getOpenMPClauseName(DVar.CKind)
1367 << getOpenMPClauseName(OMPC_firstprivate);
1368 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1369 << getOpenMPClauseName(DVar.CKind);
1370 continue;
1371 }
1372
1373 // OpenMP [2.9.3.4, Restrictions, p.2]
1374 // A list item that is private within a parallel region must not appear
1375 // in a firstprivate clause on a worksharing construct if any of the
1376 // worksharing regions arising from the worksharing construct ever bind
1377 // to any of the parallel regions arising from the parallel construct.
1378 // OpenMP [2.9.3.4, Restrictions, p.3]
1379 // A list item that appears in a reduction clause of a parallel construct
1380 // must not appear in a firstprivate clause on a worksharing or task
1381 // construct if any of the worksharing or task regions arising from the
1382 // worksharing or task construct ever bind to any of the parallel regions
1383 // arising from the parallel construct.
1384 // OpenMP [2.9.3.4, Restrictions, p.4]
1385 // A list item that appears in a reduction clause in worksharing
1386 // construct must not appear in a firstprivate clause in a task construct
1387 // encountered during execution of any of the worksharing regions arising
1388 // from the worksharing construct.
1389 // TODO:
1390 }
1391
1392 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1393 Vars.push_back(DE);
1394 }
1395
1396 if (Vars.empty()) return 0;
1397
1398 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1399 Vars);
1400}
1401
Alexey Bataev758e55e2013-09-06 18:03:48 +00001402OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1403 SourceLocation StartLoc,
1404 SourceLocation LParenLoc,
1405 SourceLocation EndLoc) {
1406 SmallVector<Expr *, 8> Vars;
1407 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1408 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001409 assert(*I && "NULL expr in OpenMP shared clause.");
1410 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001411 // It will be analyzed later.
1412 Vars.push_back(*I);
1413 continue;
1414 }
1415
1416 SourceLocation ELoc = (*I)->getExprLoc();
1417 // OpenMP [2.1, C/C++]
1418 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001419 // OpenMP [2.14.3.2, Restrictions, p.1]
1420 // A variable that is part of another variable (as an array or structure
1421 // element) cannot appear in a shared unless it is a static data member
1422 // of a C++ class.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001423 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1424 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1425 Diag(ELoc, diag::err_omp_expected_var_name)
1426 << (*I)->getSourceRange();
1427 continue;
1428 }
1429 Decl *D = DE->getDecl();
1430 VarDecl *VD = cast<VarDecl>(D);
1431
1432 QualType Type = VD->getType();
1433 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1434 // It will be analyzed later.
1435 Vars.push_back(DE);
1436 continue;
1437 }
1438
1439 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1440 // in a Construct]
1441 // Variables with the predetermined data-sharing attributes may not be
1442 // listed in data-sharing attributes clauses, except for the cases
1443 // listed below. For these exceptions only, listing a predetermined
1444 // variable in a data-sharing attribute clause is allowed and overrides
1445 // the variable's predetermined data-sharing attributes.
1446 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1447 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1448 Diag(ELoc, diag::err_omp_wrong_dsa)
1449 << getOpenMPClauseName(DVar.CKind)
1450 << getOpenMPClauseName(OMPC_shared);
1451 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1452 << getOpenMPClauseName(DVar.CKind);
1453 continue;
1454 }
1455
1456 DSAStack->addDSA(VD, DE, OMPC_shared);
1457 Vars.push_back(DE);
1458 }
1459
1460 if (Vars.empty()) return 0;
1461
1462 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1463}
1464
Alexander Musman8dba6642014-04-22 13:09:42 +00001465OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1466 SourceLocation StartLoc,
1467 SourceLocation LParenLoc,
1468 SourceLocation ColonLoc,
1469 SourceLocation EndLoc) {
1470 SmallVector<Expr *, 8> Vars;
1471 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1472 I != E; ++I) {
1473 assert(*I && "NULL expr in OpenMP linear clause.");
1474 if (isa<DependentScopeDeclRefExpr>(*I)) {
1475 // It will be analyzed later.
1476 Vars.push_back(*I);
1477 continue;
1478 }
1479
1480 // OpenMP [2.14.3.7, linear clause]
1481 // A list item that appears in a linear clause is subject to the private
1482 // clause semantics described in Section 2.14.3.3 on page 159 except as
1483 // noted. In addition, the value of the new list item on each iteration
1484 // of the associated loop(s) corresponds to the value of the original
1485 // list item before entering the construct plus the logical number of
1486 // the iteration times linear-step.
1487
1488 SourceLocation ELoc = (*I)->getExprLoc();
1489 // OpenMP [2.1, C/C++]
1490 // A list item is a variable name.
1491 // OpenMP [2.14.3.3, Restrictions, p.1]
1492 // A variable that is part of another variable (as an array or
1493 // structure element) cannot appear in a private clause.
1494 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1495 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1496 Diag(ELoc, diag::err_omp_expected_var_name) << (*I)->getSourceRange();
1497 continue;
1498 }
1499
1500 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1501
1502 // OpenMP [2.14.3.7, linear clause]
1503 // A list-item cannot appear in more than one linear clause.
1504 // A list-item that appears in a linear clause cannot appear in any
1505 // other data-sharing attribute clause.
1506 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1507 if (DVar.RefExpr) {
1508 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1509 << getOpenMPClauseName(OMPC_linear);
1510 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1511 << getOpenMPClauseName(DVar.CKind);
1512 continue;
1513 }
1514
1515 QualType QType = VD->getType();
1516 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1517 // It will be analyzed later.
1518 Vars.push_back(DE);
1519 continue;
1520 }
1521
1522 // A variable must not have an incomplete type or a reference type.
1523 if (RequireCompleteType(ELoc, QType,
1524 diag::err_omp_linear_incomplete_type)) {
1525 continue;
1526 }
1527 if (QType->isReferenceType()) {
1528 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1529 << getOpenMPClauseName(OMPC_linear) << QType;
1530 bool IsDecl =
1531 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1532 Diag(VD->getLocation(),
1533 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1534 << VD;
1535 continue;
1536 }
1537
1538 // A list item must not be const-qualified.
1539 if (QType.isConstant(Context)) {
1540 Diag(ELoc, diag::err_omp_const_variable)
1541 << getOpenMPClauseName(OMPC_linear);
1542 bool IsDecl =
1543 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1544 Diag(VD->getLocation(),
1545 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1546 << VD;
1547 continue;
1548 }
1549
1550 // A list item must be of integral or pointer type.
1551 QType = QType.getUnqualifiedType().getCanonicalType();
1552 const Type *Ty = QType.getTypePtrOrNull();
1553 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1554 !Ty->isPointerType())) {
1555 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1556 bool IsDecl =
1557 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1558 Diag(VD->getLocation(),
1559 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1560 << VD;
1561 continue;
1562 }
1563
1564 DSAStack->addDSA(VD, DE, OMPC_linear);
1565 Vars.push_back(DE);
1566 }
1567
1568 if (Vars.empty())
1569 return 0;
1570
1571 Expr *StepExpr = Step;
1572 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1573 !Step->isInstantiationDependent() &&
1574 !Step->containsUnexpandedParameterPack()) {
1575 SourceLocation StepLoc = Step->getLocStart();
1576 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1577 if (Val.isInvalid())
1578 return 0;
1579 StepExpr = Val.take();
1580
1581 // Warn about zero linear step (it would be probably better specified as
1582 // making corresponding variables 'const').
1583 llvm::APSInt Result;
1584 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1585 !Result.isNegative() && !Result.isStrictlyPositive())
1586 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1587 << (Vars.size() > 1);
1588 }
1589
1590 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1591 Vars, StepExpr);
1592}
1593
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001594OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1595 SourceLocation StartLoc,
1596 SourceLocation LParenLoc,
1597 SourceLocation EndLoc) {
1598 SmallVector<Expr *, 8> Vars;
1599 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1600 I != E; ++I) {
1601 assert(*I && "NULL expr in OpenMP copyin clause.");
1602 if (isa<DependentScopeDeclRefExpr>(*I)) {
1603 // It will be analyzed later.
1604 Vars.push_back(*I);
1605 continue;
1606 }
1607
1608 SourceLocation ELoc = (*I)->getExprLoc();
1609 // OpenMP [2.1, C/C++]
1610 // A list item is a variable name.
1611 // OpenMP [2.14.4.1, Restrictions, p.1]
1612 // A list item that appears in a copyin clause must be threadprivate.
1613 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1614 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1615 Diag(ELoc, diag::err_omp_expected_var_name)
1616 << (*I)->getSourceRange();
1617 continue;
1618 }
1619
1620 Decl *D = DE->getDecl();
1621 VarDecl *VD = cast<VarDecl>(D);
1622
1623 QualType Type = VD->getType();
1624 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1625 // It will be analyzed later.
1626 Vars.push_back(DE);
1627 continue;
1628 }
1629
1630 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1631 // A list item that appears in a copyin clause must be threadprivate.
1632 if (!DSAStack->isThreadPrivate(VD)) {
1633 Diag(ELoc, diag::err_omp_required_access)
1634 << getOpenMPClauseName(OMPC_copyin)
1635 << getOpenMPDirectiveName(OMPD_threadprivate);
1636 continue;
1637 }
1638
1639 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1640 // A variable of class type (or array thereof) that appears in a
1641 // copyin clause requires an accesible, unambiguous copy assignment
1642 // operator for the class type.
1643 Type = Context.getBaseElementType(Type);
1644 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
1645 Type->getAsCXXRecordDecl() : 0;
1646 if (RD) {
1647 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1648 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
1649 if (!MD ||
1650 CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
1651 MD->isDeleted()) {
1652 Diag(ELoc, diag::err_omp_required_method)
1653 << getOpenMPClauseName(OMPC_copyin) << 2;
1654 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1655 VarDecl::DeclarationOnly;
1656 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1657 diag::note_defined_here) << VD;
1658 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1659 continue;
1660 }
1661 MarkFunctionReferenced(ELoc, MD);
1662 DiagnoseUseOfDecl(MD, ELoc);
1663 }
1664
1665 DSAStack->addDSA(VD, DE, OMPC_copyin);
1666 Vars.push_back(DE);
1667 }
1668
1669 if (Vars.empty()) return 0;
1670
1671 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1672}
1673
Alexey Bataev758e55e2013-09-06 18:03:48 +00001674#undef DSAStack