blob: 39ec6327357eb4444e5daf320c08b3f6debda834 [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;
Alexander Musman8bd31e62014-05-27 15:12:19 +0000812 case OMPC_collapse:
813 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
814 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000815 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000816 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000817 case OMPC_private:
818 case OMPC_firstprivate:
819 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000820 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000821 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000822 case OMPC_threadprivate:
823 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000824 llvm_unreachable("Clause is not allowed.");
825 }
826 return Res;
827}
828
829OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition,
830 SourceLocation StartLoc,
831 SourceLocation LParenLoc,
832 SourceLocation EndLoc) {
833 Expr *ValExpr = Condition;
834 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
835 !Condition->isInstantiationDependent() &&
836 !Condition->containsUnexpandedParameterPack()) {
837 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
838 Condition->getExprLoc(),
839 Condition);
840 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000841 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000842
843 ValExpr = Val.take();
844 }
845
846 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
847}
848
Alexey Bataev568a8332014-03-06 06:15:19 +0000849ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
850 Expr *Op) {
851 if (!Op)
852 return ExprError();
853
854 class IntConvertDiagnoser : public ICEConvertDiagnoser {
855 public:
856 IntConvertDiagnoser()
857 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/false,
858 false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000859 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
860 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000861 return S.Diag(Loc, diag::err_omp_not_integral) << T;
862 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000863 SemaDiagnosticBuilder diagnoseIncomplete(
864 Sema &S, SourceLocation Loc, QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000865 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
866 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000867 SemaDiagnosticBuilder diagnoseExplicitConv(
868 Sema &S, SourceLocation Loc, QualType T, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000869 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
870 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000871 SemaDiagnosticBuilder noteExplicitConv(
872 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000873 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
874 << ConvTy->isEnumeralType() << ConvTy;
875 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000876 SemaDiagnosticBuilder diagnoseAmbiguous(
877 Sema &S, SourceLocation Loc, QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000878 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
879 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000880 SemaDiagnosticBuilder noteAmbiguous(
881 Sema &S, CXXConversionDecl *Conv, QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000882 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
883 << ConvTy->isEnumeralType() << ConvTy;
884 }
Craig Toppere14c0f82014-03-12 04:55:44 +0000885 SemaDiagnosticBuilder diagnoseConversion(
Alexey Bataev4ca40ed2014-05-12 04:23:46 +0000886 Sema &, SourceLocation, QualType, QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000887 llvm_unreachable("conversion functions are permitted");
888 }
889 } ConvertDiagnoser;
890 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
891}
892
893OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
894 SourceLocation StartLoc,
895 SourceLocation LParenLoc,
896 SourceLocation EndLoc) {
897 Expr *ValExpr = NumThreads;
898 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
899 !NumThreads->isInstantiationDependent() &&
900 !NumThreads->containsUnexpandedParameterPack()) {
901 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
902 ExprResult Val =
903 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
904 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000905 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000906
907 ValExpr = Val.take();
908
909 // OpenMP [2.5, Restrictions]
910 // The num_threads expression must evaluate to a positive integer value.
911 llvm::APSInt Result;
912 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
913 Result.isSigned() && !Result.isStrictlyPositive()) {
914 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
915 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000916 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000917 }
918 }
919
920 return new (Context) OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc,
921 EndLoc);
922}
923
Alexey Bataev62c87d22014-03-21 04:51:18 +0000924ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
925 OpenMPClauseKind CKind) {
926 if (!E)
927 return ExprError();
928 if (E->isValueDependent() || E->isTypeDependent() ||
929 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
930 return Owned(E);
931 llvm::APSInt Result;
932 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
933 if (ICE.isInvalid())
934 return ExprError();
935 if (!Result.isStrictlyPositive()) {
936 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
937 << getOpenMPClauseName(CKind) << E->getSourceRange();
938 return ExprError();
939 }
940 return ICE;
941}
942
943OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
944 SourceLocation LParenLoc,
945 SourceLocation EndLoc) {
946 // OpenMP [2.8.1, simd construct, Description]
947 // The parameter of the safelen clause must be a constant
948 // positive integer expression.
949 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
950 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000951 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000952 return new (Context)
953 OMPSafelenClause(Safelen.take(), StartLoc, LParenLoc, EndLoc);
954}
955
Alexander Musman8bd31e62014-05-27 15:12:19 +0000956OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *Num, SourceLocation StartLoc,
957 SourceLocation LParenLoc,
958 SourceLocation EndLoc) {
959 // OpenMP [2.8.1, simd construct, Description]
960 // The parameter of the collapse clause must be a constant
961 // positive integer expression.
962 ExprResult NumForLoops =
963 VerifyPositiveIntegerConstantInClause(Num, OMPC_collapse);
964 if (NumForLoops.isInvalid())
965 return nullptr;
966 return new (Context)
967 OMPCollapseClause(NumForLoops.take(), StartLoc, LParenLoc, EndLoc);
968}
969
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000970OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
971 unsigned Argument,
972 SourceLocation ArgumentLoc,
973 SourceLocation StartLoc,
974 SourceLocation LParenLoc,
975 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000976 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000977 switch (Kind) {
978 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000979 Res =
980 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
981 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000982 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000983 case OMPC_proc_bind:
984 Res =
985 ActOnOpenMPProcBindClause(static_cast<OpenMPProcBindClauseKind>(Argument),
986 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
987 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000988 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +0000989 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +0000990 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +0000991 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000992 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000993 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +0000994 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000995 case OMPC_linear:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000996 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000997 case OMPC_threadprivate:
998 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000999 llvm_unreachable("Clause is not allowed.");
1000 }
1001 return Res;
1002}
1003
1004OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1005 SourceLocation KindKwLoc,
1006 SourceLocation StartLoc,
1007 SourceLocation LParenLoc,
1008 SourceLocation EndLoc) {
1009 if (Kind == OMPC_DEFAULT_unknown) {
1010 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001011 static_assert(OMPC_DEFAULT_unknown > 0,
1012 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001013 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001014 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001015 Values += "'";
1016 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1017 Values += "'";
1018 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001019 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001020 Values += " or ";
1021 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001022 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001023 break;
1024 default:
1025 Values += Sep;
1026 break;
1027 }
1028 }
1029 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
1030 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001031 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001032 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001033 switch (Kind) {
1034 case OMPC_DEFAULT_none:
1035 DSAStack->setDefaultDSANone();
1036 break;
1037 case OMPC_DEFAULT_shared:
1038 DSAStack->setDefaultDSAShared();
1039 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001040 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001041 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001042 break;
1043 }
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001044 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
1045 EndLoc);
1046}
1047
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001048OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1049 SourceLocation KindKwLoc,
1050 SourceLocation StartLoc,
1051 SourceLocation LParenLoc,
1052 SourceLocation EndLoc) {
1053 if (Kind == OMPC_PROC_BIND_unknown) {
1054 std::string Values;
1055 std::string Sep(", ");
1056 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1057 Values += "'";
1058 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1059 Values += "'";
1060 switch (i) {
1061 case OMPC_PROC_BIND_unknown - 2:
1062 Values += " or ";
1063 break;
1064 case OMPC_PROC_BIND_unknown - 1:
1065 break;
1066 default:
1067 Values += Sep;
1068 break;
1069 }
1070 }
1071 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
1072 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001073 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001074 }
1075 return new (Context) OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc,
1076 EndLoc);
1077}
1078
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001079OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
1080 ArrayRef<Expr *> VarList,
Alexander Musman8dba6642014-04-22 13:09:42 +00001081 Expr *TailExpr,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001082 SourceLocation StartLoc,
1083 SourceLocation LParenLoc,
Alexander Musman8dba6642014-04-22 13:09:42 +00001084 SourceLocation ColonLoc,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001085 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001086 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001087 switch (Kind) {
1088 case OMPC_private:
1089 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1090 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001091 case OMPC_firstprivate:
1092 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1093 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001094 case OMPC_shared:
1095 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1096 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001097 case OMPC_linear:
1098 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1099 ColonLoc, EndLoc);
1100 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001101 case OMPC_copyin:
1102 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1103 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001104 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001105 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001106 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001107 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001108 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001109 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001110 case OMPC_threadprivate:
1111 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001112 llvm_unreachable("Clause is not allowed.");
1113 }
1114 return Res;
1115}
1116
1117OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1118 SourceLocation StartLoc,
1119 SourceLocation LParenLoc,
1120 SourceLocation EndLoc) {
1121 SmallVector<Expr *, 8> Vars;
1122 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1123 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001124 assert(*I && "NULL expr in OpenMP private clause.");
1125 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001126 // It will be analyzed later.
1127 Vars.push_back(*I);
1128 continue;
1129 }
1130
1131 SourceLocation ELoc = (*I)->getExprLoc();
1132 // OpenMP [2.1, C/C++]
1133 // A list item is a variable name.
1134 // OpenMP [2.9.3.3, Restrictions, p.1]
1135 // A variable that is part of another variable (as an array or
1136 // structure element) cannot appear in a private clause.
1137 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1138 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1139 Diag(ELoc, diag::err_omp_expected_var_name)
1140 << (*I)->getSourceRange();
1141 continue;
1142 }
1143 Decl *D = DE->getDecl();
1144 VarDecl *VD = cast<VarDecl>(D);
1145
1146 QualType Type = VD->getType();
1147 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1148 // It will be analyzed later.
1149 Vars.push_back(DE);
1150 continue;
1151 }
1152
1153 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1154 // A variable that appears in a private clause must not have an incomplete
1155 // type or a reference type.
1156 if (RequireCompleteType(ELoc, Type,
1157 diag::err_omp_private_incomplete_type)) {
1158 continue;
1159 }
1160 if (Type->isReferenceType()) {
1161 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1162 << getOpenMPClauseName(OMPC_private) << Type;
1163 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1164 VarDecl::DeclarationOnly;
1165 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1166 diag::note_defined_here) << VD;
1167 continue;
1168 }
1169
1170 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1171 // A variable of class type (or array thereof) that appears in a private
1172 // clause requires an accesible, unambiguous default constructor for the
1173 // class type.
1174 while (Type.getNonReferenceType()->isArrayType()) {
1175 Type = cast<ArrayType>(
1176 Type.getNonReferenceType().getTypePtr())->getElementType();
1177 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001178 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1179 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1180 : nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001181 if (RD) {
1182 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1183 PartialDiagnostic PD =
1184 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1185 if (!CD ||
1186 CheckConstructorAccess(ELoc, CD,
1187 InitializedEntity::InitializeTemporary(Type),
1188 CD->getAccess(), PD) == AR_inaccessible ||
1189 CD->isDeleted()) {
1190 Diag(ELoc, diag::err_omp_required_method)
1191 << getOpenMPClauseName(OMPC_private) << 0;
1192 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1193 VarDecl::DeclarationOnly;
1194 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1195 diag::note_defined_here) << VD;
1196 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1197 continue;
1198 }
1199 MarkFunctionReferenced(ELoc, CD);
1200 DiagnoseUseOfDecl(CD, ELoc);
1201
1202 CXXDestructorDecl *DD = RD->getDestructor();
1203 if (DD) {
1204 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1205 DD->isDeleted()) {
1206 Diag(ELoc, diag::err_omp_required_method)
1207 << getOpenMPClauseName(OMPC_private) << 4;
1208 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1209 VarDecl::DeclarationOnly;
1210 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1211 diag::note_defined_here) << VD;
1212 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1213 continue;
1214 }
1215 MarkFunctionReferenced(ELoc, DD);
1216 DiagnoseUseOfDecl(DD, ELoc);
1217 }
1218 }
1219
Alexey Bataev758e55e2013-09-06 18:03:48 +00001220 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1221 // in a Construct]
1222 // Variables with the predetermined data-sharing attributes may not be
1223 // listed in data-sharing attributes clauses, except for the cases
1224 // listed below. For these exceptions only, listing a predetermined
1225 // variable in a data-sharing attribute clause is allowed and overrides
1226 // the variable's predetermined data-sharing attributes.
1227 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1228 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
1229 Diag(ELoc, diag::err_omp_wrong_dsa)
1230 << getOpenMPClauseName(DVar.CKind)
1231 << getOpenMPClauseName(OMPC_private);
1232 if (DVar.RefExpr) {
1233 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1234 << getOpenMPClauseName(DVar.CKind);
1235 } else {
1236 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1237 << getOpenMPClauseName(DVar.CKind);
1238 }
1239 continue;
1240 }
1241
1242 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001243 Vars.push_back(DE);
1244 }
1245
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001246 if (Vars.empty()) return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001247
1248 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1249}
1250
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001251OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1252 SourceLocation StartLoc,
1253 SourceLocation LParenLoc,
1254 SourceLocation EndLoc) {
1255 SmallVector<Expr *, 8> Vars;
1256 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1257 I != E; ++I) {
1258 assert(*I && "NULL expr in OpenMP firstprivate clause.");
1259 if (isa<DependentScopeDeclRefExpr>(*I)) {
1260 // It will be analyzed later.
1261 Vars.push_back(*I);
1262 continue;
1263 }
1264
1265 SourceLocation ELoc = (*I)->getExprLoc();
1266 // OpenMP [2.1, C/C++]
1267 // A list item is a variable name.
1268 // OpenMP [2.9.3.3, Restrictions, p.1]
1269 // A variable that is part of another variable (as an array or
1270 // structure element) cannot appear in a private clause.
1271 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
1272 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1273 Diag(ELoc, diag::err_omp_expected_var_name)
1274 << (*I)->getSourceRange();
1275 continue;
1276 }
1277 Decl *D = DE->getDecl();
1278 VarDecl *VD = cast<VarDecl>(D);
1279
1280 QualType Type = VD->getType();
1281 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1282 // It will be analyzed later.
1283 Vars.push_back(DE);
1284 continue;
1285 }
1286
1287 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1288 // A variable that appears in a private clause must not have an incomplete
1289 // type or a reference type.
1290 if (RequireCompleteType(ELoc, Type,
1291 diag::err_omp_firstprivate_incomplete_type)) {
1292 continue;
1293 }
1294 if (Type->isReferenceType()) {
1295 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1296 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1297 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1298 VarDecl::DeclarationOnly;
1299 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1300 diag::note_defined_here) << VD;
1301 continue;
1302 }
1303
1304 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1305 // A variable of class type (or array thereof) that appears in a private
1306 // clause requires an accesible, unambiguous copy constructor for the
1307 // class type.
1308 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001309 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1310 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1311 : nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001312 if (RD) {
1313 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1314 PartialDiagnostic PD =
1315 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1316 if (!CD ||
1317 CheckConstructorAccess(ELoc, CD,
1318 InitializedEntity::InitializeTemporary(Type),
1319 CD->getAccess(), PD) == AR_inaccessible ||
1320 CD->isDeleted()) {
1321 Diag(ELoc, diag::err_omp_required_method)
1322 << getOpenMPClauseName(OMPC_firstprivate) << 1;
1323 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1324 VarDecl::DeclarationOnly;
1325 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1326 diag::note_defined_here) << VD;
1327 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1328 continue;
1329 }
1330 MarkFunctionReferenced(ELoc, CD);
1331 DiagnoseUseOfDecl(CD, ELoc);
1332
1333 CXXDestructorDecl *DD = RD->getDestructor();
1334 if (DD) {
1335 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1336 DD->isDeleted()) {
1337 Diag(ELoc, diag::err_omp_required_method)
1338 << getOpenMPClauseName(OMPC_firstprivate) << 4;
1339 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1340 VarDecl::DeclarationOnly;
1341 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1342 diag::note_defined_here) << VD;
1343 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1344 continue;
1345 }
1346 MarkFunctionReferenced(ELoc, DD);
1347 DiagnoseUseOfDecl(DD, ELoc);
1348 }
1349 }
1350
1351 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1352 // variable and it was checked already.
1353 if (StartLoc.isValid() && EndLoc.isValid()) {
1354 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1355 Type = Type.getNonReferenceType().getCanonicalType();
1356 bool IsConstant = Type.isConstant(Context);
1357 Type = Context.getBaseElementType(Type);
1358 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1359 // A list item that specifies a given variable may not appear in more
1360 // than one clause on the same directive, except that a variable may be
1361 // specified in both firstprivate and lastprivate clauses.
1362 // TODO: add processing for lastprivate.
1363 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1364 DVar.RefExpr) {
1365 Diag(ELoc, diag::err_omp_wrong_dsa)
1366 << getOpenMPClauseName(DVar.CKind)
1367 << getOpenMPClauseName(OMPC_firstprivate);
1368 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1369 << getOpenMPClauseName(DVar.CKind);
1370 continue;
1371 }
1372
1373 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1374 // in a Construct]
1375 // Variables with the predetermined data-sharing attributes may not be
1376 // listed in data-sharing attributes clauses, except for the cases
1377 // listed below. For these exceptions only, listing a predetermined
1378 // variable in a data-sharing attribute clause is allowed and overrides
1379 // the variable's predetermined data-sharing attributes.
1380 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1381 // in a Construct, C/C++, p.2]
1382 // Variables with const-qualified type having no mutable member may be
1383 // listed in a firstprivate clause, even if they are static data members.
1384 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1385 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1386 Diag(ELoc, diag::err_omp_wrong_dsa)
1387 << getOpenMPClauseName(DVar.CKind)
1388 << getOpenMPClauseName(OMPC_firstprivate);
1389 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1390 << getOpenMPClauseName(DVar.CKind);
1391 continue;
1392 }
1393
1394 // OpenMP [2.9.3.4, Restrictions, p.2]
1395 // A list item that is private within a parallel region must not appear
1396 // in a firstprivate clause on a worksharing construct if any of the
1397 // worksharing regions arising from the worksharing construct ever bind
1398 // to any of the parallel regions arising from the parallel construct.
1399 // OpenMP [2.9.3.4, Restrictions, p.3]
1400 // A list item that appears in a reduction clause of a parallel construct
1401 // must not appear in a firstprivate clause on a worksharing or task
1402 // construct if any of the worksharing or task regions arising from the
1403 // worksharing or task construct ever bind to any of the parallel regions
1404 // arising from the parallel construct.
1405 // OpenMP [2.9.3.4, Restrictions, p.4]
1406 // A list item that appears in a reduction clause in worksharing
1407 // construct must not appear in a firstprivate clause in a task construct
1408 // encountered during execution of any of the worksharing regions arising
1409 // from the worksharing construct.
1410 // TODO:
1411 }
1412
1413 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1414 Vars.push_back(DE);
1415 }
1416
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001417 if (Vars.empty()) return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001418
1419 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1420 Vars);
1421}
1422
Alexey Bataev758e55e2013-09-06 18:03:48 +00001423OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1424 SourceLocation StartLoc,
1425 SourceLocation LParenLoc,
1426 SourceLocation EndLoc) {
1427 SmallVector<Expr *, 8> Vars;
1428 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1429 I != E; ++I) {
Alexey Bataev756c1962013-09-24 03:17:45 +00001430 assert(*I && "NULL expr in OpenMP shared clause.");
1431 if (isa<DependentScopeDeclRefExpr>(*I)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001432 // It will be analyzed later.
1433 Vars.push_back(*I);
1434 continue;
1435 }
1436
1437 SourceLocation ELoc = (*I)->getExprLoc();
1438 // OpenMP [2.1, C/C++]
1439 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001440 // OpenMP [2.14.3.2, Restrictions, p.1]
1441 // A variable that is part of another variable (as an array or structure
1442 // element) cannot appear in a shared unless it is a static data member
1443 // of a C++ class.
Alexey Bataev758e55e2013-09-06 18:03:48 +00001444 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1445 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1446 Diag(ELoc, diag::err_omp_expected_var_name)
1447 << (*I)->getSourceRange();
1448 continue;
1449 }
1450 Decl *D = DE->getDecl();
1451 VarDecl *VD = cast<VarDecl>(D);
1452
1453 QualType Type = VD->getType();
1454 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1455 // It will be analyzed later.
1456 Vars.push_back(DE);
1457 continue;
1458 }
1459
1460 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1461 // in a Construct]
1462 // Variables with the predetermined data-sharing attributes may not be
1463 // listed in data-sharing attributes clauses, except for the cases
1464 // listed below. For these exceptions only, listing a predetermined
1465 // variable in a data-sharing attribute clause is allowed and overrides
1466 // the variable's predetermined data-sharing attributes.
1467 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1468 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1469 Diag(ELoc, diag::err_omp_wrong_dsa)
1470 << getOpenMPClauseName(DVar.CKind)
1471 << getOpenMPClauseName(OMPC_shared);
1472 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1473 << getOpenMPClauseName(DVar.CKind);
1474 continue;
1475 }
1476
1477 DSAStack->addDSA(VD, DE, OMPC_shared);
1478 Vars.push_back(DE);
1479 }
1480
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001481 if (Vars.empty()) return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001482
1483 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1484}
1485
Alexander Musman8dba6642014-04-22 13:09:42 +00001486OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1487 SourceLocation StartLoc,
1488 SourceLocation LParenLoc,
1489 SourceLocation ColonLoc,
1490 SourceLocation EndLoc) {
1491 SmallVector<Expr *, 8> Vars;
1492 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1493 I != E; ++I) {
1494 assert(*I && "NULL expr in OpenMP linear clause.");
1495 if (isa<DependentScopeDeclRefExpr>(*I)) {
1496 // It will be analyzed later.
1497 Vars.push_back(*I);
1498 continue;
1499 }
1500
1501 // OpenMP [2.14.3.7, linear clause]
1502 // A list item that appears in a linear clause is subject to the private
1503 // clause semantics described in Section 2.14.3.3 on page 159 except as
1504 // noted. In addition, the value of the new list item on each iteration
1505 // of the associated loop(s) corresponds to the value of the original
1506 // list item before entering the construct plus the logical number of
1507 // the iteration times linear-step.
1508
1509 SourceLocation ELoc = (*I)->getExprLoc();
1510 // OpenMP [2.1, C/C++]
1511 // A list item is a variable name.
1512 // OpenMP [2.14.3.3, Restrictions, p.1]
1513 // A variable that is part of another variable (as an array or
1514 // structure element) cannot appear in a private clause.
1515 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1516 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1517 Diag(ELoc, diag::err_omp_expected_var_name) << (*I)->getSourceRange();
1518 continue;
1519 }
1520
1521 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1522
1523 // OpenMP [2.14.3.7, linear clause]
1524 // A list-item cannot appear in more than one linear clause.
1525 // A list-item that appears in a linear clause cannot appear in any
1526 // other data-sharing attribute clause.
1527 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1528 if (DVar.RefExpr) {
1529 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1530 << getOpenMPClauseName(OMPC_linear);
1531 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1532 << getOpenMPClauseName(DVar.CKind);
1533 continue;
1534 }
1535
1536 QualType QType = VD->getType();
1537 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1538 // It will be analyzed later.
1539 Vars.push_back(DE);
1540 continue;
1541 }
1542
1543 // A variable must not have an incomplete type or a reference type.
1544 if (RequireCompleteType(ELoc, QType,
1545 diag::err_omp_linear_incomplete_type)) {
1546 continue;
1547 }
1548 if (QType->isReferenceType()) {
1549 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1550 << getOpenMPClauseName(OMPC_linear) << QType;
1551 bool IsDecl =
1552 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1553 Diag(VD->getLocation(),
1554 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1555 << VD;
1556 continue;
1557 }
1558
1559 // A list item must not be const-qualified.
1560 if (QType.isConstant(Context)) {
1561 Diag(ELoc, diag::err_omp_const_variable)
1562 << getOpenMPClauseName(OMPC_linear);
1563 bool IsDecl =
1564 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1565 Diag(VD->getLocation(),
1566 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1567 << VD;
1568 continue;
1569 }
1570
1571 // A list item must be of integral or pointer type.
1572 QType = QType.getUnqualifiedType().getCanonicalType();
1573 const Type *Ty = QType.getTypePtrOrNull();
1574 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1575 !Ty->isPointerType())) {
1576 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1577 bool IsDecl =
1578 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1579 Diag(VD->getLocation(),
1580 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1581 << VD;
1582 continue;
1583 }
1584
1585 DSAStack->addDSA(VD, DE, OMPC_linear);
1586 Vars.push_back(DE);
1587 }
1588
1589 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001590 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001591
1592 Expr *StepExpr = Step;
1593 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1594 !Step->isInstantiationDependent() &&
1595 !Step->containsUnexpandedParameterPack()) {
1596 SourceLocation StepLoc = Step->getLocStart();
1597 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1598 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001599 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001600 StepExpr = Val.take();
1601
1602 // Warn about zero linear step (it would be probably better specified as
1603 // making corresponding variables 'const').
1604 llvm::APSInt Result;
1605 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1606 !Result.isNegative() && !Result.isStrictlyPositive())
1607 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1608 << (Vars.size() > 1);
1609 }
1610
1611 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1612 Vars, StepExpr);
1613}
1614
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001615OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1616 SourceLocation StartLoc,
1617 SourceLocation LParenLoc,
1618 SourceLocation EndLoc) {
1619 SmallVector<Expr *, 8> Vars;
1620 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1621 I != E; ++I) {
1622 assert(*I && "NULL expr in OpenMP copyin clause.");
1623 if (isa<DependentScopeDeclRefExpr>(*I)) {
1624 // It will be analyzed later.
1625 Vars.push_back(*I);
1626 continue;
1627 }
1628
1629 SourceLocation ELoc = (*I)->getExprLoc();
1630 // OpenMP [2.1, C/C++]
1631 // A list item is a variable name.
1632 // OpenMP [2.14.4.1, Restrictions, p.1]
1633 // A list item that appears in a copyin clause must be threadprivate.
1634 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1635 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1636 Diag(ELoc, diag::err_omp_expected_var_name)
1637 << (*I)->getSourceRange();
1638 continue;
1639 }
1640
1641 Decl *D = DE->getDecl();
1642 VarDecl *VD = cast<VarDecl>(D);
1643
1644 QualType Type = VD->getType();
1645 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1646 // It will be analyzed later.
1647 Vars.push_back(DE);
1648 continue;
1649 }
1650
1651 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1652 // A list item that appears in a copyin clause must be threadprivate.
1653 if (!DSAStack->isThreadPrivate(VD)) {
1654 Diag(ELoc, diag::err_omp_required_access)
1655 << getOpenMPClauseName(OMPC_copyin)
1656 << getOpenMPDirectiveName(OMPD_threadprivate);
1657 continue;
1658 }
1659
1660 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1661 // A variable of class type (or array thereof) that appears in a
1662 // copyin clause requires an accesible, unambiguous copy assignment
1663 // operator for the class type.
1664 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001665 CXXRecordDecl *RD =
1666 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001667 if (RD) {
1668 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1669 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
1670 if (!MD ||
1671 CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
1672 MD->isDeleted()) {
1673 Diag(ELoc, diag::err_omp_required_method)
1674 << getOpenMPClauseName(OMPC_copyin) << 2;
1675 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1676 VarDecl::DeclarationOnly;
1677 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1678 diag::note_defined_here) << VD;
1679 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1680 continue;
1681 }
1682 MarkFunctionReferenced(ELoc, MD);
1683 DiagnoseUseOfDecl(MD, ELoc);
1684 }
1685
1686 DSAStack->addDSA(VD, DE, OMPC_copyin);
1687 Vars.push_back(DE);
1688 }
1689
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001690 if (Vars.empty()) return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001691
1692 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1693}
1694
Alexey Bataev758e55e2013-09-06 18:03:48 +00001695#undef DSAStack