blob: 32b956b81ea361926e8beb56d07f631dfc1e7b91 [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
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000835namespace {
836/// \brief Helper class for checking canonical form of the OpenMP loops and
837/// extracting iteration space of each loop in the loop nest, that will be used
838/// for IR generation.
839class OpenMPIterationSpaceChecker {
840 /// \brief Reference to Sema.
841 Sema &SemaRef;
842 /// \brief A location for diagnostics (when there is no some better location).
843 SourceLocation DefaultLoc;
844 /// \brief A location for diagnostics (when increment is not compatible).
845 SourceLocation ConditionLoc;
846 /// \brief A source location for referring to condition later.
847 SourceRange ConditionSrcRange;
848 /// \brief Loop variable.
849 VarDecl *Var;
850 /// \brief Lower bound (initializer for the var).
851 Expr *LB;
852 /// \brief Upper bound.
853 Expr *UB;
854 /// \brief Loop step (increment).
855 Expr *Step;
856 /// \brief This flag is true when condition is one of:
857 /// Var < UB
858 /// Var <= UB
859 /// UB > Var
860 /// UB >= Var
861 bool TestIsLessOp;
862 /// \brief This flag is true when condition is strict ( < or > ).
863 bool TestIsStrictOp;
864 /// \brief This flag is true when step is subtracted on each iteration.
865 bool SubtractStep;
866
867public:
868 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
869 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
870 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
871 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
872 SubtractStep(false) {}
873 /// \brief Check init-expr for canonical loop form and save loop counter
874 /// variable - #Var and its initialization value - #LB.
875 bool CheckInit(Stmt *S);
876 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
877 /// for less/greater and for strict/non-strict comparison.
878 bool CheckCond(Expr *S);
879 /// \brief Check incr-expr for canonical loop form and return true if it
880 /// does not conform, otherwise save loop step (#Step).
881 bool CheckInc(Expr *S);
882 /// \brief Return the loop counter variable.
883 VarDecl *GetLoopVar() const { return Var; }
884 /// \brief Return true if any expression is dependent.
885 bool Dependent() const;
886
887private:
888 /// \brief Check the right-hand side of an assignment in the increment
889 /// expression.
890 bool CheckIncRHS(Expr *RHS);
891 /// \brief Helper to set loop counter variable and its initializer.
892 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
893 /// \brief Helper to set upper bound.
894 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
895 const SourceLocation &SL);
896 /// \brief Helper to set loop increment.
897 bool SetStep(Expr *NewStep, bool Subtract);
898};
899
900bool OpenMPIterationSpaceChecker::Dependent() const {
901 if (!Var) {
902 assert(!LB && !UB && !Step);
903 return false;
904 }
905 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
906 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
907}
908
909bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
910 // State consistency checking to ensure correct usage.
911 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
912 !TestIsLessOp && !TestIsStrictOp);
913 if (!NewVar || !NewLB)
914 return true;
915 Var = NewVar;
916 LB = NewLB;
917 return false;
918}
919
920bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
921 const SourceRange &SR,
922 const SourceLocation &SL) {
923 // State consistency checking to ensure correct usage.
924 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
925 !TestIsLessOp && !TestIsStrictOp);
926 if (!NewUB)
927 return true;
928 UB = NewUB;
929 TestIsLessOp = LessOp;
930 TestIsStrictOp = StrictOp;
931 ConditionSrcRange = SR;
932 ConditionLoc = SL;
933 return false;
934}
935
936bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
937 // State consistency checking to ensure correct usage.
938 assert(Var != nullptr && LB != nullptr && Step == nullptr);
939 if (!NewStep)
940 return true;
941 if (!NewStep->isValueDependent()) {
942 // Check that the step is integer expression.
943 SourceLocation StepLoc = NewStep->getLocStart();
944 ExprResult Val =
945 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
946 if (Val.isInvalid())
947 return true;
948 NewStep = Val.get();
949
950 // OpenMP [2.6, Canonical Loop Form, Restrictions]
951 // If test-expr is of form var relational-op b and relational-op is < or
952 // <= then incr-expr must cause var to increase on each iteration of the
953 // loop. If test-expr is of form var relational-op b and relational-op is
954 // > or >= then incr-expr must cause var to decrease on each iteration of
955 // the loop.
956 // If test-expr is of form b relational-op var and relational-op is < or
957 // <= then incr-expr must cause var to decrease on each iteration of the
958 // loop. If test-expr is of form b relational-op var and relational-op is
959 // > or >= then incr-expr must cause var to increase on each iteration of
960 // the loop.
961 llvm::APSInt Result;
962 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
963 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
964 bool IsConstNeg =
965 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
966 bool IsConstZero = IsConstant && !Result.getBoolValue();
967 if (UB && (IsConstZero ||
968 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
969 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
970 SemaRef.Diag(NewStep->getExprLoc(),
971 diag::err_omp_loop_incr_not_compatible)
972 << Var << TestIsLessOp << NewStep->getSourceRange();
973 SemaRef.Diag(ConditionLoc,
974 diag::note_omp_loop_cond_requres_compatible_incr)
975 << TestIsLessOp << ConditionSrcRange;
976 return true;
977 }
978 }
979
980 Step = NewStep;
981 SubtractStep = Subtract;
982 return false;
983}
984
985bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
986 // Check init-expr for canonical loop form and save loop counter
987 // variable - #Var and its initialization value - #LB.
988 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
989 // var = lb
990 // integer-type var = lb
991 // random-access-iterator-type var = lb
992 // pointer-type var = lb
993 //
994 if (!S) {
995 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
996 return true;
997 }
998 if (Expr *E = dyn_cast<Expr>(S))
999 S = E->IgnoreParens();
1000 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1001 if (BO->getOpcode() == BO_Assign)
1002 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1003 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1004 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1005 if (DS->isSingleDecl()) {
1006 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1007 if (Var->hasInit()) {
1008 // Accept non-canonical init form here but emit ext. warning.
1009 if (Var->getInitStyle() != VarDecl::CInit)
1010 SemaRef.Diag(S->getLocStart(),
1011 diag::ext_omp_loop_not_canonical_init)
1012 << S->getSourceRange();
1013 return SetVarAndLB(Var, Var->getInit());
1014 }
1015 }
1016 }
1017 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1018 if (CE->getOperator() == OO_Equal)
1019 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1020 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1021
1022 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1023 << S->getSourceRange();
1024 return true;
1025}
1026
1027/// \brief Ignore parenthesises, implicit casts, copy constructor and return the
1028/// variable (which may be the loop variable) if possible.
1029static const VarDecl *GetInitVarDecl(const Expr *E) {
1030 if (!E)
1031 return 0;
1032 E = E->IgnoreParenImpCasts();
1033 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1034 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1035 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1036 CE->getArg(0) != nullptr)
1037 E = CE->getArg(0)->IgnoreParenImpCasts();
1038 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1039 if (!DRE)
1040 return nullptr;
1041 return dyn_cast<VarDecl>(DRE->getDecl());
1042}
1043
1044bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1045 // Check test-expr for canonical form, save upper-bound UB, flags for
1046 // less/greater and for strict/non-strict comparison.
1047 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1048 // var relational-op b
1049 // b relational-op var
1050 //
1051 if (!S) {
1052 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1053 return true;
1054 }
1055 S = S->IgnoreParenImpCasts();
1056 SourceLocation CondLoc = S->getLocStart();
1057 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1058 if (BO->isRelationalOp()) {
1059 if (GetInitVarDecl(BO->getLHS()) == Var)
1060 return SetUB(BO->getRHS(),
1061 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1062 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1063 BO->getSourceRange(), BO->getOperatorLoc());
1064 if (GetInitVarDecl(BO->getRHS()) == Var)
1065 return SetUB(BO->getLHS(),
1066 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1067 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1068 BO->getSourceRange(), BO->getOperatorLoc());
1069 }
1070 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1071 if (CE->getNumArgs() == 2) {
1072 auto Op = CE->getOperator();
1073 switch (Op) {
1074 case OO_Greater:
1075 case OO_GreaterEqual:
1076 case OO_Less:
1077 case OO_LessEqual:
1078 if (GetInitVarDecl(CE->getArg(0)) == Var)
1079 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1080 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1081 CE->getOperatorLoc());
1082 if (GetInitVarDecl(CE->getArg(1)) == Var)
1083 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1084 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1085 CE->getOperatorLoc());
1086 break;
1087 default:
1088 break;
1089 }
1090 }
1091 }
1092 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1093 << S->getSourceRange() << Var;
1094 return true;
1095}
1096
1097bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1098 // RHS of canonical loop form increment can be:
1099 // var + incr
1100 // incr + var
1101 // var - incr
1102 //
1103 RHS = RHS->IgnoreParenImpCasts();
1104 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1105 if (BO->isAdditiveOp()) {
1106 bool IsAdd = BO->getOpcode() == BO_Add;
1107 if (GetInitVarDecl(BO->getLHS()) == Var)
1108 return SetStep(BO->getRHS(), !IsAdd);
1109 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1110 return SetStep(BO->getLHS(), false);
1111 }
1112 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1113 bool IsAdd = CE->getOperator() == OO_Plus;
1114 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1115 if (GetInitVarDecl(CE->getArg(0)) == Var)
1116 return SetStep(CE->getArg(1), !IsAdd);
1117 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1118 return SetStep(CE->getArg(0), false);
1119 }
1120 }
1121 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1122 << RHS->getSourceRange() << Var;
1123 return true;
1124}
1125
1126bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1127 // Check incr-expr for canonical loop form and return true if it
1128 // does not conform.
1129 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1130 // ++var
1131 // var++
1132 // --var
1133 // var--
1134 // var += incr
1135 // var -= incr
1136 // var = var + incr
1137 // var = incr + var
1138 // var = var - incr
1139 //
1140 if (!S) {
1141 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1142 return true;
1143 }
1144 S = S->IgnoreParens();
1145 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1146 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1147 return SetStep(
1148 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1149 (UO->isDecrementOp() ? -1 : 1)).get(),
1150 false);
1151 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1152 switch (BO->getOpcode()) {
1153 case BO_AddAssign:
1154 case BO_SubAssign:
1155 if (GetInitVarDecl(BO->getLHS()) == Var)
1156 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1157 break;
1158 case BO_Assign:
1159 if (GetInitVarDecl(BO->getLHS()) == Var)
1160 return CheckIncRHS(BO->getRHS());
1161 break;
1162 default:
1163 break;
1164 }
1165 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1166 switch (CE->getOperator()) {
1167 case OO_PlusPlus:
1168 case OO_MinusMinus:
1169 if (GetInitVarDecl(CE->getArg(0)) == Var)
1170 return SetStep(
1171 SemaRef.ActOnIntegerConstant(
1172 CE->getLocStart(),
1173 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1174 false);
1175 break;
1176 case OO_PlusEqual:
1177 case OO_MinusEqual:
1178 if (GetInitVarDecl(CE->getArg(0)) == Var)
1179 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1180 break;
1181 case OO_Equal:
1182 if (GetInitVarDecl(CE->getArg(0)) == Var)
1183 return CheckIncRHS(CE->getArg(1));
1184 break;
1185 default:
1186 break;
1187 }
1188 }
1189 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1190 << S->getSourceRange() << Var;
1191 return true;
1192}
1193}
1194
1195/// \brief Called on a for stmt to check and extract its iteration space
1196/// for further processing (such as collapsing).
1197static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
1198 Sema &SemaRef, DSAStackTy &DSA) {
1199 // OpenMP [2.6, Canonical Loop Form]
1200 // for (init-expr; test-expr; incr-expr) structured-block
1201 auto For = dyn_cast_or_null<ForStmt>(S);
1202 if (!For) {
1203 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
1204 << getOpenMPDirectiveName(DKind);
1205 return true;
1206 }
1207 assert(For->getBody());
1208
1209 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1210
1211 // Check init.
1212 Stmt *Init = For->getInit();
1213 if (ISC.CheckInit(Init)) {
1214 return true;
1215 }
1216
1217 bool HasErrors = false;
1218
1219 // Check loop variable's type.
1220 VarDecl *Var = ISC.GetLoopVar();
1221
1222 // OpenMP [2.6, Canonical Loop Form]
1223 // Var is one of the following:
1224 // A variable of signed or unsigned integer type.
1225 // For C++, a variable of a random access iterator type.
1226 // For C, a variable of a pointer type.
1227 QualType VarType = Var->getType();
1228 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1229 !VarType->isPointerType() &&
1230 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1231 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1232 << SemaRef.getLangOpts().CPlusPlus;
1233 HasErrors = true;
1234 }
1235
1236 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1237 // a Construct, C/C++].
1238 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1239 // parallel for construct may be listed in a private or lastprivate clause.
1240 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
1241 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_linear &&
1242 DVar.CKind != OMPC_threadprivate) {
1243 // The loop iteration variable in the associated for-loop of a simd
1244 // construct with just one associated for-loop may be listed in a linear
1245 // clause with a constant-linear-step that is the increment of the
1246 // associated for-loop.
1247 // FIXME: allow OMPC_lastprivate when it is ready.
1248 assert(DKind == OMPD_simd && "DSA for non-simd loop vars");
1249 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1250 << getOpenMPClauseName(DVar.CKind);
1251 if (DVar.RefExpr)
1252 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1253 << getOpenMPClauseName(DVar.CKind);
1254 else
1255 SemaRef.Diag(Var->getLocation(), diag::note_omp_predetermined_dsa)
1256 << getOpenMPClauseName(DVar.CKind);
1257 HasErrors = true;
1258 } else {
1259 // Make the loop iteration variable private by default.
1260 DSA.addDSA(Var, nullptr, OMPC_private);
1261 }
1262
1263 // Check test-expr.
1264 HasErrors |= ISC.CheckCond(For->getCond());
1265
1266 // Check incr-expr.
1267 HasErrors |= ISC.CheckInc(For->getInc());
1268
1269 if (ISC.Dependent())
1270 return HasErrors;
1271
1272 // FIXME: Build loop's iteration space representation.
1273 return HasErrors;
1274}
1275
1276/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1277/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1278/// to get the first for loop.
1279static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1280 if (IgnoreCaptured)
1281 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1282 S = CapS->getCapturedStmt();
1283 // OpenMP [2.8.1, simd construct, Restrictions]
1284 // All loops associated with the construct must be perfectly nested; that is,
1285 // there must be no intervening code nor any OpenMP directive between any two
1286 // loops.
1287 while (true) {
1288 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1289 S = AS->getSubStmt();
1290 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1291 if (CS->size() != 1)
1292 break;
1293 S = CS->body_back();
1294 } else
1295 break;
1296 }
1297 return S;
1298}
1299
1300/// \brief Called on a for stmt to check itself and nested loops (if any).
1301static bool CheckOpenMPLoop(OpenMPDirectiveKind DKind, unsigned NestedLoopCount,
1302 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA) {
1303 // This is helper routine for loop directives (e.g., 'for', 'simd',
1304 // 'for simd', etc.).
1305 assert(NestedLoopCount == 1);
1306 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1307 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
1308 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA))
1309 return true;
1310 // Move on to the next nested for loop, or to the loop body.
1311 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1312 }
1313
1314 // FIXME: Build resulting iteration space for IR generation (collapsing
1315 // iteration spaces when loop count > 1 ('collapse' clause)).
1316 return false;
1317}
1318
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001319StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001320 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001321 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001322 // In presence of clause 'collapse', it will define the nested loops number.
1323 // For now, pass default value of 1.
1324 if (CheckOpenMPLoop(OMPD_simd, 1, AStmt, *this, *DSAStack))
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001325 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001326
1327 getCurFunction()->setHasBranchProtectedScope();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001328 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001329}
1330
Alexey Bataeved09d242014-05-28 05:53:51 +00001331OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001332 SourceLocation StartLoc,
1333 SourceLocation LParenLoc,
1334 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001335 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001336 switch (Kind) {
1337 case OMPC_if:
1338 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1339 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001340 case OMPC_num_threads:
1341 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1342 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001343 case OMPC_safelen:
1344 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1345 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001346 case OMPC_collapse:
1347 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1348 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001349 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001350 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001351 case OMPC_private:
1352 case OMPC_firstprivate:
1353 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +00001354 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001355 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001356 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001357 case OMPC_threadprivate:
1358 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001359 llvm_unreachable("Clause is not allowed.");
1360 }
1361 return Res;
1362}
1363
Alexey Bataeved09d242014-05-28 05:53:51 +00001364OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001365 SourceLocation LParenLoc,
1366 SourceLocation EndLoc) {
1367 Expr *ValExpr = Condition;
1368 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1369 !Condition->isInstantiationDependent() &&
1370 !Condition->containsUnexpandedParameterPack()) {
1371 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001372 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001373 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001374 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001375
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001376 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001377 }
1378
1379 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1380}
1381
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001382ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1383 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001384 if (!Op)
1385 return ExprError();
1386
1387 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1388 public:
1389 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001390 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001391 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1392 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001393 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1394 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001395 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1396 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001397 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1398 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001399 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1400 QualType T,
1401 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001402 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1403 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001404 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1405 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001406 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001407 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001408 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001409 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1410 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001411 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1412 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001413 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1414 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001415 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001416 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001417 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001418 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1419 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001420 llvm_unreachable("conversion functions are permitted");
1421 }
1422 } ConvertDiagnoser;
1423 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1424}
1425
1426OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1427 SourceLocation StartLoc,
1428 SourceLocation LParenLoc,
1429 SourceLocation EndLoc) {
1430 Expr *ValExpr = NumThreads;
1431 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1432 !NumThreads->isInstantiationDependent() &&
1433 !NumThreads->containsUnexpandedParameterPack()) {
1434 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1435 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001436 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001437 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001438 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001439
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001440 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001441
1442 // OpenMP [2.5, Restrictions]
1443 // The num_threads expression must evaluate to a positive integer value.
1444 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001445 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1446 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001447 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1448 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001449 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001450 }
1451 }
1452
Alexey Bataeved09d242014-05-28 05:53:51 +00001453 return new (Context)
1454 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001455}
1456
Alexey Bataev62c87d22014-03-21 04:51:18 +00001457ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1458 OpenMPClauseKind CKind) {
1459 if (!E)
1460 return ExprError();
1461 if (E->isValueDependent() || E->isTypeDependent() ||
1462 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001463 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001464 llvm::APSInt Result;
1465 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1466 if (ICE.isInvalid())
1467 return ExprError();
1468 if (!Result.isStrictlyPositive()) {
1469 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1470 << getOpenMPClauseName(CKind) << E->getSourceRange();
1471 return ExprError();
1472 }
1473 return ICE;
1474}
1475
1476OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1477 SourceLocation LParenLoc,
1478 SourceLocation EndLoc) {
1479 // OpenMP [2.8.1, simd construct, Description]
1480 // The parameter of the safelen clause must be a constant
1481 // positive integer expression.
1482 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1483 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001484 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001485 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001486 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001487}
1488
Alexander Musman8bd31e62014-05-27 15:12:19 +00001489OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *Num, SourceLocation StartLoc,
1490 SourceLocation LParenLoc,
1491 SourceLocation EndLoc) {
1492 // OpenMP [2.8.1, simd construct, Description]
1493 // The parameter of the collapse clause must be a constant
1494 // positive integer expression.
1495 ExprResult NumForLoops =
1496 VerifyPositiveIntegerConstantInClause(Num, OMPC_collapse);
1497 if (NumForLoops.isInvalid())
1498 return nullptr;
1499 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001500 OMPCollapseClause(NumForLoops.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001501}
1502
Alexey Bataeved09d242014-05-28 05:53:51 +00001503OMPClause *Sema::ActOnOpenMPSimpleClause(
1504 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1505 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001506 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001507 switch (Kind) {
1508 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001509 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001510 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1511 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001512 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001513 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001514 Res = ActOnOpenMPProcBindClause(
1515 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1516 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001517 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001518 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001519 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001520 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001521 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001522 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001523 case OMPC_firstprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001524 case OMPC_shared:
Alexander Musman8dba6642014-04-22 13:09:42 +00001525 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001526 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001527 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001528 case OMPC_threadprivate:
1529 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001530 llvm_unreachable("Clause is not allowed.");
1531 }
1532 return Res;
1533}
1534
1535OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1536 SourceLocation KindKwLoc,
1537 SourceLocation StartLoc,
1538 SourceLocation LParenLoc,
1539 SourceLocation EndLoc) {
1540 if (Kind == OMPC_DEFAULT_unknown) {
1541 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001542 static_assert(OMPC_DEFAULT_unknown > 0,
1543 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001544 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001545 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001546 Values += "'";
1547 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1548 Values += "'";
1549 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001550 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001551 Values += " or ";
1552 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001553 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001554 break;
1555 default:
1556 Values += Sep;
1557 break;
1558 }
1559 }
1560 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001561 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001562 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001563 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001564 switch (Kind) {
1565 case OMPC_DEFAULT_none:
1566 DSAStack->setDefaultDSANone();
1567 break;
1568 case OMPC_DEFAULT_shared:
1569 DSAStack->setDefaultDSAShared();
1570 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001571 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001572 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001573 break;
1574 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001575 return new (Context)
1576 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001577}
1578
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001579OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1580 SourceLocation KindKwLoc,
1581 SourceLocation StartLoc,
1582 SourceLocation LParenLoc,
1583 SourceLocation EndLoc) {
1584 if (Kind == OMPC_PROC_BIND_unknown) {
1585 std::string Values;
1586 std::string Sep(", ");
1587 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1588 Values += "'";
1589 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1590 Values += "'";
1591 switch (i) {
1592 case OMPC_PROC_BIND_unknown - 2:
1593 Values += " or ";
1594 break;
1595 case OMPC_PROC_BIND_unknown - 1:
1596 break;
1597 default:
1598 Values += Sep;
1599 break;
1600 }
1601 }
1602 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001603 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001604 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001605 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001606 return new (Context)
1607 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001608}
1609
Alexey Bataeved09d242014-05-28 05:53:51 +00001610OMPClause *
1611Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind, ArrayRef<Expr *> VarList,
1612 Expr *TailExpr, SourceLocation StartLoc,
1613 SourceLocation LParenLoc,
1614 SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001615 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001616 switch (Kind) {
1617 case OMPC_private:
1618 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1619 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001620 case OMPC_firstprivate:
1621 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1622 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001623 case OMPC_shared:
1624 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1625 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001626 case OMPC_linear:
1627 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1628 ColonLoc, EndLoc);
1629 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001630 case OMPC_aligned:
1631 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
1632 ColonLoc, EndLoc);
1633 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001634 case OMPC_copyin:
1635 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1636 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001637 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001638 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001639 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001640 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001641 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001642 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001643 case OMPC_threadprivate:
1644 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001645 llvm_unreachable("Clause is not allowed.");
1646 }
1647 return Res;
1648}
1649
1650OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1651 SourceLocation StartLoc,
1652 SourceLocation LParenLoc,
1653 SourceLocation EndLoc) {
1654 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001655 for (auto &RefExpr : VarList) {
1656 assert(RefExpr && "NULL expr in OpenMP private clause.");
1657 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001658 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001659 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001660 continue;
1661 }
1662
Alexey Bataeved09d242014-05-28 05:53:51 +00001663 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001664 // OpenMP [2.1, C/C++]
1665 // A list item is a variable name.
1666 // OpenMP [2.9.3.3, Restrictions, p.1]
1667 // A variable that is part of another variable (as an array or
1668 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001669 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001670 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001671 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001672 continue;
1673 }
1674 Decl *D = DE->getDecl();
1675 VarDecl *VD = cast<VarDecl>(D);
1676
1677 QualType Type = VD->getType();
1678 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1679 // It will be analyzed later.
1680 Vars.push_back(DE);
1681 continue;
1682 }
1683
1684 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1685 // A variable that appears in a private clause must not have an incomplete
1686 // type or a reference type.
1687 if (RequireCompleteType(ELoc, Type,
1688 diag::err_omp_private_incomplete_type)) {
1689 continue;
1690 }
1691 if (Type->isReferenceType()) {
1692 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001693 << getOpenMPClauseName(OMPC_private) << Type;
1694 bool IsDecl =
1695 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1696 Diag(VD->getLocation(),
1697 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1698 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001699 continue;
1700 }
1701
1702 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1703 // A variable of class type (or array thereof) that appears in a private
1704 // clause requires an accesible, unambiguous default constructor for the
1705 // class type.
1706 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001707 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
1708 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001709 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001710 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1711 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1712 : nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001713 if (RD) {
1714 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1715 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001716 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1717 if (!CD || CheckConstructorAccess(
1718 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1719 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001720 CD->isDeleted()) {
1721 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001722 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001723 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1724 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001725 Diag(VD->getLocation(),
1726 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1727 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001728 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1729 continue;
1730 }
1731 MarkFunctionReferenced(ELoc, CD);
1732 DiagnoseUseOfDecl(CD, ELoc);
1733
1734 CXXDestructorDecl *DD = RD->getDestructor();
1735 if (DD) {
1736 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1737 DD->isDeleted()) {
1738 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001739 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001740 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1741 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001742 Diag(VD->getLocation(),
1743 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1744 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001745 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1746 continue;
1747 }
1748 MarkFunctionReferenced(ELoc, DD);
1749 DiagnoseUseOfDecl(DD, ELoc);
1750 }
1751 }
1752
Alexey Bataev758e55e2013-09-06 18:03:48 +00001753 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1754 // in a Construct]
1755 // Variables with the predetermined data-sharing attributes may not be
1756 // listed in data-sharing attributes clauses, except for the cases
1757 // listed below. For these exceptions only, listing a predetermined
1758 // variable in a data-sharing attribute clause is allowed and overrides
1759 // the variable's predetermined data-sharing attributes.
1760 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1761 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001762 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1763 << getOpenMPClauseName(OMPC_private);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001764 if (DVar.RefExpr) {
1765 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001766 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001767 } else {
1768 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001769 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001770 }
1771 continue;
1772 }
1773
1774 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001775 Vars.push_back(DE);
1776 }
1777
Alexey Bataeved09d242014-05-28 05:53:51 +00001778 if (Vars.empty())
1779 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001780
1781 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1782}
1783
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001784OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1785 SourceLocation StartLoc,
1786 SourceLocation LParenLoc,
1787 SourceLocation EndLoc) {
1788 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001789 for (auto &RefExpr : VarList) {
1790 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
1791 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001792 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001793 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001794 continue;
1795 }
1796
Alexey Bataeved09d242014-05-28 05:53:51 +00001797 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001798 // OpenMP [2.1, C/C++]
1799 // A list item is a variable name.
1800 // OpenMP [2.9.3.3, Restrictions, p.1]
1801 // A variable that is part of another variable (as an array or
1802 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001803 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001804 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001805 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001806 continue;
1807 }
1808 Decl *D = DE->getDecl();
1809 VarDecl *VD = cast<VarDecl>(D);
1810
1811 QualType Type = VD->getType();
1812 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1813 // It will be analyzed later.
1814 Vars.push_back(DE);
1815 continue;
1816 }
1817
1818 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1819 // A variable that appears in a private clause must not have an incomplete
1820 // type or a reference type.
1821 if (RequireCompleteType(ELoc, Type,
1822 diag::err_omp_firstprivate_incomplete_type)) {
1823 continue;
1824 }
1825 if (Type->isReferenceType()) {
1826 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001827 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1828 bool IsDecl =
1829 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1830 Diag(VD->getLocation(),
1831 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1832 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001833 continue;
1834 }
1835
1836 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1837 // A variable of class type (or array thereof) that appears in a private
1838 // clause requires an accesible, unambiguous copy constructor for the
1839 // class type.
1840 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001841 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1842 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1843 : nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001844 if (RD) {
1845 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1846 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001847 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
1848 if (!CD || CheckConstructorAccess(
1849 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
1850 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001851 CD->isDeleted()) {
1852 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001853 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001854 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1855 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001856 Diag(VD->getLocation(),
1857 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1858 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001859 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1860 continue;
1861 }
1862 MarkFunctionReferenced(ELoc, CD);
1863 DiagnoseUseOfDecl(CD, ELoc);
1864
1865 CXXDestructorDecl *DD = RD->getDestructor();
1866 if (DD) {
1867 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1868 DD->isDeleted()) {
1869 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001870 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001871 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1872 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001873 Diag(VD->getLocation(),
1874 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1875 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001876 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1877 continue;
1878 }
1879 MarkFunctionReferenced(ELoc, DD);
1880 DiagnoseUseOfDecl(DD, ELoc);
1881 }
1882 }
1883
1884 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1885 // variable and it was checked already.
1886 if (StartLoc.isValid() && EndLoc.isValid()) {
1887 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1888 Type = Type.getNonReferenceType().getCanonicalType();
1889 bool IsConstant = Type.isConstant(Context);
1890 Type = Context.getBaseElementType(Type);
1891 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1892 // A list item that specifies a given variable may not appear in more
1893 // than one clause on the same directive, except that a variable may be
1894 // specified in both firstprivate and lastprivate clauses.
1895 // TODO: add processing for lastprivate.
1896 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1897 DVar.RefExpr) {
1898 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001899 << getOpenMPClauseName(DVar.CKind)
1900 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001901 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001902 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001903 continue;
1904 }
1905
1906 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1907 // in a Construct]
1908 // Variables with the predetermined data-sharing attributes may not be
1909 // listed in data-sharing attributes clauses, except for the cases
1910 // listed below. For these exceptions only, listing a predetermined
1911 // variable in a data-sharing attribute clause is allowed and overrides
1912 // the variable's predetermined data-sharing attributes.
1913 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1914 // in a Construct, C/C++, p.2]
1915 // Variables with const-qualified type having no mutable member may be
1916 // listed in a firstprivate clause, even if they are static data members.
1917 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1918 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1919 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001920 << getOpenMPClauseName(DVar.CKind)
1921 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001922 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001923 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001924 continue;
1925 }
1926
1927 // OpenMP [2.9.3.4, Restrictions, p.2]
1928 // A list item that is private within a parallel region must not appear
1929 // in a firstprivate clause on a worksharing construct if any of the
1930 // worksharing regions arising from the worksharing construct ever bind
1931 // to any of the parallel regions arising from the parallel construct.
1932 // OpenMP [2.9.3.4, Restrictions, p.3]
1933 // A list item that appears in a reduction clause of a parallel construct
1934 // must not appear in a firstprivate clause on a worksharing or task
1935 // construct if any of the worksharing or task regions arising from the
1936 // worksharing or task construct ever bind to any of the parallel regions
1937 // arising from the parallel construct.
1938 // OpenMP [2.9.3.4, Restrictions, p.4]
1939 // A list item that appears in a reduction clause in worksharing
1940 // construct must not appear in a firstprivate clause in a task construct
1941 // encountered during execution of any of the worksharing regions arising
1942 // from the worksharing construct.
1943 // TODO:
1944 }
1945
1946 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1947 Vars.push_back(DE);
1948 }
1949
Alexey Bataeved09d242014-05-28 05:53:51 +00001950 if (Vars.empty())
1951 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001952
1953 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1954 Vars);
1955}
1956
Alexey Bataev758e55e2013-09-06 18:03:48 +00001957OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1958 SourceLocation StartLoc,
1959 SourceLocation LParenLoc,
1960 SourceLocation EndLoc) {
1961 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001962 for (auto &RefExpr : VarList) {
1963 assert(RefExpr && "NULL expr in OpenMP shared clause.");
1964 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001965 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001966 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001967 continue;
1968 }
1969
Alexey Bataeved09d242014-05-28 05:53:51 +00001970 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001971 // OpenMP [2.1, C/C++]
1972 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00001973 // OpenMP [2.14.3.2, Restrictions, p.1]
1974 // A variable that is part of another variable (as an array or structure
1975 // element) cannot appear in a shared unless it is a static data member
1976 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00001977 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001978 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001979 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001980 continue;
1981 }
1982 Decl *D = DE->getDecl();
1983 VarDecl *VD = cast<VarDecl>(D);
1984
1985 QualType Type = VD->getType();
1986 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1987 // It will be analyzed later.
1988 Vars.push_back(DE);
1989 continue;
1990 }
1991
1992 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1993 // in a Construct]
1994 // Variables with the predetermined data-sharing attributes may not be
1995 // listed in data-sharing attributes clauses, except for the cases
1996 // listed below. For these exceptions only, listing a predetermined
1997 // variable in a data-sharing attribute clause is allowed and overrides
1998 // the variable's predetermined data-sharing attributes.
1999 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002000 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2001 DVar.RefExpr) {
2002 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2003 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002004 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002005 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002006 continue;
2007 }
2008
2009 DSAStack->addDSA(VD, DE, OMPC_shared);
2010 Vars.push_back(DE);
2011 }
2012
Alexey Bataeved09d242014-05-28 05:53:51 +00002013 if (Vars.empty())
2014 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002015
2016 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2017}
2018
Alexander Musman8dba6642014-04-22 13:09:42 +00002019OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2020 SourceLocation StartLoc,
2021 SourceLocation LParenLoc,
2022 SourceLocation ColonLoc,
2023 SourceLocation EndLoc) {
2024 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002025 for (auto &RefExpr : VarList) {
2026 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2027 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002028 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002029 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002030 continue;
2031 }
2032
2033 // OpenMP [2.14.3.7, linear clause]
2034 // A list item that appears in a linear clause is subject to the private
2035 // clause semantics described in Section 2.14.3.3 on page 159 except as
2036 // noted. In addition, the value of the new list item on each iteration
2037 // of the associated loop(s) corresponds to the value of the original
2038 // list item before entering the construct plus the logical number of
2039 // the iteration times linear-step.
2040
Alexey Bataeved09d242014-05-28 05:53:51 +00002041 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00002042 // OpenMP [2.1, C/C++]
2043 // A list item is a variable name.
2044 // OpenMP [2.14.3.3, Restrictions, p.1]
2045 // A variable that is part of another variable (as an array or
2046 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002047 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002048 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002049 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00002050 continue;
2051 }
2052
2053 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2054
2055 // OpenMP [2.14.3.7, linear clause]
2056 // A list-item cannot appear in more than one linear clause.
2057 // A list-item that appears in a linear clause cannot appear in any
2058 // other data-sharing attribute clause.
2059 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2060 if (DVar.RefExpr) {
2061 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2062 << getOpenMPClauseName(OMPC_linear);
2063 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2064 << getOpenMPClauseName(DVar.CKind);
2065 continue;
2066 }
2067
2068 QualType QType = VD->getType();
2069 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2070 // It will be analyzed later.
2071 Vars.push_back(DE);
2072 continue;
2073 }
2074
2075 // A variable must not have an incomplete type or a reference type.
2076 if (RequireCompleteType(ELoc, QType,
2077 diag::err_omp_linear_incomplete_type)) {
2078 continue;
2079 }
2080 if (QType->isReferenceType()) {
2081 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2082 << getOpenMPClauseName(OMPC_linear) << QType;
2083 bool IsDecl =
2084 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2085 Diag(VD->getLocation(),
2086 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2087 << VD;
2088 continue;
2089 }
2090
2091 // A list item must not be const-qualified.
2092 if (QType.isConstant(Context)) {
2093 Diag(ELoc, diag::err_omp_const_variable)
2094 << getOpenMPClauseName(OMPC_linear);
2095 bool IsDecl =
2096 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2097 Diag(VD->getLocation(),
2098 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2099 << VD;
2100 continue;
2101 }
2102
2103 // A list item must be of integral or pointer type.
2104 QType = QType.getUnqualifiedType().getCanonicalType();
2105 const Type *Ty = QType.getTypePtrOrNull();
2106 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
2107 !Ty->isPointerType())) {
2108 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
2109 bool IsDecl =
2110 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2111 Diag(VD->getLocation(),
2112 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2113 << VD;
2114 continue;
2115 }
2116
2117 DSAStack->addDSA(VD, DE, OMPC_linear);
2118 Vars.push_back(DE);
2119 }
2120
2121 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002122 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00002123
2124 Expr *StepExpr = Step;
2125 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2126 !Step->isInstantiationDependent() &&
2127 !Step->containsUnexpandedParameterPack()) {
2128 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002129 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00002130 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002131 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002132 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00002133
2134 // Warn about zero linear step (it would be probably better specified as
2135 // making corresponding variables 'const').
2136 llvm::APSInt Result;
2137 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
2138 !Result.isNegative() && !Result.isStrictlyPositive())
2139 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
2140 << (Vars.size() > 1);
2141 }
2142
2143 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
2144 Vars, StepExpr);
2145}
2146
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002147OMPClause *Sema::ActOnOpenMPAlignedClause(
2148 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
2149 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
2150
2151 SmallVector<Expr *, 8> Vars;
2152 for (auto &RefExpr : VarList) {
2153 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
2154 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2155 // It will be analyzed later.
2156 Vars.push_back(RefExpr);
2157 continue;
2158 }
2159
2160 SourceLocation ELoc = RefExpr->getExprLoc();
2161 // OpenMP [2.1, C/C++]
2162 // A list item is a variable name.
2163 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
2164 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2165 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2166 continue;
2167 }
2168
2169 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2170
2171 // OpenMP [2.8.1, simd construct, Restrictions]
2172 // The type of list items appearing in the aligned clause must be
2173 // array, pointer, reference to array, or reference to pointer.
2174 QualType QType = DE->getType()
2175 .getNonReferenceType()
2176 .getUnqualifiedType()
2177 .getCanonicalType();
2178 const Type *Ty = QType.getTypePtrOrNull();
2179 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
2180 !Ty->isPointerType())) {
2181 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
2182 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
2183 bool IsDecl =
2184 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2185 Diag(VD->getLocation(),
2186 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2187 << VD;
2188 continue;
2189 }
2190
2191 // OpenMP [2.8.1, simd construct, Restrictions]
2192 // A list-item cannot appear in more than one aligned clause.
2193 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
2194 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
2195 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
2196 << getOpenMPClauseName(OMPC_aligned);
2197 continue;
2198 }
2199
2200 Vars.push_back(DE);
2201 }
2202
2203 // OpenMP [2.8.1, simd construct, Description]
2204 // The parameter of the aligned clause, alignment, must be a constant
2205 // positive integer expression.
2206 // If no optional parameter is specified, implementation-defined default
2207 // alignments for SIMD instructions on the target platforms are assumed.
2208 if (Alignment != nullptr) {
2209 ExprResult AlignResult =
2210 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
2211 if (AlignResult.isInvalid())
2212 return nullptr;
2213 Alignment = AlignResult.get();
2214 }
2215 if (Vars.empty())
2216 return nullptr;
2217
2218 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
2219 EndLoc, Vars, Alignment);
2220}
2221
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002222OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
2223 SourceLocation StartLoc,
2224 SourceLocation LParenLoc,
2225 SourceLocation EndLoc) {
2226 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002227 for (auto &RefExpr : VarList) {
2228 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
2229 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002230 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002231 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002232 continue;
2233 }
2234
Alexey Bataeved09d242014-05-28 05:53:51 +00002235 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002236 // OpenMP [2.1, C/C++]
2237 // A list item is a variable name.
2238 // OpenMP [2.14.4.1, Restrictions, p.1]
2239 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00002240 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002241 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002242 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002243 continue;
2244 }
2245
2246 Decl *D = DE->getDecl();
2247 VarDecl *VD = cast<VarDecl>(D);
2248
2249 QualType Type = VD->getType();
2250 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2251 // It will be analyzed later.
2252 Vars.push_back(DE);
2253 continue;
2254 }
2255
2256 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
2257 // A list item that appears in a copyin clause must be threadprivate.
2258 if (!DSAStack->isThreadPrivate(VD)) {
2259 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00002260 << getOpenMPClauseName(OMPC_copyin)
2261 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002262 continue;
2263 }
2264
2265 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
2266 // A variable of class type (or array thereof) that appears in a
2267 // copyin clause requires an accesible, unambiguous copy assignment
2268 // operator for the class type.
2269 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002270 CXXRecordDecl *RD =
2271 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002272 if (RD) {
2273 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2274 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataeved09d242014-05-28 05:53:51 +00002275 if (!MD || CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002276 MD->isDeleted()) {
2277 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002278 << getOpenMPClauseName(OMPC_copyin) << 2;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002279 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2280 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002281 Diag(VD->getLocation(),
2282 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2283 << VD;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002284 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2285 continue;
2286 }
2287 MarkFunctionReferenced(ELoc, MD);
2288 DiagnoseUseOfDecl(MD, ELoc);
2289 }
2290
2291 DSAStack->addDSA(VD, DE, OMPC_copyin);
2292 Vars.push_back(DE);
2293 }
2294
Alexey Bataeved09d242014-05-28 05:53:51 +00002295 if (Vars.empty())
2296 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002297
2298 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2299}
2300
Alexey Bataev758e55e2013-09-06 18:03:48 +00002301#undef DSAStack