blob: 18cfd43c80449a0fe620ba75a0060f5200eef8bb [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;
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 };
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;
Alexander Musmancb7f9c42014-05-15 13:04:49 +000066 SharingMapTy(OpenMPDirectiveKind DKind, const DeclarationNameInfo &Name,
Alexey Bataev758e55e2013-09-06 18:03:48 +000067 Scope *CurScope)
Alexander Musmancb7f9c42014-05-15 13:04:49 +000068 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
69 DirectiveName(Name), CurScope(CurScope) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 SharingMapTy()
Alexander Musmancb7f9c42014-05-15 13:04:49 +000071 : SharingMap(), DefaultAttr(DSA_unspecified), Directive(OMPD_unknown),
72 DirectiveName(), CurScope(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000073 };
74
75 typedef SmallVector<SharingMapTy, 64> StackTy;
76
77 /// \brief Stack of used declaration and their data-sharing attributes.
78 StackTy Stack;
79 Sema &Actions;
80
81 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
82
83 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +000084
85 /// \brief Checks if the variable is a local for OpenMP region.
86 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataev758e55e2013-09-06 18:03:48 +000087public:
88 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) { }
89
90 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
91 Scope *CurScope) {
92 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
93 }
94
95 void pop() {
96 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
97 Stack.pop_back();
98 }
99
100 /// \brief Adds explicit data sharing attribute to the specified declaration.
101 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
102
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 /// \brief Returns data sharing attributes from top of the stack for the
104 /// specified declaration.
105 DSAVarData getTopDSA(VarDecl *D);
106 /// \brief Returns data-sharing attributes for the specified declaration.
107 DSAVarData getImplicitDSA(VarDecl *D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000108 /// \brief Checks if the specified variables has \a CKind data-sharing
109 /// attribute in \a DKind directive.
110 DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
111 OpenMPDirectiveKind DKind = OMPD_unknown);
112
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113 /// \brief Returns currently analyzed directive.
114 OpenMPDirectiveKind getCurrentDirective() const {
115 return Stack.back().Directive;
116 }
117
118 /// \brief Set default data sharing attribute to none.
119 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
120 /// \brief Set default data sharing attribute to shared.
121 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
122
123 DefaultDataSharingAttributes getDefaultDSA() const {
124 return Stack.back().DefaultAttr;
125 }
126
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000127 /// \brief Checks if the spewcified variable is threadprivate.
128 bool isThreadPrivate(VarDecl *D) {
129 DSAVarData DVar = getTopDSA(D);
130 return (DVar.CKind == OMPC_threadprivate || DVar.CKind == OMPC_copyin);
131 }
132
133 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000134 Scope *getCurScope() { return Stack.back().CurScope; }
135};
136} // end anonymous namespace.
137
138DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
139 VarDecl *D) {
140 DSAVarData DVar;
141 if (Iter == Stack.rend() - 1) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000142 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
143 // in a region but not in construct]
144 // File-scope or namespace-scope variables referenced in called routines
145 // in the region are shared unless they appear in a threadprivate
146 // directive.
147 // TODO
148 if (!D->isFunctionOrMethodVarDecl())
149 DVar.CKind = OMPC_shared;
150
151 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
152 // in a region but not in construct]
153 // Variables with static storage duration that are declared in called
154 // routines in the region are shared.
155 if (D->hasGlobalStorage())
156 DVar.CKind = OMPC_shared;
157
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158 return DVar;
159 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000160
Alexey Bataev758e55e2013-09-06 18:03:48 +0000161 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000162 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
163 // in a Construct, C/C++, predetermined, p.1]
164 // Variables with automatic storage duration that are declared in a scope
165 // inside the construct are private.
166 if (DVar.DKind != OMPD_parallel) {
167 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
168 (D->getStorageClass() == SC_Auto ||
169 D->getStorageClass() == SC_None)) {
170 DVar.CKind = OMPC_private;
171 return DVar;
172 }
173 }
174
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175 // Explicitly specified attributes and local variables with predetermined
176 // attributes.
177 if (Iter->SharingMap.count(D)) {
178 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
179 DVar.CKind = Iter->SharingMap[D].Attributes;
180 return DVar;
181 }
182
183 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
184 // in a Construct, C/C++, implicitly determined, p.1]
185 // In a parallel or task construct, the data-sharing attributes of these
186 // variables are determined by the default clause, if present.
187 switch (Iter->DefaultAttr) {
188 case DSA_shared:
189 DVar.CKind = OMPC_shared;
190 return DVar;
191 case DSA_none:
192 return DVar;
193 case DSA_unspecified:
194 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
195 // in a Construct, implicitly determined, p.2]
196 // In a parallel construct, if no default clause is present, these
197 // variables are shared.
198 if (DVar.DKind == OMPD_parallel) {
199 DVar.CKind = OMPC_shared;
200 return DVar;
201 }
202
203 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
204 // in a Construct, implicitly determined, p.4]
205 // In a task construct, if no default clause is present, a variable that in
206 // the enclosing context is determined to be shared by all implicit tasks
207 // bound to the current team is shared.
208 // TODO
209 if (DVar.DKind == OMPD_task) {
210 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000211 for (StackTy::reverse_iterator I = std::next(Iter),
212 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000213 I != EE; ++I) {
214 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
215 // in a Construct, implicitly determined, p.6]
216 // In a task construct, if no default clause is present, a variable
217 // whose data-sharing attribute is not determined by the rules above is
218 // firstprivate.
219 DVarTemp = getDSA(I, D);
220 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000221 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000222 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000223 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000224 return DVar;
225 }
226 if (I->Directive == OMPD_parallel) break;
227 }
228 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000229 DVar.CKind =
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000230 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000231 return DVar;
232 }
233 }
234 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
235 // in a Construct, implicitly determined, p.3]
236 // For constructs other than task, if no default clause is present, these
237 // variables inherit their data-sharing attributes from the enclosing
238 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000239 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240}
241
242void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
243 if (A == OMPC_threadprivate) {
244 Stack[0].SharingMap[D].Attributes = A;
245 Stack[0].SharingMap[D].RefExpr = E;
246 } else {
247 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
248 Stack.back().SharingMap[D].Attributes = A;
249 Stack.back().SharingMap[D].RefExpr = E;
250 }
251}
252
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000253bool
254DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000255 if (Stack.size() > 2) {
256 reverse_iterator I = Iter, E = Stack.rend() - 1;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000257 Scope *TopScope = nullptr;
Fraser Cormack111023c2014-04-15 08:59:09 +0000258 while (I != E && I->Directive != OMPD_parallel) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000259 ++I;
260 }
261 if (I == E) return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000262 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000263 Scope *CurScope = getCurScope();
264 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000265 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000266 }
267 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000268 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000269 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270}
271
272DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
273 DSAVarData DVar;
274
275 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
276 // in a Construct, C/C++, predetermined, p.1]
277 // Variables appearing in threadprivate directives are threadprivate.
278 if (D->getTLSKind() != VarDecl::TLS_None) {
279 DVar.CKind = OMPC_threadprivate;
280 return DVar;
281 }
282 if (Stack[0].SharingMap.count(D)) {
283 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
284 DVar.CKind = OMPC_threadprivate;
285 return DVar;
286 }
287
288 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
289 // in a Construct, C/C++, predetermined, p.1]
290 // Variables with automatic storage duration that are declared in a scope
291 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000292 OpenMPDirectiveKind Kind = getCurrentDirective();
293 if (Kind != OMPD_parallel) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000294 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataevec3da872014-01-31 05:15:34 +0000295 (D->getStorageClass() == SC_Auto ||
Alexander Musman8dba6642014-04-22 13:09:42 +0000296 D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000297 DVar.CKind = OMPC_private;
298 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000299 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000300 }
301
302 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
303 // in a Construct, C/C++, predetermined, p.4]
304 // Static data memebers are shared.
305 if (D->isStaticDataMember()) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000306 // Variables with const-qualified type having no mutable member may be listed
307 // in a firstprivate clause, even if they are static data members.
308 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
309 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
310 return DVar;
311
Alexey Bataev758e55e2013-09-06 18:03:48 +0000312 DVar.CKind = OMPC_shared;
313 return DVar;
314 }
315
316 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
317 bool IsConstant = Type.isConstant(Actions.getASTContext());
318 while (Type->isArrayType()) {
319 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
320 Type = ElemType.getNonReferenceType().getCanonicalType();
321 }
322 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
323 // in a Construct, C/C++, predetermined, p.6]
324 // Variables with const qualified type having no mutable member are
325 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000326 CXXRecordDecl *RD =
327 Actions.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000328 if (IsConstant &&
329 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
330 // Variables with const-qualified type having no mutable member may be
331 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000332 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
333 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
334 return DVar;
335
Alexey Bataev758e55e2013-09-06 18:03:48 +0000336 DVar.CKind = OMPC_shared;
337 return DVar;
338 }
339
340 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
341 // in a Construct, C/C++, predetermined, p.7]
342 // Variables with static storage duration that are declared in a scope
343 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000344 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345 DVar.CKind = OMPC_shared;
346 return DVar;
347 }
348
349 // Explicitly specified attributes and local variables with predetermined
350 // attributes.
351 if (Stack.back().SharingMap.count(D)) {
352 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
353 DVar.CKind = Stack.back().SharingMap[D].Attributes;
354 }
355
356 return DVar;
357}
358
359DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000360 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000361}
362
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000363DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
364 OpenMPDirectiveKind DKind) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000365 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
366 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000367 I != E; ++I) {
368 if (DKind != OMPD_unknown && DKind != I->Directive) continue;
369 DSAVarData DVar = getDSA(I, D);
370 if (DVar.CKind == CKind)
371 return DVar;
372 }
373 return DSAVarData();
374}
375
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376void Sema::InitDataSharingAttributesStack() {
377 VarDataSharingAttributesStack = new DSAStackTy(*this);
378}
379
380#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
381
382void Sema::DestroyDataSharingAttributesStack() {
383 delete DSAStack;
384}
385
386void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
387 const DeclarationNameInfo &DirName,
388 Scope *CurScope) {
389 DSAStack->push(DKind, DirName, CurScope);
390 PushExpressionEvaluationContext(PotentiallyEvaluated);
391}
392
393void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
394 DSAStack->pop();
395 DiscardCleanupsInEvaluationContext();
396 PopExpressionEvaluationContext();
397}
398
Alexey Bataeva769e072013-03-22 06:34:35 +0000399namespace {
400
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000401class VarDeclFilterCCC : public CorrectionCandidateCallback {
402private:
403 Sema &Actions;
404public:
405 VarDeclFilterCCC(Sema &S) : Actions(S) { }
Craig Toppere14c0f82014-03-12 04:55:44 +0000406 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000407 NamedDecl *ND = Candidate.getCorrectionDecl();
408 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
409 return VD->hasGlobalStorage() &&
410 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
411 Actions.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000412 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000413 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000414 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000415};
416}
417
418ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
419 CXXScopeSpec &ScopeSpec,
420 const DeclarationNameInfo &Id) {
421 LookupResult Lookup(*this, Id, LookupOrdinaryName);
422 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
423
424 if (Lookup.isAmbiguous())
425 return ExprError();
426
427 VarDecl *VD;
428 if (!Lookup.isSingleResult()) {
429 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000430 if (TypoCorrection Corrected =
431 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
432 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000433 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000434 PDiag(Lookup.empty()
435 ? diag::err_undeclared_var_use_suggest
436 : diag::err_omp_expected_var_arg_suggest)
437 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000438 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 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000594 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000595 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) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000801 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000802 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())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000838 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000839
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())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000902 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000903
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();
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000913 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000914 }
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())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000948 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000949 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) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000959 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000960 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);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001013 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001014 }
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);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001055 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001056 }
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) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001068 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001069 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 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001159 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1160 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1161 : nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001162 if (RD) {
1163 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1164 PartialDiagnostic PD =
1165 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1166 if (!CD ||
1167 CheckConstructorAccess(ELoc, CD,
1168 InitializedEntity::InitializeTemporary(Type),
1169 CD->getAccess(), PD) == AR_inaccessible ||
1170 CD->isDeleted()) {
1171 Diag(ELoc, diag::err_omp_required_method)
1172 << getOpenMPClauseName(OMPC_private) << 0;
1173 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1174 VarDecl::DeclarationOnly;
1175 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1176 diag::note_defined_here) << VD;
1177 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1178 continue;
1179 }
1180 MarkFunctionReferenced(ELoc, CD);
1181 DiagnoseUseOfDecl(CD, ELoc);
1182
1183 CXXDestructorDecl *DD = RD->getDestructor();
1184 if (DD) {
1185 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1186 DD->isDeleted()) {
1187 Diag(ELoc, diag::err_omp_required_method)
1188 << getOpenMPClauseName(OMPC_private) << 4;
1189 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1190 VarDecl::DeclarationOnly;
1191 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1192 diag::note_defined_here) << VD;
1193 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1194 continue;
1195 }
1196 MarkFunctionReferenced(ELoc, DD);
1197 DiagnoseUseOfDecl(DD, ELoc);
1198 }
1199 }
1200
Alexey Bataev758e55e2013-09-06 18:03:48 +00001201 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1202 // in a Construct]
1203 // Variables with the predetermined data-sharing attributes may not be
1204 // listed in data-sharing attributes clauses, except for the cases
1205 // listed below. For these exceptions only, listing a predetermined
1206 // variable in a data-sharing attribute clause is allowed and overrides
1207 // the variable's predetermined data-sharing attributes.
1208 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1209 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
1210 Diag(ELoc, diag::err_omp_wrong_dsa)
1211 << getOpenMPClauseName(DVar.CKind)
1212 << getOpenMPClauseName(OMPC_private);
1213 if (DVar.RefExpr) {
1214 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1215 << getOpenMPClauseName(DVar.CKind);
1216 } else {
1217 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1218 << getOpenMPClauseName(DVar.CKind);
1219 }
1220 continue;
1221 }
1222
1223 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001224 Vars.push_back(DE);
1225 }
1226
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001227 if (Vars.empty()) return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001228
1229 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1230}
1231
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001232OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1233 SourceLocation StartLoc,
1234 SourceLocation LParenLoc,
1235 SourceLocation EndLoc) {
1236 SmallVector<Expr *, 8> Vars;
1237 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1238 I != E; ++I) {
1239 assert(*I && "NULL expr in OpenMP firstprivate clause.");
1240 if (isa<DependentScopeDeclRefExpr>(*I)) {
1241 // It will be analyzed later.
1242 Vars.push_back(*I);
1243 continue;
1244 }
1245
1246 SourceLocation ELoc = (*I)->getExprLoc();
1247 // OpenMP [2.1, C/C++]
1248 // A list item is a variable name.
1249 // OpenMP [2.9.3.3, Restrictions, p.1]
1250 // A variable that is part of another variable (as an array or
1251 // structure element) cannot appear in a private clause.
1252 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1253 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1254 Diag(ELoc, diag::err_omp_expected_var_name)
1255 << (*I)->getSourceRange();
1256 continue;
1257 }
1258 Decl *D = DE->getDecl();
1259 VarDecl *VD = cast<VarDecl>(D);
1260
1261 QualType Type = VD->getType();
1262 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1263 // It will be analyzed later.
1264 Vars.push_back(DE);
1265 continue;
1266 }
1267
1268 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1269 // A variable that appears in a private clause must not have an incomplete
1270 // type or a reference type.
1271 if (RequireCompleteType(ELoc, Type,
1272 diag::err_omp_firstprivate_incomplete_type)) {
1273 continue;
1274 }
1275 if (Type->isReferenceType()) {
1276 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1277 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1278 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1279 VarDecl::DeclarationOnly;
1280 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1281 diag::note_defined_here) << VD;
1282 continue;
1283 }
1284
1285 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1286 // A variable of class type (or array thereof) that appears in a private
1287 // clause requires an accesible, unambiguous copy constructor for the
1288 // class type.
1289 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001290 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1291 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1292 : nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001293 if (RD) {
1294 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1295 PartialDiagnostic PD =
1296 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1297 if (!CD ||
1298 CheckConstructorAccess(ELoc, CD,
1299 InitializedEntity::InitializeTemporary(Type),
1300 CD->getAccess(), PD) == AR_inaccessible ||
1301 CD->isDeleted()) {
1302 Diag(ELoc, diag::err_omp_required_method)
1303 << getOpenMPClauseName(OMPC_firstprivate) << 1;
1304 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1305 VarDecl::DeclarationOnly;
1306 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1307 diag::note_defined_here) << VD;
1308 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1309 continue;
1310 }
1311 MarkFunctionReferenced(ELoc, CD);
1312 DiagnoseUseOfDecl(CD, ELoc);
1313
1314 CXXDestructorDecl *DD = RD->getDestructor();
1315 if (DD) {
1316 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1317 DD->isDeleted()) {
1318 Diag(ELoc, diag::err_omp_required_method)
1319 << getOpenMPClauseName(OMPC_firstprivate) << 4;
1320 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1321 VarDecl::DeclarationOnly;
1322 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1323 diag::note_defined_here) << VD;
1324 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1325 continue;
1326 }
1327 MarkFunctionReferenced(ELoc, DD);
1328 DiagnoseUseOfDecl(DD, ELoc);
1329 }
1330 }
1331
1332 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1333 // variable and it was checked already.
1334 if (StartLoc.isValid() && EndLoc.isValid()) {
1335 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1336 Type = Type.getNonReferenceType().getCanonicalType();
1337 bool IsConstant = Type.isConstant(Context);
1338 Type = Context.getBaseElementType(Type);
1339 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1340 // A list item that specifies a given variable may not appear in more
1341 // than one clause on the same directive, except that a variable may be
1342 // specified in both firstprivate and lastprivate clauses.
1343 // TODO: add processing for lastprivate.
1344 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1345 DVar.RefExpr) {
1346 Diag(ELoc, diag::err_omp_wrong_dsa)
1347 << getOpenMPClauseName(DVar.CKind)
1348 << getOpenMPClauseName(OMPC_firstprivate);
1349 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1350 << getOpenMPClauseName(DVar.CKind);
1351 continue;
1352 }
1353
1354 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1355 // in a Construct]
1356 // Variables with the predetermined data-sharing attributes may not be
1357 // listed in data-sharing attributes clauses, except for the cases
1358 // listed below. For these exceptions only, listing a predetermined
1359 // variable in a data-sharing attribute clause is allowed and overrides
1360 // the variable's predetermined data-sharing attributes.
1361 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1362 // in a Construct, C/C++, p.2]
1363 // Variables with const-qualified type having no mutable member may be
1364 // listed in a firstprivate clause, even if they are static data members.
1365 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1366 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1367 Diag(ELoc, diag::err_omp_wrong_dsa)
1368 << getOpenMPClauseName(DVar.CKind)
1369 << getOpenMPClauseName(OMPC_firstprivate);
1370 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1371 << getOpenMPClauseName(DVar.CKind);
1372 continue;
1373 }
1374
1375 // OpenMP [2.9.3.4, Restrictions, p.2]
1376 // A list item that is private within a parallel region must not appear
1377 // in a firstprivate clause on a worksharing construct if any of the
1378 // worksharing regions arising from the worksharing construct ever bind
1379 // to any of the parallel regions arising from the parallel construct.
1380 // OpenMP [2.9.3.4, Restrictions, p.3]
1381 // A list item that appears in a reduction clause of a parallel construct
1382 // must not appear in a firstprivate clause on a worksharing or task
1383 // construct if any of the worksharing or task regions arising from the
1384 // worksharing or task construct ever bind to any of the parallel regions
1385 // arising from the parallel construct.
1386 // OpenMP [2.9.3.4, Restrictions, p.4]
1387 // A list item that appears in a reduction clause in worksharing
1388 // construct must not appear in a firstprivate clause in a task construct
1389 // encountered during execution of any of the worksharing regions arising
1390 // from the worksharing construct.
1391 // TODO:
1392 }
1393
1394 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1395 Vars.push_back(DE);
1396 }
1397
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001398 if (Vars.empty()) return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001399
1400 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1401 Vars);
1402}
1403
Alexey Bataev758e55e2013-09-06 18:03:48 +00001404OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1405 SourceLocation StartLoc,
1406 SourceLocation LParenLoc,
1407 SourceLocation EndLoc) {
1408 SmallVector<Expr *, 8> Vars;
1409 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1410 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001411 assert(*I && "NULL expr in OpenMP shared clause.");
1412 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001413 // It will be analyzed later.
1414 Vars.push_back(*I);
1415 continue;
1416 }
1417
1418 SourceLocation ELoc = (*I)->getExprLoc();
1419 // OpenMP [2.1, C/C++]
1420 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001421 // OpenMP [2.14.3.2, Restrictions, p.1]
1422 // A variable that is part of another variable (as an array or structure
1423 // element) cannot appear in a shared unless it is a static data member
1424 // of a C++ class.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001425 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1426 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1427 Diag(ELoc, diag::err_omp_expected_var_name)
1428 << (*I)->getSourceRange();
1429 continue;
1430 }
1431 Decl *D = DE->getDecl();
1432 VarDecl *VD = cast<VarDecl>(D);
1433
1434 QualType Type = VD->getType();
1435 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1436 // It will be analyzed later.
1437 Vars.push_back(DE);
1438 continue;
1439 }
1440
1441 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1442 // in a Construct]
1443 // Variables with the predetermined data-sharing attributes may not be
1444 // listed in data-sharing attributes clauses, except for the cases
1445 // listed below. For these exceptions only, listing a predetermined
1446 // variable in a data-sharing attribute clause is allowed and overrides
1447 // the variable's predetermined data-sharing attributes.
1448 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1449 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1450 Diag(ELoc, diag::err_omp_wrong_dsa)
1451 << getOpenMPClauseName(DVar.CKind)
1452 << getOpenMPClauseName(OMPC_shared);
1453 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1454 << getOpenMPClauseName(DVar.CKind);
1455 continue;
1456 }
1457
1458 DSAStack->addDSA(VD, DE, OMPC_shared);
1459 Vars.push_back(DE);
1460 }
1461
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001462 if (Vars.empty()) return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001463
1464 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1465}
1466
Alexander Musman8dba6642014-04-22 13:09:42 +00001467OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1468 SourceLocation StartLoc,
1469 SourceLocation LParenLoc,
1470 SourceLocation ColonLoc,
1471 SourceLocation EndLoc) {
1472 SmallVector<Expr *, 8> Vars;
1473 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1474 I != E; ++I) {
1475 assert(*I && "NULL expr in OpenMP linear clause.");
1476 if (isa<DependentScopeDeclRefExpr>(*I)) {
1477 // It will be analyzed later.
1478 Vars.push_back(*I);
1479 continue;
1480 }
1481
1482 // OpenMP [2.14.3.7, linear clause]
1483 // A list item that appears in a linear clause is subject to the private
1484 // clause semantics described in Section 2.14.3.3 on page 159 except as
1485 // noted. In addition, the value of the new list item on each iteration
1486 // of the associated loop(s) corresponds to the value of the original
1487 // list item before entering the construct plus the logical number of
1488 // the iteration times linear-step.
1489
1490 SourceLocation ELoc = (*I)->getExprLoc();
1491 // OpenMP [2.1, C/C++]
1492 // A list item is a variable name.
1493 // OpenMP [2.14.3.3, Restrictions, p.1]
1494 // A variable that is part of another variable (as an array or
1495 // structure element) cannot appear in a private clause.
1496 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1497 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1498 Diag(ELoc, diag::err_omp_expected_var_name) << (*I)->getSourceRange();
1499 continue;
1500 }
1501
1502 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1503
1504 // OpenMP [2.14.3.7, linear clause]
1505 // A list-item cannot appear in more than one linear clause.
1506 // A list-item that appears in a linear clause cannot appear in any
1507 // other data-sharing attribute clause.
1508 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1509 if (DVar.RefExpr) {
1510 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1511 << getOpenMPClauseName(OMPC_linear);
1512 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1513 << getOpenMPClauseName(DVar.CKind);
1514 continue;
1515 }
1516
1517 QualType QType = VD->getType();
1518 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1519 // It will be analyzed later.
1520 Vars.push_back(DE);
1521 continue;
1522 }
1523
1524 // A variable must not have an incomplete type or a reference type.
1525 if (RequireCompleteType(ELoc, QType,
1526 diag::err_omp_linear_incomplete_type)) {
1527 continue;
1528 }
1529 if (QType->isReferenceType()) {
1530 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1531 << getOpenMPClauseName(OMPC_linear) << QType;
1532 bool IsDecl =
1533 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1534 Diag(VD->getLocation(),
1535 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1536 << VD;
1537 continue;
1538 }
1539
1540 // A list item must not be const-qualified.
1541 if (QType.isConstant(Context)) {
1542 Diag(ELoc, diag::err_omp_const_variable)
1543 << getOpenMPClauseName(OMPC_linear);
1544 bool IsDecl =
1545 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1546 Diag(VD->getLocation(),
1547 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1548 << VD;
1549 continue;
1550 }
1551
1552 // A list item must be of integral or pointer type.
1553 QType = QType.getUnqualifiedType().getCanonicalType();
1554 const Type *Ty = QType.getTypePtrOrNull();
1555 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1556 !Ty->isPointerType())) {
1557 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1558 bool IsDecl =
1559 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1560 Diag(VD->getLocation(),
1561 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1562 << VD;
1563 continue;
1564 }
1565
1566 DSAStack->addDSA(VD, DE, OMPC_linear);
1567 Vars.push_back(DE);
1568 }
1569
1570 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001571 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001572
1573 Expr *StepExpr = Step;
1574 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1575 !Step->isInstantiationDependent() &&
1576 !Step->containsUnexpandedParameterPack()) {
1577 SourceLocation StepLoc = Step->getLocStart();
1578 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1579 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001580 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001581 StepExpr = Val.take();
1582
1583 // Warn about zero linear step (it would be probably better specified as
1584 // making corresponding variables 'const').
1585 llvm::APSInt Result;
1586 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1587 !Result.isNegative() && !Result.isStrictlyPositive())
1588 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1589 << (Vars.size() > 1);
1590 }
1591
1592 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1593 Vars, StepExpr);
1594}
1595
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001596OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1597 SourceLocation StartLoc,
1598 SourceLocation LParenLoc,
1599 SourceLocation EndLoc) {
1600 SmallVector<Expr *, 8> Vars;
1601 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1602 I != E; ++I) {
1603 assert(*I && "NULL expr in OpenMP copyin clause.");
1604 if (isa<DependentScopeDeclRefExpr>(*I)) {
1605 // It will be analyzed later.
1606 Vars.push_back(*I);
1607 continue;
1608 }
1609
1610 SourceLocation ELoc = (*I)->getExprLoc();
1611 // OpenMP [2.1, C/C++]
1612 // A list item is a variable name.
1613 // OpenMP [2.14.4.1, Restrictions, p.1]
1614 // A list item that appears in a copyin clause must be threadprivate.
1615 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1616 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1617 Diag(ELoc, diag::err_omp_expected_var_name)
1618 << (*I)->getSourceRange();
1619 continue;
1620 }
1621
1622 Decl *D = DE->getDecl();
1623 VarDecl *VD = cast<VarDecl>(D);
1624
1625 QualType Type = VD->getType();
1626 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1627 // It will be analyzed later.
1628 Vars.push_back(DE);
1629 continue;
1630 }
1631
1632 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1633 // A list item that appears in a copyin clause must be threadprivate.
1634 if (!DSAStack->isThreadPrivate(VD)) {
1635 Diag(ELoc, diag::err_omp_required_access)
1636 << getOpenMPClauseName(OMPC_copyin)
1637 << getOpenMPDirectiveName(OMPD_threadprivate);
1638 continue;
1639 }
1640
1641 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1642 // A variable of class type (or array thereof) that appears in a
1643 // copyin clause requires an accesible, unambiguous copy assignment
1644 // operator for the class type.
1645 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001646 CXXRecordDecl *RD =
1647 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001648 if (RD) {
1649 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1650 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
1651 if (!MD ||
1652 CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
1653 MD->isDeleted()) {
1654 Diag(ELoc, diag::err_omp_required_method)
1655 << getOpenMPClauseName(OMPC_copyin) << 2;
1656 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1657 VarDecl::DeclarationOnly;
1658 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1659 diag::note_defined_here) << VD;
1660 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1661 continue;
1662 }
1663 MarkFunctionReferenced(ELoc, MD);
1664 DiagnoseUseOfDecl(MD, ELoc);
1665 }
1666
1667 DSAStack->addDSA(VD, DE, OMPC_copyin);
1668 Vars.push_back(DE);
1669 }
1670
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001671 if (Vars.empty()) return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001672
1673 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1674}
1675
Alexey Bataev758e55e2013-09-06 18:03:48 +00001676#undef DSAStack