blob: c78c42952cadfdefcf8420dad51c7b1076381270 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "clang/Basic/OpenMPKinds.h"
23#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000025#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000028#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000029using namespace clang;
30
Alexey Bataev758e55e2013-09-06 18:03:48 +000031//===----------------------------------------------------------------------===//
32// Stack of data-sharing attributes for variables
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// \brief Default data sharing attributes, which can be applied to directive.
37enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000038 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
39 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
40 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000041};
42
43/// \brief Stack for tracking declarations used in OpenMP directives and
44/// clauses and their data-sharing attributes.
45class DSAStackTy {
46public:
47 struct DSAVarData {
48 OpenMPDirectiveKind DKind;
49 OpenMPClauseKind CKind;
50 DeclRefExpr *RefExpr;
Alexander Musmancb7f9c42014-05-15 13:04:49 +000051 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000052 };
Alexey Bataeved09d242014-05-28 05:53:51 +000053
Alexey Bataev758e55e2013-09-06 18:03:48 +000054private:
55 struct DSAInfo {
56 OpenMPClauseKind Attributes;
57 DeclRefExpr *RefExpr;
58 };
59 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000060 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000061
62 struct SharingMapTy {
63 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000064 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065 DefaultDataSharingAttributes DefaultAttr;
66 OpenMPDirectiveKind Directive;
67 DeclarationNameInfo DirectiveName;
68 Scope *CurScope;
Alexey Bataeved09d242014-05-28 05:53:51 +000069 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 Scope *CurScope)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000071 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
72 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope) {
73 }
Alexey Bataev758e55e2013-09-06 18:03:48 +000074 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +000075 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
76 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000077 };
78
79 typedef SmallVector<SharingMapTy, 64> StackTy;
80
81 /// \brief Stack of used declaration and their data-sharing attributes.
82 StackTy Stack;
83 Sema &Actions;
84
85 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
86
87 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +000088
89 /// \brief Checks if the variable is a local for OpenMP region.
90 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +000091
Alexey Bataev758e55e2013-09-06 18:03:48 +000092public:
Alexey Bataeved09d242014-05-28 05:53:51 +000093 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000094
95 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
96 Scope *CurScope) {
97 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
98 }
99
100 void pop() {
101 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
102 Stack.pop_back();
103 }
104
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000105 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
106 /// add it and return NULL; otherwise return previous occurence's expression
107 /// for diagnostics.
108 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
109
Alexey Bataev758e55e2013-09-06 18:03:48 +0000110 /// \brief Adds explicit data sharing attribute to the specified declaration.
111 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
112
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113 /// \brief Returns data sharing attributes from top of the stack for the
114 /// specified declaration.
115 DSAVarData getTopDSA(VarDecl *D);
116 /// \brief Returns data-sharing attributes for the specified declaration.
117 DSAVarData getImplicitDSA(VarDecl *D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000118 /// \brief Checks if the specified variables has \a CKind data-sharing
119 /// attribute in \a DKind directive.
120 DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
121 OpenMPDirectiveKind DKind = OMPD_unknown);
122
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123 /// \brief Returns currently analyzed directive.
124 OpenMPDirectiveKind getCurrentDirective() const {
125 return Stack.back().Directive;
126 }
127
128 /// \brief Set default data sharing attribute to none.
129 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
130 /// \brief Set default data sharing attribute to shared.
131 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
132
133 DefaultDataSharingAttributes getDefaultDSA() const {
134 return Stack.back().DefaultAttr;
135 }
136
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000137 /// \brief Checks if the spewcified variable is threadprivate.
138 bool isThreadPrivate(VarDecl *D) {
139 DSAVarData DVar = getTopDSA(D);
140 return (DVar.CKind == OMPC_threadprivate || DVar.CKind == OMPC_copyin);
141 }
142
143 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 Scope *getCurScope() { return Stack.back().CurScope; }
145};
Alexey Bataeved09d242014-05-28 05:53:51 +0000146} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000147
148DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
149 VarDecl *D) {
150 DSAVarData DVar;
151 if (Iter == Stack.rend() - 1) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000152 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
153 // in a region but not in construct]
154 // File-scope or namespace-scope variables referenced in called routines
155 // in the region are shared unless they appear in a threadprivate
156 // directive.
157 // TODO
158 if (!D->isFunctionOrMethodVarDecl())
159 DVar.CKind = OMPC_shared;
160
161 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
162 // in a region but not in construct]
163 // Variables with static storage duration that are declared in called
164 // routines in the region are shared.
165 if (D->hasGlobalStorage())
166 DVar.CKind = OMPC_shared;
167
Alexey Bataev758e55e2013-09-06 18:03:48 +0000168 return DVar;
169 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000170
Alexey Bataev758e55e2013-09-06 18:03:48 +0000171 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000172 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
173 // in a Construct, C/C++, predetermined, p.1]
174 // Variables with automatic storage duration that are declared in a scope
175 // inside the construct are private.
176 if (DVar.DKind != OMPD_parallel) {
177 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000178 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000179 DVar.CKind = OMPC_private;
180 return DVar;
181 }
182 }
183
Alexey Bataev758e55e2013-09-06 18:03:48 +0000184 // Explicitly specified attributes and local variables with predetermined
185 // attributes.
186 if (Iter->SharingMap.count(D)) {
187 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
188 DVar.CKind = Iter->SharingMap[D].Attributes;
189 return DVar;
190 }
191
192 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
193 // in a Construct, C/C++, implicitly determined, p.1]
194 // In a parallel or task construct, the data-sharing attributes of these
195 // variables are determined by the default clause, if present.
196 switch (Iter->DefaultAttr) {
197 case DSA_shared:
198 DVar.CKind = OMPC_shared;
199 return DVar;
200 case DSA_none:
201 return DVar;
202 case DSA_unspecified:
203 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
204 // in a Construct, implicitly determined, p.2]
205 // In a parallel construct, if no default clause is present, these
206 // variables are shared.
207 if (DVar.DKind == OMPD_parallel) {
208 DVar.CKind = OMPC_shared;
209 return DVar;
210 }
211
212 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
213 // in a Construct, implicitly determined, p.4]
214 // In a task construct, if no default clause is present, a variable that in
215 // the enclosing context is determined to be shared by all implicit tasks
216 // bound to the current team is shared.
217 // TODO
218 if (DVar.DKind == OMPD_task) {
219 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000220 for (StackTy::reverse_iterator I = std::next(Iter),
221 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000222 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000223 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
224 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225 // in a Construct, implicitly determined, p.6]
226 // In a task construct, if no default clause is present, a variable
227 // whose data-sharing attribute is not determined by the rules above is
228 // firstprivate.
229 DVarTemp = getDSA(I, D);
230 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000231 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000232 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000233 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000234 return DVar;
235 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000236 if (I->Directive == OMPD_parallel)
237 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238 }
239 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000241 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000242 return DVar;
243 }
244 }
245 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
246 // in a Construct, implicitly determined, p.3]
247 // For constructs other than task, if no default clause is present, these
248 // variables inherit their data-sharing attributes from the enclosing
249 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000250 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000251}
252
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000253DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
254 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
255 auto It = Stack.back().AlignedMap.find(D);
256 if (It == Stack.back().AlignedMap.end()) {
257 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
258 Stack.back().AlignedMap[D] = NewDE;
259 return nullptr;
260 } else {
261 assert(It->second && "Unexpected nullptr expr in the aligned map");
262 return It->second;
263 }
264 return nullptr;
265}
266
Alexey Bataev758e55e2013-09-06 18:03:48 +0000267void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
268 if (A == OMPC_threadprivate) {
269 Stack[0].SharingMap[D].Attributes = A;
270 Stack[0].SharingMap[D].RefExpr = E;
271 } else {
272 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
273 Stack.back().SharingMap[D].Attributes = A;
274 Stack.back().SharingMap[D].RefExpr = E;
275 }
276}
277
Alexey Bataeved09d242014-05-28 05:53:51 +0000278bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000279 if (Stack.size() > 2) {
280 reverse_iterator I = Iter, E = Stack.rend() - 1;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000281 Scope *TopScope = nullptr;
Fraser Cormack111023c2014-04-15 08:59:09 +0000282 while (I != E && I->Directive != OMPD_parallel) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000283 ++I;
284 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000285 if (I == E)
286 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000287 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000288 Scope *CurScope = getCurScope();
289 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000290 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000291 }
292 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000293 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000294 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000295}
296
297DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
298 DSAVarData DVar;
299
300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
301 // in a Construct, C/C++, predetermined, p.1]
302 // Variables appearing in threadprivate directives are threadprivate.
303 if (D->getTLSKind() != VarDecl::TLS_None) {
304 DVar.CKind = OMPC_threadprivate;
305 return DVar;
306 }
307 if (Stack[0].SharingMap.count(D)) {
308 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
309 DVar.CKind = OMPC_threadprivate;
310 return DVar;
311 }
312
313 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
314 // in a Construct, C/C++, predetermined, p.1]
315 // Variables with automatic storage duration that are declared in a scope
316 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000317 OpenMPDirectiveKind Kind = getCurrentDirective();
318 if (Kind != OMPD_parallel) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000319 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000320 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000321 DVar.CKind = OMPC_private;
322 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000323 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000324 }
325
326 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
327 // in a Construct, C/C++, predetermined, p.4]
328 // Static data memebers are shared.
329 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000330 // Variables with const-qualified type having no mutable member may be
331 // listed
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000332 // in a firstprivate clause, even if they are static data members.
333 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
334 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
335 return DVar;
336
Alexey Bataev758e55e2013-09-06 18:03:48 +0000337 DVar.CKind = OMPC_shared;
338 return DVar;
339 }
340
341 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
342 bool IsConstant = Type.isConstant(Actions.getASTContext());
343 while (Type->isArrayType()) {
344 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
345 Type = ElemType.getNonReferenceType().getCanonicalType();
346 }
347 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
348 // in a Construct, C/C++, predetermined, p.6]
349 // Variables with const qualified type having no mutable member are
350 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000351 CXXRecordDecl *RD =
352 Actions.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000353 if (IsConstant &&
354 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
355 // Variables with const-qualified type having no mutable member may be
356 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000357 DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
358 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
359 return DVar;
360
Alexey Bataev758e55e2013-09-06 18:03:48 +0000361 DVar.CKind = OMPC_shared;
362 return DVar;
363 }
364
365 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
366 // in a Construct, C/C++, predetermined, p.7]
367 // Variables with static storage duration that are declared in a scope
368 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000369 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000370 DVar.CKind = OMPC_shared;
371 return DVar;
372 }
373
374 // Explicitly specified attributes and local variables with predetermined
375 // attributes.
376 if (Stack.back().SharingMap.count(D)) {
377 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
378 DVar.CKind = Stack.back().SharingMap[D].Attributes;
379 }
380
381 return DVar;
382}
383
384DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000385 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386}
387
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000388DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
389 OpenMPDirectiveKind DKind) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000390 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
391 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000392 I != E; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000393 if (DKind != OMPD_unknown && DKind != I->Directive)
394 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000395 DSAVarData DVar = getDSA(I, D);
396 if (DVar.CKind == CKind)
397 return DVar;
398 }
399 return DSAVarData();
400}
401
Alexey Bataev758e55e2013-09-06 18:03:48 +0000402void Sema::InitDataSharingAttributesStack() {
403 VarDataSharingAttributesStack = new DSAStackTy(*this);
404}
405
406#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
407
Alexey Bataeved09d242014-05-28 05:53:51 +0000408void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000409
410void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
411 const DeclarationNameInfo &DirName,
412 Scope *CurScope) {
413 DSAStack->push(DKind, DirName, CurScope);
414 PushExpressionEvaluationContext(PotentiallyEvaluated);
415}
416
417void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
418 DSAStack->pop();
419 DiscardCleanupsInEvaluationContext();
420 PopExpressionEvaluationContext();
421}
422
Alexey Bataeva769e072013-03-22 06:34:35 +0000423namespace {
424
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000425class VarDeclFilterCCC : public CorrectionCandidateCallback {
426private:
427 Sema &Actions;
Alexey Bataeved09d242014-05-28 05:53:51 +0000428
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000429public:
Alexey Bataeved09d242014-05-28 05:53:51 +0000430 VarDeclFilterCCC(Sema &S) : Actions(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000431 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000432 NamedDecl *ND = Candidate.getCorrectionDecl();
433 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
434 return VD->hasGlobalStorage() &&
435 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
436 Actions.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000437 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000438 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000439 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000440};
Alexey Bataeved09d242014-05-28 05:53:51 +0000441} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000442
443ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
444 CXXScopeSpec &ScopeSpec,
445 const DeclarationNameInfo &Id) {
446 LookupResult Lookup(*this, Id, LookupOrdinaryName);
447 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
448
449 if (Lookup.isAmbiguous())
450 return ExprError();
451
452 VarDecl *VD;
453 if (!Lookup.isSingleResult()) {
454 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000455 if (TypoCorrection Corrected =
456 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
457 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000458 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000459 PDiag(Lookup.empty()
460 ? diag::err_undeclared_var_use_suggest
461 : diag::err_omp_expected_var_arg_suggest)
462 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000463 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000464 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000465 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
466 : diag::err_omp_expected_var_arg)
467 << Id.getName();
468 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000469 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000470 } else {
471 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000472 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000473 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
474 return ExprError();
475 }
476 }
477 Lookup.suppressDiagnostics();
478
479 // OpenMP [2.9.2, Syntax, C/C++]
480 // Variables must be file-scope, namespace-scope, or static block-scope.
481 if (!VD->hasGlobalStorage()) {
482 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000483 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
484 bool IsDecl =
485 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000486 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000487 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
488 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000489 return ExprError();
490 }
491
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000492 VarDecl *CanonicalVD = VD->getCanonicalDecl();
493 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000494 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
495 // A threadprivate directive for file-scope variables must appear outside
496 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000497 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
498 !getCurLexicalContext()->isTranslationUnit()) {
499 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000500 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
501 bool IsDecl =
502 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
503 Diag(VD->getLocation(),
504 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
505 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000506 return ExprError();
507 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000508 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
509 // A threadprivate directive for static class member variables must appear
510 // in the class definition, in the same scope in which the member
511 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000512 if (CanonicalVD->isStaticDataMember() &&
513 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
514 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000515 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
516 bool IsDecl =
517 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
518 Diag(VD->getLocation(),
519 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
520 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000521 return ExprError();
522 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000523 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
524 // A threadprivate directive for namespace-scope variables must appear
525 // outside any definition or declaration other than the namespace
526 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000527 if (CanonicalVD->getDeclContext()->isNamespace() &&
528 (!getCurLexicalContext()->isFileContext() ||
529 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
530 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000531 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
532 bool IsDecl =
533 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
534 Diag(VD->getLocation(),
535 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
536 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000537 return ExprError();
538 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000539 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
540 // A threadprivate directive for static block-scope variables must appear
541 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000542 if (CanonicalVD->isStaticLocal() && CurScope &&
543 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000544 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000545 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
546 bool IsDecl =
547 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
548 Diag(VD->getLocation(),
549 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
550 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000551 return ExprError();
552 }
553
554 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
555 // A threadprivate directive must lexically precede all references to any
556 // of the variables in its list.
557 if (VD->isUsed()) {
558 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000559 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000560 return ExprError();
561 }
562
563 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000564 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000565 return DE;
566}
567
Alexey Bataeved09d242014-05-28 05:53:51 +0000568Sema::DeclGroupPtrTy
569Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
570 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000571 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000572 CurContext->addDecl(D);
573 return DeclGroupPtrTy::make(DeclGroupRef(D));
574 }
575 return DeclGroupPtrTy();
576}
577
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000578namespace {
579class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
580 Sema &SemaRef;
581
582public:
583 bool VisitDeclRefExpr(const DeclRefExpr *E) {
584 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
585 if (VD->hasLocalStorage()) {
586 SemaRef.Diag(E->getLocStart(),
587 diag::err_omp_local_var_in_threadprivate_init)
588 << E->getSourceRange();
589 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
590 << VD << VD->getSourceRange();
591 return true;
592 }
593 }
594 return false;
595 }
596 bool VisitStmt(const Stmt *S) {
597 for (auto Child : S->children()) {
598 if (Child && Visit(Child))
599 return true;
600 }
601 return false;
602 }
603 LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
604};
605} // namespace
606
Alexey Bataeved09d242014-05-28 05:53:51 +0000607OMPThreadPrivateDecl *
608Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000609 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000610 for (auto &RefExpr : VarList) {
611 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000612 VarDecl *VD = cast<VarDecl>(DE->getDecl());
613 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000614
615 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
616 // A threadprivate variable must not have an incomplete type.
617 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000618 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000619 continue;
620 }
621
622 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
623 // A threadprivate variable must not have a reference type.
624 if (VD->getType()->isReferenceType()) {
625 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000626 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
627 bool IsDecl =
628 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
629 Diag(VD->getLocation(),
630 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
631 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000632 continue;
633 }
634
Richard Smithfd3834f2013-04-13 02:43:54 +0000635 // Check if this is a TLS variable.
636 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000637 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000638 bool IsDecl =
639 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
640 Diag(VD->getLocation(),
641 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
642 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000643 continue;
644 }
645
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000646 // Check if initial value of threadprivate variable reference variable with
647 // local storage (it is not supported by runtime).
648 if (auto Init = VD->getAnyInitializer()) {
649 LocalVarRefChecker Checker(*this);
650 if (Checker.Visit(Init)) continue;
651 }
652
Alexey Bataeved09d242014-05-28 05:53:51 +0000653 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000654 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000655 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000656 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000657 if (!Vars.empty()) {
658 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
659 Vars);
660 D->setAccess(AS_public);
661 }
662 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000663}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000664
Alexey Bataev758e55e2013-09-06 18:03:48 +0000665namespace {
666class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
667 DSAStackTy *Stack;
668 Sema &Actions;
669 bool ErrorFound;
670 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000671 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataeved09d242014-05-28 05:53:51 +0000672
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673public:
674 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000675 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000676 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000677 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
678 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000679
680 SourceLocation ELoc = E->getExprLoc();
681
682 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
683 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
684 if (DVar.CKind != OMPC_unknown) {
685 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000686 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000687 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000688 return;
689 }
690 // The default(none) clause requires that each variable that is referenced
691 // in the construct, and does not have a predetermined data-sharing
692 // attribute, must have its data-sharing attribute explicitly determined
693 // by being listed in a data-sharing attribute clause.
694 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
695 (DKind == OMPD_parallel || DKind == OMPD_task)) {
696 ErrorFound = true;
697 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
698 return;
699 }
700
701 // OpenMP [2.9.3.6, Restrictions, p.2]
702 // A list item that appears in a reduction clause of the innermost
703 // enclosing worksharing or parallel construct may not be accessed in an
704 // explicit task.
705 // TODO:
706
707 // Define implicit data-sharing attributes for task.
708 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000709 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
710 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000711 }
712 }
713 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000714 for (auto C : S->clauses())
715 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000716 for (StmtRange R = C->children(); R; ++R)
717 if (Stmt *Child = *R)
718 Visit(Child);
719 }
720 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000721 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
722 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000723 if (Stmt *Child = *I)
724 if (!isa<OMPExecutableDirective>(Child))
725 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000726 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000727
728 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000729 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000730
731 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
Alexey Bataeved09d242014-05-28 05:53:51 +0000732 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000733};
Alexey Bataeved09d242014-05-28 05:53:51 +0000734} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000735
Alexey Bataev9959db52014-05-06 10:08:46 +0000736void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
737 Scope *CurScope) {
738 switch (DKind) {
739 case OMPD_parallel: {
740 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
741 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
742 Sema::CapturedParamNameType Params[3] = {
743 std::make_pair(".global_tid.", KmpInt32PtrTy),
744 std::make_pair(".bound_tid.", KmpInt32PtrTy),
745 std::make_pair(StringRef(), QualType()) // __context with shared vars
746 };
747 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
748 break;
749 }
750 case OMPD_simd: {
751 Sema::CapturedParamNameType Params[1] = {
752 std::make_pair(StringRef(), QualType()) // __context with shared vars
753 };
754 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
755 break;
756 }
757 case OMPD_threadprivate:
758 case OMPD_task:
759 llvm_unreachable("OpenMP Directive is not allowed");
760 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000761 llvm_unreachable("Unknown OpenMP directive");
762 }
763}
764
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000765StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
766 ArrayRef<OMPClause *> Clauses,
767 Stmt *AStmt,
768 SourceLocation StartLoc,
769 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000770 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
771
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000772 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000773
774 // Check default data sharing attributes for referenced variables.
775 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
776 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
777 if (DSAChecker.isErrorFound())
778 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000779 // Generate list of implicitly defined firstprivate variables.
780 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
781 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
782
783 bool ErrorFound = false;
784 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000785 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
786 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
787 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000788 ClausesWithImplicit.push_back(Implicit);
789 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +0000790 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000791 } else
792 ErrorFound = true;
793 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000794
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000795 switch (Kind) {
796 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +0000797 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
798 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000799 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000800 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +0000801 Res =
802 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000803 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000804 case OMPD_threadprivate:
805 case OMPD_task:
806 llvm_unreachable("OpenMP Directive is not allowed");
807 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000808 llvm_unreachable("Unknown OpenMP directive");
809 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000810
Alexey Bataeved09d242014-05-28 05:53:51 +0000811 if (ErrorFound)
812 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000813 return Res;
814}
815
816StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
817 Stmt *AStmt,
818 SourceLocation StartLoc,
819 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000820 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
821 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
822 // 1.2.2 OpenMP Language Terminology
823 // Structured block - An executable statement with a single entry at the
824 // top and a single exit at the bottom.
825 // The point of exit cannot be a branch out of the structured block.
826 // longjmp() and throw() must not violate the entry/exit criteria.
827 CS->getCapturedDecl()->setNothrow();
828
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000829 getCurFunction()->setHasBranchProtectedScope();
830
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000831 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
832 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000833}
834
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000835StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +0000836 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000837 SourceLocation EndLoc) {
838 Stmt *CStmt = AStmt;
839 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(CStmt))
840 CStmt = CS->getCapturedStmt();
841 while (AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(CStmt))
842 CStmt = AS->getSubStmt();
843 ForStmt *For = dyn_cast<ForStmt>(CStmt);
844 if (!For) {
845 Diag(CStmt->getLocStart(), diag::err_omp_not_for)
Alexey Bataeved09d242014-05-28 05:53:51 +0000846 << getOpenMPDirectiveName(OMPD_simd);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000847 return StmtError();
848 }
849
850 // FIXME: Checking loop canonical form, collapsing etc.
851
852 getCurFunction()->setHasBranchProtectedScope();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000853 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000854}
855
Alexey Bataeved09d242014-05-28 05:53:51 +0000856OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000857 SourceLocation StartLoc,
858 SourceLocation LParenLoc,
859 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000860 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000861 switch (Kind) {
862 case OMPC_if:
863 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
864 break;
Alexey Bataev568a8332014-03-06 06:15:19 +0000865 case OMPC_num_threads:
866 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
867 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000868 case OMPC_safelen:
869 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
870 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +0000871 case OMPC_collapse:
872 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
873 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000874 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +0000875 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000876 case OMPC_private:
877 case OMPC_firstprivate:
878 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +0000879 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000880 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000881 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000882 case OMPC_threadprivate:
883 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000884 llvm_unreachable("Clause is not allowed.");
885 }
886 return Res;
887}
888
Alexey Bataeved09d242014-05-28 05:53:51 +0000889OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000890 SourceLocation LParenLoc,
891 SourceLocation EndLoc) {
892 Expr *ValExpr = Condition;
893 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
894 !Condition->isInstantiationDependent() &&
895 !Condition->containsUnexpandedParameterPack()) {
896 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000897 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000898 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000899 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000900
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000901 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +0000902 }
903
904 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
905}
906
Alexey Bataev568a8332014-03-06 06:15:19 +0000907ExprResult Sema::PerformImplicitIntegerConversion(SourceLocation Loc,
908 Expr *Op) {
909 if (!Op)
910 return ExprError();
911
912 class IntConvertDiagnoser : public ICEConvertDiagnoser {
913 public:
914 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +0000915 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000916 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
917 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000918 return S.Diag(Loc, diag::err_omp_not_integral) << T;
919 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000920 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
921 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000922 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
923 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000924 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
925 QualType T,
926 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000927 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
928 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000929 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
930 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000931 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +0000932 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +0000933 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000934 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
935 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000936 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
937 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000938 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
939 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000940 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +0000941 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +0000942 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000943 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
944 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +0000945 llvm_unreachable("conversion functions are permitted");
946 }
947 } ConvertDiagnoser;
948 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
949}
950
951OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
952 SourceLocation StartLoc,
953 SourceLocation LParenLoc,
954 SourceLocation EndLoc) {
955 Expr *ValExpr = NumThreads;
956 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
957 !NumThreads->isInstantiationDependent() &&
958 !NumThreads->containsUnexpandedParameterPack()) {
959 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
960 ExprResult Val =
961 PerformImplicitIntegerConversion(NumThreadsLoc, NumThreads);
962 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000963 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000964
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000965 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +0000966
967 // OpenMP [2.5, Restrictions]
968 // The num_threads expression must evaluate to a positive integer value.
969 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +0000970 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
971 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +0000972 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
973 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000974 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +0000975 }
976 }
977
Alexey Bataeved09d242014-05-28 05:53:51 +0000978 return new (Context)
979 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +0000980}
981
Alexey Bataev62c87d22014-03-21 04:51:18 +0000982ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
983 OpenMPClauseKind CKind) {
984 if (!E)
985 return ExprError();
986 if (E->isValueDependent() || E->isTypeDependent() ||
987 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000988 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +0000989 llvm::APSInt Result;
990 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
991 if (ICE.isInvalid())
992 return ExprError();
993 if (!Result.isStrictlyPositive()) {
994 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
995 << getOpenMPClauseName(CKind) << E->getSourceRange();
996 return ExprError();
997 }
998 return ICE;
999}
1000
1001OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1002 SourceLocation LParenLoc,
1003 SourceLocation EndLoc) {
1004 // OpenMP [2.8.1, simd construct, Description]
1005 // The parameter of the safelen clause must be a constant
1006 // positive integer expression.
1007 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1008 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001009 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001010 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001011 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001012}
1013
Alexander Musman8bd31e62014-05-27 15:12:19 +00001014OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *Num, SourceLocation StartLoc,
1015 SourceLocation LParenLoc,
1016 SourceLocation EndLoc) {
1017 // OpenMP [2.8.1, simd construct, Description]
1018 // The parameter of the collapse clause must be a constant
1019 // positive integer expression.
1020 ExprResult NumForLoops =
1021 VerifyPositiveIntegerConstantInClause(Num, OMPC_collapse);
1022 if (NumForLoops.isInvalid())
1023 return nullptr;
1024 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001025 OMPCollapseClause(NumForLoops.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001026}
1027
Alexey Bataeved09d242014-05-28 05:53:51 +00001028OMPClause *Sema::ActOnOpenMPSimpleClause(
1029 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1030 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001031 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001032 switch (Kind) {
1033 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001034 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001035 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1036 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001037 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001038 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001039 Res = ActOnOpenMPProcBindClause(
1040 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1041 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001042 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001043 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001044 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001045 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001046 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001047 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001048 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001049 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +00001050 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001051 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001052 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001053 case OMPC_threadprivate:
1054 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001055 llvm_unreachable("Clause is not allowed.");
1056 }
1057 return Res;
1058}
1059
1060OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1061 SourceLocation KindKwLoc,
1062 SourceLocation StartLoc,
1063 SourceLocation LParenLoc,
1064 SourceLocation EndLoc) {
1065 if (Kind == OMPC_DEFAULT_unknown) {
1066 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001067 static_assert(OMPC_DEFAULT_unknown > 0,
1068 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001069 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001070 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001071 Values += "'";
1072 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1073 Values += "'";
1074 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001075 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001076 Values += " or ";
1077 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001078 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001079 break;
1080 default:
1081 Values += Sep;
1082 break;
1083 }
1084 }
1085 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001086 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001087 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001088 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001089 switch (Kind) {
1090 case OMPC_DEFAULT_none:
1091 DSAStack->setDefaultDSANone();
1092 break;
1093 case OMPC_DEFAULT_shared:
1094 DSAStack->setDefaultDSAShared();
1095 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001096 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001097 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001098 break;
1099 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001100 return new (Context)
1101 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001102}
1103
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001104OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1105 SourceLocation KindKwLoc,
1106 SourceLocation StartLoc,
1107 SourceLocation LParenLoc,
1108 SourceLocation EndLoc) {
1109 if (Kind == OMPC_PROC_BIND_unknown) {
1110 std::string Values;
1111 std::string Sep(", ");
1112 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1113 Values += "'";
1114 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1115 Values += "'";
1116 switch (i) {
1117 case OMPC_PROC_BIND_unknown - 2:
1118 Values += " or ";
1119 break;
1120 case OMPC_PROC_BIND_unknown - 1:
1121 break;
1122 default:
1123 Values += Sep;
1124 break;
1125 }
1126 }
1127 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001128 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001129 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001130 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001131 return new (Context)
1132 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001133}
1134
Alexey Bataeved09d242014-05-28 05:53:51 +00001135OMPClause *
1136Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, ArrayRef<Expr *> VarList,
1137 Expr *TailExpr, SourceLocation StartLoc,
1138 SourceLocation LParenLoc,
1139 SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001140 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001141 switch (Kind) {
1142 case OMPC_private:
1143 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1144 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001145 case OMPC_firstprivate:
1146 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1147 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001148 case OMPC_shared:
1149 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1150 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001151 case OMPC_linear:
1152 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1153 ColonLoc, EndLoc);
1154 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001155 case OMPC_aligned:
1156 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
1157 ColonLoc, EndLoc);
1158 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001159 case OMPC_copyin:
1160 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1161 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001162 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001163 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001164 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001165 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001166 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001167 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001168 case OMPC_threadprivate:
1169 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001170 llvm_unreachable("Clause is not allowed.");
1171 }
1172 return Res;
1173}
1174
1175OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1176 SourceLocation StartLoc,
1177 SourceLocation LParenLoc,
1178 SourceLocation EndLoc) {
1179 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001180 for (auto &RefExpr : VarList) {
1181 assert(RefExpr && "NULL expr in OpenMP private clause.");
1182 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001183 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001184 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001185 continue;
1186 }
1187
Alexey Bataeved09d242014-05-28 05:53:51 +00001188 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001189 // OpenMP [2.1, C/C++]
1190 // A list item is a variable name.
1191 // OpenMP [2.9.3.3, Restrictions, p.1]
1192 // A variable that is part of another variable (as an array or
1193 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001195 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001196 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001197 continue;
1198 }
1199 Decl *D = DE->getDecl();
1200 VarDecl *VD = cast<VarDecl>(D);
1201
1202 QualType Type = VD->getType();
1203 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1204 // It will be analyzed later.
1205 Vars.push_back(DE);
1206 continue;
1207 }
1208
1209 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1210 // A variable that appears in a private clause must not have an incomplete
1211 // type or a reference type.
1212 if (RequireCompleteType(ELoc, Type,
1213 diag::err_omp_private_incomplete_type)) {
1214 continue;
1215 }
1216 if (Type->isReferenceType()) {
1217 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001218 << getOpenMPClauseName(OMPC_private) << Type;
1219 bool IsDecl =
1220 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1221 Diag(VD->getLocation(),
1222 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1223 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001224 continue;
1225 }
1226
1227 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1228 // A variable of class type (or array thereof) that appears in a private
1229 // clause requires an accesible, unambiguous default constructor for the
1230 // class type.
1231 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001232 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
1233 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001234 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001235 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1236 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1237 : nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001238 if (RD) {
1239 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1240 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001241 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1242 if (!CD || CheckConstructorAccess(
1243 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1244 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001245 CD->isDeleted()) {
1246 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001247 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001248 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1249 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001250 Diag(VD->getLocation(),
1251 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1252 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001253 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1254 continue;
1255 }
1256 MarkFunctionReferenced(ELoc, CD);
1257 DiagnoseUseOfDecl(CD, ELoc);
1258
1259 CXXDestructorDecl *DD = RD->getDestructor();
1260 if (DD) {
1261 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1262 DD->isDeleted()) {
1263 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001264 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001265 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1266 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001267 Diag(VD->getLocation(),
1268 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1269 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001270 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1271 continue;
1272 }
1273 MarkFunctionReferenced(ELoc, DD);
1274 DiagnoseUseOfDecl(DD, ELoc);
1275 }
1276 }
1277
Alexey Bataev758e55e2013-09-06 18:03:48 +00001278 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1279 // in a Construct]
1280 // Variables with the predetermined data-sharing attributes may not be
1281 // listed in data-sharing attributes clauses, except for the cases
1282 // listed below. For these exceptions only, listing a predetermined
1283 // variable in a data-sharing attribute clause is allowed and overrides
1284 // the variable's predetermined data-sharing attributes.
1285 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1286 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001287 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1288 << getOpenMPClauseName(OMPC_private);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001289 if (DVar.RefExpr) {
1290 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001291 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001292 } else {
1293 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001294 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001295 }
1296 continue;
1297 }
1298
1299 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001300 Vars.push_back(DE);
1301 }
1302
Alexey Bataeved09d242014-05-28 05:53:51 +00001303 if (Vars.empty())
1304 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001305
1306 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1307}
1308
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001309OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1310 SourceLocation StartLoc,
1311 SourceLocation LParenLoc,
1312 SourceLocation EndLoc) {
1313 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001314 for (auto &RefExpr : VarList) {
1315 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
1316 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001317 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001318 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001319 continue;
1320 }
1321
Alexey Bataeved09d242014-05-28 05:53:51 +00001322 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001323 // OpenMP [2.1, C/C++]
1324 // A list item is a variable name.
1325 // OpenMP [2.9.3.3, Restrictions, p.1]
1326 // A variable that is part of another variable (as an array or
1327 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001328 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001329 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001330 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001331 continue;
1332 }
1333 Decl *D = DE->getDecl();
1334 VarDecl *VD = cast<VarDecl>(D);
1335
1336 QualType Type = VD->getType();
1337 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1338 // It will be analyzed later.
1339 Vars.push_back(DE);
1340 continue;
1341 }
1342
1343 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1344 // A variable that appears in a private clause must not have an incomplete
1345 // type or a reference type.
1346 if (RequireCompleteType(ELoc, Type,
1347 diag::err_omp_firstprivate_incomplete_type)) {
1348 continue;
1349 }
1350 if (Type->isReferenceType()) {
1351 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001352 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1353 bool IsDecl =
1354 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1355 Diag(VD->getLocation(),
1356 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1357 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001358 continue;
1359 }
1360
1361 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1362 // A variable of class type (or array thereof) that appears in a private
1363 // clause requires an accesible, unambiguous copy constructor for the
1364 // class type.
1365 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001366 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1367 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1368 : nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001369 if (RD) {
1370 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1371 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001372 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1373 if (!CD || CheckConstructorAccess(
1374 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1375 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001376 CD->isDeleted()) {
1377 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001378 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001379 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1380 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001381 Diag(VD->getLocation(),
1382 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1383 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001384 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1385 continue;
1386 }
1387 MarkFunctionReferenced(ELoc, CD);
1388 DiagnoseUseOfDecl(CD, ELoc);
1389
1390 CXXDestructorDecl *DD = RD->getDestructor();
1391 if (DD) {
1392 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1393 DD->isDeleted()) {
1394 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001395 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001396 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1397 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001398 Diag(VD->getLocation(),
1399 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1400 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001401 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1402 continue;
1403 }
1404 MarkFunctionReferenced(ELoc, DD);
1405 DiagnoseUseOfDecl(DD, ELoc);
1406 }
1407 }
1408
1409 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1410 // variable and it was checked already.
1411 if (StartLoc.isValid() && EndLoc.isValid()) {
1412 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1413 Type = Type.getNonReferenceType().getCanonicalType();
1414 bool IsConstant = Type.isConstant(Context);
1415 Type = Context.getBaseElementType(Type);
1416 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1417 // A list item that specifies a given variable may not appear in more
1418 // than one clause on the same directive, except that a variable may be
1419 // specified in both firstprivate and lastprivate clauses.
1420 // TODO: add processing for lastprivate.
1421 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1422 DVar.RefExpr) {
1423 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001424 << getOpenMPClauseName(DVar.CKind)
1425 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001426 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001427 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001428 continue;
1429 }
1430
1431 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1432 // in a Construct]
1433 // Variables with the predetermined data-sharing attributes may not be
1434 // listed in data-sharing attributes clauses, except for the cases
1435 // listed below. For these exceptions only, listing a predetermined
1436 // variable in a data-sharing attribute clause is allowed and overrides
1437 // the variable's predetermined data-sharing attributes.
1438 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1439 // in a Construct, C/C++, p.2]
1440 // Variables with const-qualified type having no mutable member may be
1441 // listed in a firstprivate clause, even if they are static data members.
1442 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1443 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1444 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001445 << getOpenMPClauseName(DVar.CKind)
1446 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001447 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001448 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001449 continue;
1450 }
1451
1452 // OpenMP [2.9.3.4, Restrictions, p.2]
1453 // A list item that is private within a parallel region must not appear
1454 // in a firstprivate clause on a worksharing construct if any of the
1455 // worksharing regions arising from the worksharing construct ever bind
1456 // to any of the parallel regions arising from the parallel construct.
1457 // OpenMP [2.9.3.4, Restrictions, p.3]
1458 // A list item that appears in a reduction clause of a parallel construct
1459 // must not appear in a firstprivate clause on a worksharing or task
1460 // construct if any of the worksharing or task regions arising from the
1461 // worksharing or task construct ever bind to any of the parallel regions
1462 // arising from the parallel construct.
1463 // OpenMP [2.9.3.4, Restrictions, p.4]
1464 // A list item that appears in a reduction clause in worksharing
1465 // construct must not appear in a firstprivate clause in a task construct
1466 // encountered during execution of any of the worksharing regions arising
1467 // from the worksharing construct.
1468 // TODO:
1469 }
1470
1471 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1472 Vars.push_back(DE);
1473 }
1474
Alexey Bataeved09d242014-05-28 05:53:51 +00001475 if (Vars.empty())
1476 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001477
1478 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1479 Vars);
1480}
1481
Alexey Bataev758e55e2013-09-06 18:03:48 +00001482OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1483 SourceLocation StartLoc,
1484 SourceLocation LParenLoc,
1485 SourceLocation EndLoc) {
1486 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001487 for (auto &RefExpr : VarList) {
1488 assert(RefExpr && "NULL expr in OpenMP shared clause.");
1489 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001490 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001491 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001492 continue;
1493 }
1494
Alexey Bataeved09d242014-05-28 05:53:51 +00001495 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001496 // OpenMP [2.1, C/C++]
1497 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001498 // OpenMP [2.14.3.2, Restrictions, p.1]
1499 // A variable that is part of another variable (as an array or structure
1500 // element) cannot appear in a shared unless it is a static data member
1501 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00001502 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001503 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001504 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001505 continue;
1506 }
1507 Decl *D = DE->getDecl();
1508 VarDecl *VD = cast<VarDecl>(D);
1509
1510 QualType Type = VD->getType();
1511 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1512 // It will be analyzed later.
1513 Vars.push_back(DE);
1514 continue;
1515 }
1516
1517 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1518 // in a Construct]
1519 // Variables with the predetermined data-sharing attributes may not be
1520 // listed in data-sharing attributes clauses, except for the cases
1521 // listed below. For these exceptions only, listing a predetermined
1522 // variable in a data-sharing attribute clause is allowed and overrides
1523 // the variable's predetermined data-sharing attributes.
1524 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00001525 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
1526 DVar.RefExpr) {
1527 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1528 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001529 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001530 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001531 continue;
1532 }
1533
1534 DSAStack->addDSA(VD, DE, OMPC_shared);
1535 Vars.push_back(DE);
1536 }
1537
Alexey Bataeved09d242014-05-28 05:53:51 +00001538 if (Vars.empty())
1539 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001540
1541 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1542}
1543
Alexander Musman8dba6642014-04-22 13:09:42 +00001544OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
1545 SourceLocation StartLoc,
1546 SourceLocation LParenLoc,
1547 SourceLocation ColonLoc,
1548 SourceLocation EndLoc) {
1549 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001550 for (auto &RefExpr : VarList) {
1551 assert(RefExpr && "NULL expr in OpenMP linear clause.");
1552 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00001553 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001554 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00001555 continue;
1556 }
1557
1558 // OpenMP [2.14.3.7, linear clause]
1559 // A list item that appears in a linear clause is subject to the private
1560 // clause semantics described in Section 2.14.3.3 on page 159 except as
1561 // noted. In addition, the value of the new list item on each iteration
1562 // of the associated loop(s) corresponds to the value of the original
1563 // list item before entering the construct plus the logical number of
1564 // the iteration times linear-step.
1565
Alexey Bataeved09d242014-05-28 05:53:51 +00001566 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00001567 // OpenMP [2.1, C/C++]
1568 // A list item is a variable name.
1569 // OpenMP [2.14.3.3, Restrictions, p.1]
1570 // A variable that is part of another variable (as an array or
1571 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001572 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00001573 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001574 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00001575 continue;
1576 }
1577
1578 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1579
1580 // OpenMP [2.14.3.7, linear clause]
1581 // A list-item cannot appear in more than one linear clause.
1582 // A list-item that appears in a linear clause cannot appear in any
1583 // other data-sharing attribute clause.
1584 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1585 if (DVar.RefExpr) {
1586 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1587 << getOpenMPClauseName(OMPC_linear);
1588 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1589 << getOpenMPClauseName(DVar.CKind);
1590 continue;
1591 }
1592
1593 QualType QType = VD->getType();
1594 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1595 // It will be analyzed later.
1596 Vars.push_back(DE);
1597 continue;
1598 }
1599
1600 // A variable must not have an incomplete type or a reference type.
1601 if (RequireCompleteType(ELoc, QType,
1602 diag::err_omp_linear_incomplete_type)) {
1603 continue;
1604 }
1605 if (QType->isReferenceType()) {
1606 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
1607 << getOpenMPClauseName(OMPC_linear) << QType;
1608 bool IsDecl =
1609 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1610 Diag(VD->getLocation(),
1611 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1612 << VD;
1613 continue;
1614 }
1615
1616 // A list item must not be const-qualified.
1617 if (QType.isConstant(Context)) {
1618 Diag(ELoc, diag::err_omp_const_variable)
1619 << getOpenMPClauseName(OMPC_linear);
1620 bool IsDecl =
1621 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1622 Diag(VD->getLocation(),
1623 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1624 << VD;
1625 continue;
1626 }
1627
1628 // A list item must be of integral or pointer type.
1629 QType = QType.getUnqualifiedType().getCanonicalType();
1630 const Type *Ty = QType.getTypePtrOrNull();
1631 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
1632 !Ty->isPointerType())) {
1633 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
1634 bool IsDecl =
1635 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1636 Diag(VD->getLocation(),
1637 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1638 << VD;
1639 continue;
1640 }
1641
1642 DSAStack->addDSA(VD, DE, OMPC_linear);
1643 Vars.push_back(DE);
1644 }
1645
1646 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001647 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00001648
1649 Expr *StepExpr = Step;
1650 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
1651 !Step->isInstantiationDependent() &&
1652 !Step->containsUnexpandedParameterPack()) {
1653 SourceLocation StepLoc = Step->getLocStart();
1654 ExprResult Val = PerformImplicitIntegerConversion(StepLoc, Step);
1655 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001656 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001657 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00001658
1659 // Warn about zero linear step (it would be probably better specified as
1660 // making corresponding variables 'const').
1661 llvm::APSInt Result;
1662 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
1663 !Result.isNegative() && !Result.isStrictlyPositive())
1664 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
1665 << (Vars.size() > 1);
1666 }
1667
1668 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
1669 Vars, StepExpr);
1670}
1671
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001672OMPClause *Sema::ActOnOpenMPAlignedClause(
1673 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
1674 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
1675
1676 SmallVector<Expr *, 8> Vars;
1677 for (auto &RefExpr : VarList) {
1678 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
1679 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
1680 // It will be analyzed later.
1681 Vars.push_back(RefExpr);
1682 continue;
1683 }
1684
1685 SourceLocation ELoc = RefExpr->getExprLoc();
1686 // OpenMP [2.1, C/C++]
1687 // A list item is a variable name.
1688 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
1689 if (!DE || !isa<VarDecl>(DE->getDecl())) {
1690 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
1691 continue;
1692 }
1693
1694 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1695
1696 // OpenMP [2.8.1, simd construct, Restrictions]
1697 // The type of list items appearing in the aligned clause must be
1698 // array, pointer, reference to array, or reference to pointer.
1699 QualType QType = DE->getType()
1700 .getNonReferenceType()
1701 .getUnqualifiedType()
1702 .getCanonicalType();
1703 const Type *Ty = QType.getTypePtrOrNull();
1704 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
1705 !Ty->isPointerType())) {
1706 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
1707 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
1708 bool IsDecl =
1709 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1710 Diag(VD->getLocation(),
1711 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1712 << VD;
1713 continue;
1714 }
1715
1716 // OpenMP [2.8.1, simd construct, Restrictions]
1717 // A list-item cannot appear in more than one aligned clause.
1718 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
1719 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
1720 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
1721 << getOpenMPClauseName(OMPC_aligned);
1722 continue;
1723 }
1724
1725 Vars.push_back(DE);
1726 }
1727
1728 // OpenMP [2.8.1, simd construct, Description]
1729 // The parameter of the aligned clause, alignment, must be a constant
1730 // positive integer expression.
1731 // If no optional parameter is specified, implementation-defined default
1732 // alignments for SIMD instructions on the target platforms are assumed.
1733 if (Alignment != nullptr) {
1734 ExprResult AlignResult =
1735 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
1736 if (AlignResult.isInvalid())
1737 return nullptr;
1738 Alignment = AlignResult.get();
1739 }
1740 if (Vars.empty())
1741 return nullptr;
1742
1743 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
1744 EndLoc, Vars, Alignment);
1745}
1746
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001747OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
1748 SourceLocation StartLoc,
1749 SourceLocation LParenLoc,
1750 SourceLocation EndLoc) {
1751 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001752 for (auto &RefExpr : VarList) {
1753 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
1754 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001755 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001756 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001757 continue;
1758 }
1759
Alexey Bataeved09d242014-05-28 05:53:51 +00001760 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001761 // OpenMP [2.1, C/C++]
1762 // A list item is a variable name.
1763 // OpenMP [2.14.4.1, Restrictions, p.1]
1764 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00001765 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001766 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001767 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001768 continue;
1769 }
1770
1771 Decl *D = DE->getDecl();
1772 VarDecl *VD = cast<VarDecl>(D);
1773
1774 QualType Type = VD->getType();
1775 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1776 // It will be analyzed later.
1777 Vars.push_back(DE);
1778 continue;
1779 }
1780
1781 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
1782 // A list item that appears in a copyin clause must be threadprivate.
1783 if (!DSAStack->isThreadPrivate(VD)) {
1784 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00001785 << getOpenMPClauseName(OMPC_copyin)
1786 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001787 continue;
1788 }
1789
1790 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
1791 // A variable of class type (or array thereof) that appears in a
1792 // copyin clause requires an accesible, unambiguous copy assignment
1793 // operator for the class type.
1794 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001795 CXXRecordDecl *RD =
1796 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001797 if (RD) {
1798 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
1799 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataeved09d242014-05-28 05:53:51 +00001800 if (!MD || CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001801 MD->isDeleted()) {
1802 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001803 << getOpenMPClauseName(OMPC_copyin) << 2;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001804 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1805 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001806 Diag(VD->getLocation(),
1807 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1808 << VD;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001809 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1810 continue;
1811 }
1812 MarkFunctionReferenced(ELoc, MD);
1813 DiagnoseUseOfDecl(MD, ELoc);
1814 }
1815
1816 DSAStack->addDSA(VD, DE, OMPC_copyin);
1817 Vars.push_back(DE);
1818 }
1819
Alexey Bataeved09d242014-05-28 05:53:51 +00001820 if (Vars.empty())
1821 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001822
1823 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1824}
1825
Alexey Bataev758e55e2013-09-06 18:03:48 +00001826#undef DSAStack