blob: 56903ceee3eacfd6fe8ec6dab71251a9c5759b8d [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};
Alexey Bataevf29276e2014-06-18 04:14:57 +000042template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000043 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000044 bool operator()(T Kind) {
45 for (auto KindEl : Arr)
46 if (KindEl == Kind)
47 return true;
48 return false;
49 }
50
51private:
52 ArrayRef<T> Arr;
53};
Alexey Bataev23b69422014-06-18 07:08:49 +000054struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000055 MatchesAlways() {}
Alexey Bataev23b69422014-06-18 07:08:49 +000056 template <class T>
Alexey Bataevf29276e2014-06-18 04:14:57 +000057 bool operator()(T) { return true; }
58};
59
60typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
61typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000062
63/// \brief Stack for tracking declarations used in OpenMP directives and
64/// clauses and their data-sharing attributes.
65class DSAStackTy {
66public:
67 struct DSAVarData {
68 OpenMPDirectiveKind DKind;
69 OpenMPClauseKind CKind;
70 DeclRefExpr *RefExpr;
Alexander Musmancb7f9c42014-05-15 13:04:49 +000071 DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000072 };
Alexey Bataeved09d242014-05-28 05:53:51 +000073
Alexey Bataev758e55e2013-09-06 18:03:48 +000074private:
75 struct DSAInfo {
76 OpenMPClauseKind Attributes;
77 DeclRefExpr *RefExpr;
78 };
79 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000080 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000081
82 struct SharingMapTy {
83 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000084 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000085 DefaultDataSharingAttributes DefaultAttr;
86 OpenMPDirectiveKind Directive;
87 DeclarationNameInfo DirectiveName;
88 Scope *CurScope;
Alexey Bataeved09d242014-05-28 05:53:51 +000089 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 Scope *CurScope)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
92 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope) {
93 }
Alexey Bataev758e55e2013-09-06 18:03:48 +000094 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +000095 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
96 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000097 };
98
99 typedef SmallVector<SharingMapTy, 64> StackTy;
100
101 /// \brief Stack of used declaration and their data-sharing attributes.
102 StackTy Stack;
103 Sema &Actions;
104
105 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
106
107 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000108
109 /// \brief Checks if the variable is a local for OpenMP region.
110 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000111
Alexey Bataev758e55e2013-09-06 18:03:48 +0000112public:
Alexey Bataeved09d242014-05-28 05:53:51 +0000113 explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114
115 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
116 Scope *CurScope) {
117 Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
118 }
119
120 void pop() {
121 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
122 Stack.pop_back();
123 }
124
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000125 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000126 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000127 /// for diagnostics.
128 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
129
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130 /// \brief Adds explicit data sharing attribute to the specified declaration.
131 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
132
Alexey Bataev758e55e2013-09-06 18:03:48 +0000133 /// \brief Returns data sharing attributes from top of the stack for the
134 /// specified declaration.
135 DSAVarData getTopDSA(VarDecl *D);
136 /// \brief Returns data-sharing attributes for the specified declaration.
137 DSAVarData getImplicitDSA(VarDecl *D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000138 /// \brief Checks if the specified variables has data-sharing attributes which
139 /// match specified \a CPred predicate in any directive which matches \a DPred
140 /// predicate.
141 template <class ClausesPredicate, class DirectivesPredicate>
142 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
143 DirectivesPredicate DPred);
144 /// \brief Checks if the specified variables has data-sharing attributes which
145 /// match specified \a CPred predicate in any innermost directive which
146 /// matches \a DPred predicate.
147 template <class ClausesPredicate, class DirectivesPredicate>
148 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
149 DirectivesPredicate DPred);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000150
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151 /// \brief Returns currently analyzed directive.
152 OpenMPDirectiveKind getCurrentDirective() const {
153 return Stack.back().Directive;
154 }
155
156 /// \brief Set default data sharing attribute to none.
157 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
158 /// \brief Set default data sharing attribute to shared.
159 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
160
161 DefaultDataSharingAttributes getDefaultDSA() const {
162 return Stack.back().DefaultAttr;
163 }
164
Alexey Bataevf29276e2014-06-18 04:14:57 +0000165 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000166 bool isThreadPrivate(VarDecl *D) {
167 DSAVarData DVar = getTopDSA(D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000168 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000169 }
170
171 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000172 Scope *getCurScope() { return Stack.back().CurScope; }
173};
Alexey Bataeved09d242014-05-28 05:53:51 +0000174} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175
176DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
177 VarDecl *D) {
178 DSAVarData DVar;
179 if (Iter == Stack.rend() - 1) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000180 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
181 // in a region but not in construct]
182 // File-scope or namespace-scope variables referenced in called routines
183 // in the region are shared unless they appear in a threadprivate
184 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000185 if (!D->isFunctionOrMethodVarDecl())
186 DVar.CKind = OMPC_shared;
187
188 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
189 // in a region but not in construct]
190 // Variables with static storage duration that are declared in called
191 // routines in the region are shared.
192 if (D->hasGlobalStorage())
193 DVar.CKind = OMPC_shared;
194
Alexey Bataev758e55e2013-09-06 18:03:48 +0000195 return DVar;
196 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000197
Alexey Bataev758e55e2013-09-06 18:03:48 +0000198 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000199 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
200 // in a Construct, C/C++, predetermined, p.1]
201 // Variables with automatic storage duration that are declared in a scope
202 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000203 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
204 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
205 DVar.CKind = OMPC_private;
206 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000207 }
208
Alexey Bataev758e55e2013-09-06 18:03:48 +0000209 // Explicitly specified attributes and local variables with predetermined
210 // attributes.
211 if (Iter->SharingMap.count(D)) {
212 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
213 DVar.CKind = Iter->SharingMap[D].Attributes;
214 return DVar;
215 }
216
217 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
218 // in a Construct, C/C++, implicitly determined, p.1]
219 // In a parallel or task construct, the data-sharing attributes of these
220 // variables are determined by the default clause, if present.
221 switch (Iter->DefaultAttr) {
222 case DSA_shared:
223 DVar.CKind = OMPC_shared;
224 return DVar;
225 case DSA_none:
226 return DVar;
227 case DSA_unspecified:
228 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
229 // in a Construct, implicitly determined, p.2]
230 // In a parallel construct, if no default clause is present, these
231 // variables are shared.
232 if (DVar.DKind == OMPD_parallel) {
233 DVar.CKind = OMPC_shared;
234 return DVar;
235 }
236
237 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
238 // in a Construct, implicitly determined, p.4]
239 // In a task construct, if no default clause is present, a variable that in
240 // the enclosing context is determined to be shared by all implicit tasks
241 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000242 if (DVar.DKind == OMPD_task) {
243 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000244 for (StackTy::reverse_iterator I = std::next(Iter),
245 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000247 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
248 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000249 // in a Construct, implicitly determined, p.6]
250 // In a task construct, if no default clause is present, a variable
251 // whose data-sharing attribute is not determined by the rules above is
252 // firstprivate.
253 DVarTemp = getDSA(I, D);
254 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000255 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000256 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000257 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258 return DVar;
259 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000260 if (I->Directive == OMPD_parallel)
261 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000262 }
263 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000265 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000266 return DVar;
267 }
268 }
269 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
270 // in a Construct, implicitly determined, p.3]
271 // For constructs other than task, if no default clause is present, these
272 // variables inherit their data-sharing attributes from the enclosing
273 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000274 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000275}
276
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000277DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
278 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
279 auto It = Stack.back().AlignedMap.find(D);
280 if (It == Stack.back().AlignedMap.end()) {
281 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
282 Stack.back().AlignedMap[D] = NewDE;
283 return nullptr;
284 } else {
285 assert(It->second && "Unexpected nullptr expr in the aligned map");
286 return It->second;
287 }
288 return nullptr;
289}
290
Alexey Bataev758e55e2013-09-06 18:03:48 +0000291void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
292 if (A == OMPC_threadprivate) {
293 Stack[0].SharingMap[D].Attributes = A;
294 Stack[0].SharingMap[D].RefExpr = E;
295 } else {
296 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
297 Stack.back().SharingMap[D].Attributes = A;
298 Stack.back().SharingMap[D].RefExpr = E;
299 }
300}
301
Alexey Bataeved09d242014-05-28 05:53:51 +0000302bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000303 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000304 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000305 Scope *TopScope = nullptr;
Fraser Cormack111023c2014-04-15 08:59:09 +0000306 while (I != E && I->Directive != OMPD_parallel) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000307 ++I;
308 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000309 if (I == E)
310 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000311 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000312 Scope *CurScope = getCurScope();
313 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000314 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000315 }
316 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000317 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000318 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000319}
320
321DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
322 DSAVarData DVar;
323
324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
325 // in a Construct, C/C++, predetermined, p.1]
326 // Variables appearing in threadprivate directives are threadprivate.
327 if (D->getTLSKind() != VarDecl::TLS_None) {
328 DVar.CKind = OMPC_threadprivate;
329 return DVar;
330 }
331 if (Stack[0].SharingMap.count(D)) {
332 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
333 DVar.CKind = OMPC_threadprivate;
334 return DVar;
335 }
336
337 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
338 // in a Construct, C/C++, predetermined, p.1]
339 // Variables with automatic storage duration that are declared in a scope
340 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000341 OpenMPDirectiveKind Kind = getCurrentDirective();
342 if (Kind != OMPD_parallel) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000343 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000344 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000345 DVar.CKind = OMPC_private;
346 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000347 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000348 }
349
350 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
351 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000352 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000353 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000354 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000355 // listed in a firstprivate clause, even if they are static data members.
356 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
Alexey Bataev23b69422014-06-18 07:08:49 +0000357 MatchesAlways());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000358 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 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
366 bool IsConstant = Type.isConstant(Actions.getASTContext());
367 while (Type->isArrayType()) {
368 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
369 Type = ElemType.getNonReferenceType().getCanonicalType();
370 }
371 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
372 // in a Construct, C/C++, predetermined, p.6]
373 // Variables with const qualified type having no mutable member are
374 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000375 CXXRecordDecl *RD =
376 Actions.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377 if (IsConstant &&
378 !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
379 // Variables with const-qualified type having no mutable member may be
380 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000381 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
Alexey Bataev23b69422014-06-18 07:08:49 +0000382 MatchesAlways());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000383 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
384 return DVar;
385
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386 DVar.CKind = OMPC_shared;
387 return DVar;
388 }
389
390 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
391 // in a Construct, C/C++, predetermined, p.7]
392 // Variables with static storage duration that are declared in a scope
393 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000394 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000395 DVar.CKind = OMPC_shared;
396 return DVar;
397 }
398
399 // Explicitly specified attributes and local variables with predetermined
400 // attributes.
401 if (Stack.back().SharingMap.count(D)) {
402 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
403 DVar.CKind = Stack.back().SharingMap[D].Attributes;
404 }
405
406 return DVar;
407}
408
409DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000410 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000411}
412
Alexey Bataevf29276e2014-06-18 04:14:57 +0000413template <class ClausesPredicate, class DirectivesPredicate>
414DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
415 DirectivesPredicate DPred) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000416 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
417 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000418 I != E; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000419 if (!DPred(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000420 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000421 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000422 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000423 return DVar;
424 }
425 return DSAVarData();
426}
427
Alexey Bataevf29276e2014-06-18 04:14:57 +0000428template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataevc5e02582014-06-16 07:08:35 +0000429DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(VarDecl *D,
Alexey Bataevf29276e2014-06-18 04:14:57 +0000430 ClausesPredicate CPred,
431 DirectivesPredicate DPred) {
Alexey Bataevc5e02582014-06-16 07:08:35 +0000432 for (auto I = Stack.rbegin(), EE = std::prev(Stack.rend()); I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000433 if (!DPred(I->Directive))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000434 continue;
435 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000436 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000437 return DVar;
438 return DSAVarData();
439 }
440 return DSAVarData();
441}
442
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443void Sema::InitDataSharingAttributesStack() {
444 VarDataSharingAttributesStack = new DSAStackTy(*this);
445}
446
447#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
448
Alexey Bataeved09d242014-05-28 05:53:51 +0000449void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450
451void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
452 const DeclarationNameInfo &DirName,
453 Scope *CurScope) {
454 DSAStack->push(DKind, DirName, CurScope);
455 PushExpressionEvaluationContext(PotentiallyEvaluated);
456}
457
458void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000459 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
460 // A variable of class type (or array thereof) that appears in a lastprivate
461 // clause requires an accessible, unambiguous default constructor for the
462 // class type, unless the list item is also specified in a firstprivate
463 // clause.
464 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
465 for (auto C : D->clauses()) {
466 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
467 for (auto VarRef : Clause->varlists()) {
468 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
469 continue;
470 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
471 auto DVar = DSAStack->getTopDSA(VD);
472 if (DVar.CKind == OMPC_lastprivate) {
473 SourceLocation ELoc = VarRef->getExprLoc();
474 auto Type = VarRef->getType();
475 if (Type->isArrayType())
476 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
477 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000478 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
479 // FIXME This code must be replaced by actual constructing of the
480 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000481 if (RD) {
482 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
483 PartialDiagnostic PD =
484 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
485 if (!CD ||
486 CheckConstructorAccess(
487 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
488 CD->getAccess(), PD) == AR_inaccessible ||
489 CD->isDeleted()) {
490 Diag(ELoc, diag::err_omp_required_method)
491 << getOpenMPClauseName(OMPC_lastprivate) << 0;
492 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
493 VarDecl::DeclarationOnly;
494 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
495 : diag::note_defined_here)
496 << VD;
497 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
498 continue;
499 }
500 MarkFunctionReferenced(ELoc, CD);
501 DiagnoseUseOfDecl(CD, ELoc);
502 }
503 }
504 }
505 }
506 }
507 }
508
Alexey Bataev758e55e2013-09-06 18:03:48 +0000509 DSAStack->pop();
510 DiscardCleanupsInEvaluationContext();
511 PopExpressionEvaluationContext();
512}
513
Alexey Bataeva769e072013-03-22 06:34:35 +0000514namespace {
515
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000516class VarDeclFilterCCC : public CorrectionCandidateCallback {
517private:
518 Sema &Actions;
Alexey Bataeved09d242014-05-28 05:53:51 +0000519
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000520public:
Alexey Bataev23b69422014-06-18 07:08:49 +0000521 explicit VarDeclFilterCCC(Sema &S) : Actions(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000522 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000523 NamedDecl *ND = Candidate.getCorrectionDecl();
524 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
525 return VD->hasGlobalStorage() &&
526 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
527 Actions.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000528 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000529 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000530 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000531};
Alexey Bataeved09d242014-05-28 05:53:51 +0000532} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000533
534ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
535 CXXScopeSpec &ScopeSpec,
536 const DeclarationNameInfo &Id) {
537 LookupResult Lookup(*this, Id, LookupOrdinaryName);
538 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
539
540 if (Lookup.isAmbiguous())
541 return ExprError();
542
543 VarDecl *VD;
544 if (!Lookup.isSingleResult()) {
545 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000546 if (TypoCorrection Corrected =
547 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
548 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000549 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000550 PDiag(Lookup.empty()
551 ? diag::err_undeclared_var_use_suggest
552 : diag::err_omp_expected_var_arg_suggest)
553 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000554 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000555 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000556 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
557 : diag::err_omp_expected_var_arg)
558 << Id.getName();
559 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000560 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000561 } else {
562 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000563 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000564 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
565 return ExprError();
566 }
567 }
568 Lookup.suppressDiagnostics();
569
570 // OpenMP [2.9.2, Syntax, C/C++]
571 // Variables must be file-scope, namespace-scope, or static block-scope.
572 if (!VD->hasGlobalStorage()) {
573 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000574 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
575 bool IsDecl =
576 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000577 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000578 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
579 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000580 return ExprError();
581 }
582
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000583 VarDecl *CanonicalVD = VD->getCanonicalDecl();
584 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000585 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
586 // A threadprivate directive for file-scope variables must appear outside
587 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000588 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
589 !getCurLexicalContext()->isTranslationUnit()) {
590 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000591 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
592 bool IsDecl =
593 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
594 Diag(VD->getLocation(),
595 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
596 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000597 return ExprError();
598 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000599 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
600 // A threadprivate directive for static class member variables must appear
601 // in the class definition, in the same scope in which the member
602 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000603 if (CanonicalVD->isStaticDataMember() &&
604 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
605 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000606 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
607 bool IsDecl =
608 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
609 Diag(VD->getLocation(),
610 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
611 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000612 return ExprError();
613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
615 // A threadprivate directive for namespace-scope variables must appear
616 // outside any definition or declaration other than the namespace
617 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000618 if (CanonicalVD->getDeclContext()->isNamespace() &&
619 (!getCurLexicalContext()->isFileContext() ||
620 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
621 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000622 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
623 bool IsDecl =
624 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
625 Diag(VD->getLocation(),
626 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
627 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000628 return ExprError();
629 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000630 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
631 // A threadprivate directive for static block-scope variables must appear
632 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000633 if (CanonicalVD->isStaticLocal() && CurScope &&
634 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000635 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000636 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
637 bool IsDecl =
638 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
639 Diag(VD->getLocation(),
640 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
641 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000642 return ExprError();
643 }
644
645 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
646 // A threadprivate directive must lexically precede all references to any
647 // of the variables in its list.
648 if (VD->isUsed()) {
649 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000650 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000651 return ExprError();
652 }
653
654 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000655 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000656 return DE;
657}
658
Alexey Bataeved09d242014-05-28 05:53:51 +0000659Sema::DeclGroupPtrTy
660Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
661 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000662 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000663 CurContext->addDecl(D);
664 return DeclGroupPtrTy::make(DeclGroupRef(D));
665 }
666 return DeclGroupPtrTy();
667}
668
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000669namespace {
670class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
671 Sema &SemaRef;
672
673public:
674 bool VisitDeclRefExpr(const DeclRefExpr *E) {
675 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
676 if (VD->hasLocalStorage()) {
677 SemaRef.Diag(E->getLocStart(),
678 diag::err_omp_local_var_in_threadprivate_init)
679 << E->getSourceRange();
680 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
681 << VD << VD->getSourceRange();
682 return true;
683 }
684 }
685 return false;
686 }
687 bool VisitStmt(const Stmt *S) {
688 for (auto Child : S->children()) {
689 if (Child && Visit(Child))
690 return true;
691 }
692 return false;
693 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000694 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000695};
696} // namespace
697
Alexey Bataeved09d242014-05-28 05:53:51 +0000698OMPThreadPrivateDecl *
699Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000700 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000701 for (auto &RefExpr : VarList) {
702 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000703 VarDecl *VD = cast<VarDecl>(DE->getDecl());
704 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000705
706 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
707 // A threadprivate variable must not have an incomplete type.
708 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000709 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000710 continue;
711 }
712
713 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
714 // A threadprivate variable must not have a reference type.
715 if (VD->getType()->isReferenceType()) {
716 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000717 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
718 bool IsDecl =
719 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
720 Diag(VD->getLocation(),
721 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
722 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000723 continue;
724 }
725
Richard Smithfd3834f2013-04-13 02:43:54 +0000726 // Check if this is a TLS variable.
727 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000728 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000729 bool IsDecl =
730 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
731 Diag(VD->getLocation(),
732 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
733 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000734 continue;
735 }
736
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000737 // Check if initial value of threadprivate variable reference variable with
738 // local storage (it is not supported by runtime).
739 if (auto Init = VD->getAnyInitializer()) {
740 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000741 if (Checker.Visit(Init))
742 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000743 }
744
Alexey Bataeved09d242014-05-28 05:53:51 +0000745 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000746 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000747 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000748 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000749 if (!Vars.empty()) {
750 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
751 Vars);
752 D->setAccess(AS_public);
753 }
754 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000755}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000756
Alexey Bataev758e55e2013-09-06 18:03:48 +0000757namespace {
758class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
759 DSAStackTy *Stack;
760 Sema &Actions;
761 bool ErrorFound;
762 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000763 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataeved09d242014-05-28 05:53:51 +0000764
Alexey Bataev758e55e2013-09-06 18:03:48 +0000765public:
766 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000767 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000768 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000769 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
770 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000771
772 SourceLocation ELoc = E->getExprLoc();
773
774 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
775 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
776 if (DVar.CKind != OMPC_unknown) {
777 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000778 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000779 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000780 return;
781 }
782 // The default(none) clause requires that each variable that is referenced
783 // in the construct, and does not have a predetermined data-sharing
784 // attribute, must have its data-sharing attribute explicitly determined
785 // by being listed in a data-sharing attribute clause.
786 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataevf29276e2014-06-18 04:14:57 +0000787 (isOpenMPParallelDirective(DKind) || DKind == OMPD_task)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000788 ErrorFound = true;
789 Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
790 return;
791 }
792
793 // OpenMP [2.9.3.6, Restrictions, p.2]
794 // A list item that appears in a reduction clause of the innermost
795 // enclosing worksharing or parallel construct may not be accessed in an
796 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000797 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev23b69422014-06-18 07:08:49 +0000798 MatchesAlways());
Alexey Bataevc5e02582014-06-16 07:08:35 +0000799 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
800 ErrorFound = true;
801 Actions.Diag(ELoc, diag::err_omp_reduction_in_task);
802 if (DVar.RefExpr) {
803 Actions.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
804 << getOpenMPClauseName(OMPC_reduction);
805 }
806 return;
807 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000808
809 // Define implicit data-sharing attributes for task.
810 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000811 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
812 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000813 }
814 }
815 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000816 for (auto C : S->clauses())
817 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000818 for (StmtRange R = C->children(); R; ++R)
819 if (Stmt *Child = *R)
820 Visit(Child);
821 }
822 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000823 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
824 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000825 if (Stmt *Child = *I)
826 if (!isa<OMPExecutableDirective>(Child))
827 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000828 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000829
830 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000831 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000832
833 DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
Alexey Bataeved09d242014-05-28 05:53:51 +0000834 : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000835};
Alexey Bataeved09d242014-05-28 05:53:51 +0000836} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000837
Alexey Bataev9959db52014-05-06 10:08:46 +0000838void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
839 Scope *CurScope) {
840 switch (DKind) {
841 case OMPD_parallel: {
842 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
843 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
844 Sema::CapturedParamNameType Params[3] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000845 std::make_pair(".global_tid.", KmpInt32PtrTy),
846 std::make_pair(".bound_tid.", KmpInt32PtrTy),
847 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000848 };
849 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
850 break;
851 }
852 case OMPD_simd: {
853 Sema::CapturedParamNameType Params[1] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000854 std::make_pair(StringRef(), QualType()) // __context with shared vars
855 };
856 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
857 break;
858 }
859 case OMPD_for: {
860 Sema::CapturedParamNameType Params[1] = {
861 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000862 };
863 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
864 break;
865 }
866 case OMPD_threadprivate:
867 case OMPD_task:
868 llvm_unreachable("OpenMP Directive is not allowed");
869 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000870 llvm_unreachable("Unknown OpenMP directive");
871 }
872}
873
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000874StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
875 ArrayRef<OMPClause *> Clauses,
876 Stmt *AStmt,
877 SourceLocation StartLoc,
878 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000879 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
880
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000881 StmtResult Res = StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000882
883 // Check default data sharing attributes for referenced variables.
884 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
885 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
886 if (DSAChecker.isErrorFound())
887 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000888 // Generate list of implicitly defined firstprivate variables.
889 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
890 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
891
892 bool ErrorFound = false;
893 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000894 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
895 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
896 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000897 ClausesWithImplicit.push_back(Implicit);
898 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +0000899 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000900 } else
901 ErrorFound = true;
902 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000903
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000904 switch (Kind) {
905 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +0000906 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
907 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000908 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000909 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +0000910 Res =
911 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000912 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +0000913 case OMPD_for:
914 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
915 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000916 case OMPD_threadprivate:
917 case OMPD_task:
918 llvm_unreachable("OpenMP Directive is not allowed");
919 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000920 llvm_unreachable("Unknown OpenMP directive");
921 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000922
Alexey Bataeved09d242014-05-28 05:53:51 +0000923 if (ErrorFound)
924 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000925 return Res;
926}
927
928StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
929 Stmt *AStmt,
930 SourceLocation StartLoc,
931 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000932 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
933 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
934 // 1.2.2 OpenMP Language Terminology
935 // Structured block - An executable statement with a single entry at the
936 // top and a single exit at the bottom.
937 // The point of exit cannot be a branch out of the structured block.
938 // longjmp() and throw() must not violate the entry/exit criteria.
939 CS->getCapturedDecl()->setNothrow();
940
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000941 getCurFunction()->setHasBranchProtectedScope();
942
Nikola Smiljanic03ff2592014-05-29 14:05:12 +0000943 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
944 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000945}
946
Alexander Musmana8e9d2e2014-06-03 10:16:47 +0000947namespace {
948/// \brief Helper class for checking canonical form of the OpenMP loops and
949/// extracting iteration space of each loop in the loop nest, that will be used
950/// for IR generation.
951class OpenMPIterationSpaceChecker {
952 /// \brief Reference to Sema.
953 Sema &SemaRef;
954 /// \brief A location for diagnostics (when there is no some better location).
955 SourceLocation DefaultLoc;
956 /// \brief A location for diagnostics (when increment is not compatible).
957 SourceLocation ConditionLoc;
958 /// \brief A source location for referring to condition later.
959 SourceRange ConditionSrcRange;
960 /// \brief Loop variable.
961 VarDecl *Var;
962 /// \brief Lower bound (initializer for the var).
963 Expr *LB;
964 /// \brief Upper bound.
965 Expr *UB;
966 /// \brief Loop step (increment).
967 Expr *Step;
968 /// \brief This flag is true when condition is one of:
969 /// Var < UB
970 /// Var <= UB
971 /// UB > Var
972 /// UB >= Var
973 bool TestIsLessOp;
974 /// \brief This flag is true when condition is strict ( < or > ).
975 bool TestIsStrictOp;
976 /// \brief This flag is true when step is subtracted on each iteration.
977 bool SubtractStep;
978
979public:
980 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
981 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
982 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
983 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
984 SubtractStep(false) {}
985 /// \brief Check init-expr for canonical loop form and save loop counter
986 /// variable - #Var and its initialization value - #LB.
987 bool CheckInit(Stmt *S);
988 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
989 /// for less/greater and for strict/non-strict comparison.
990 bool CheckCond(Expr *S);
991 /// \brief Check incr-expr for canonical loop form and return true if it
992 /// does not conform, otherwise save loop step (#Step).
993 bool CheckInc(Expr *S);
994 /// \brief Return the loop counter variable.
995 VarDecl *GetLoopVar() const { return Var; }
996 /// \brief Return true if any expression is dependent.
997 bool Dependent() const;
998
999private:
1000 /// \brief Check the right-hand side of an assignment in the increment
1001 /// expression.
1002 bool CheckIncRHS(Expr *RHS);
1003 /// \brief Helper to set loop counter variable and its initializer.
1004 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1005 /// \brief Helper to set upper bound.
1006 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1007 const SourceLocation &SL);
1008 /// \brief Helper to set loop increment.
1009 bool SetStep(Expr *NewStep, bool Subtract);
1010};
1011
1012bool OpenMPIterationSpaceChecker::Dependent() const {
1013 if (!Var) {
1014 assert(!LB && !UB && !Step);
1015 return false;
1016 }
1017 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1018 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1019}
1020
1021bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1022 // State consistency checking to ensure correct usage.
1023 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1024 !TestIsLessOp && !TestIsStrictOp);
1025 if (!NewVar || !NewLB)
1026 return true;
1027 Var = NewVar;
1028 LB = NewLB;
1029 return false;
1030}
1031
1032bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1033 const SourceRange &SR,
1034 const SourceLocation &SL) {
1035 // State consistency checking to ensure correct usage.
1036 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1037 !TestIsLessOp && !TestIsStrictOp);
1038 if (!NewUB)
1039 return true;
1040 UB = NewUB;
1041 TestIsLessOp = LessOp;
1042 TestIsStrictOp = StrictOp;
1043 ConditionSrcRange = SR;
1044 ConditionLoc = SL;
1045 return false;
1046}
1047
1048bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1049 // State consistency checking to ensure correct usage.
1050 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1051 if (!NewStep)
1052 return true;
1053 if (!NewStep->isValueDependent()) {
1054 // Check that the step is integer expression.
1055 SourceLocation StepLoc = NewStep->getLocStart();
1056 ExprResult Val =
1057 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1058 if (Val.isInvalid())
1059 return true;
1060 NewStep = Val.get();
1061
1062 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1063 // If test-expr is of form var relational-op b and relational-op is < or
1064 // <= then incr-expr must cause var to increase on each iteration of the
1065 // loop. If test-expr is of form var relational-op b and relational-op is
1066 // > or >= then incr-expr must cause var to decrease on each iteration of
1067 // the loop.
1068 // If test-expr is of form b relational-op var and relational-op is < or
1069 // <= then incr-expr must cause var to decrease on each iteration of the
1070 // loop. If test-expr is of form b relational-op var and relational-op is
1071 // > or >= then incr-expr must cause var to increase on each iteration of
1072 // the loop.
1073 llvm::APSInt Result;
1074 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1075 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1076 bool IsConstNeg =
1077 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1078 bool IsConstZero = IsConstant && !Result.getBoolValue();
1079 if (UB && (IsConstZero ||
1080 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1081 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1082 SemaRef.Diag(NewStep->getExprLoc(),
1083 diag::err_omp_loop_incr_not_compatible)
1084 << Var << TestIsLessOp << NewStep->getSourceRange();
1085 SemaRef.Diag(ConditionLoc,
1086 diag::note_omp_loop_cond_requres_compatible_incr)
1087 << TestIsLessOp << ConditionSrcRange;
1088 return true;
1089 }
1090 }
1091
1092 Step = NewStep;
1093 SubtractStep = Subtract;
1094 return false;
1095}
1096
1097bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1098 // Check init-expr for canonical loop form and save loop counter
1099 // variable - #Var and its initialization value - #LB.
1100 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1101 // var = lb
1102 // integer-type var = lb
1103 // random-access-iterator-type var = lb
1104 // pointer-type var = lb
1105 //
1106 if (!S) {
1107 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1108 return true;
1109 }
1110 if (Expr *E = dyn_cast<Expr>(S))
1111 S = E->IgnoreParens();
1112 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1113 if (BO->getOpcode() == BO_Assign)
1114 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1115 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1116 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1117 if (DS->isSingleDecl()) {
1118 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1119 if (Var->hasInit()) {
1120 // Accept non-canonical init form here but emit ext. warning.
1121 if (Var->getInitStyle() != VarDecl::CInit)
1122 SemaRef.Diag(S->getLocStart(),
1123 diag::ext_omp_loop_not_canonical_init)
1124 << S->getSourceRange();
1125 return SetVarAndLB(Var, Var->getInit());
1126 }
1127 }
1128 }
1129 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1130 if (CE->getOperator() == OO_Equal)
1131 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1132 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1133
1134 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1135 << S->getSourceRange();
1136 return true;
1137}
1138
Alexey Bataev23b69422014-06-18 07:08:49 +00001139/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001140/// variable (which may be the loop variable) if possible.
1141static const VarDecl *GetInitVarDecl(const Expr *E) {
1142 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001143 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001144 E = E->IgnoreParenImpCasts();
1145 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1146 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1147 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1148 CE->getArg(0) != nullptr)
1149 E = CE->getArg(0)->IgnoreParenImpCasts();
1150 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1151 if (!DRE)
1152 return nullptr;
1153 return dyn_cast<VarDecl>(DRE->getDecl());
1154}
1155
1156bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1157 // Check test-expr for canonical form, save upper-bound UB, flags for
1158 // less/greater and for strict/non-strict comparison.
1159 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1160 // var relational-op b
1161 // b relational-op var
1162 //
1163 if (!S) {
1164 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1165 return true;
1166 }
1167 S = S->IgnoreParenImpCasts();
1168 SourceLocation CondLoc = S->getLocStart();
1169 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1170 if (BO->isRelationalOp()) {
1171 if (GetInitVarDecl(BO->getLHS()) == Var)
1172 return SetUB(BO->getRHS(),
1173 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1174 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1175 BO->getSourceRange(), BO->getOperatorLoc());
1176 if (GetInitVarDecl(BO->getRHS()) == Var)
1177 return SetUB(BO->getLHS(),
1178 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1179 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1180 BO->getSourceRange(), BO->getOperatorLoc());
1181 }
1182 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1183 if (CE->getNumArgs() == 2) {
1184 auto Op = CE->getOperator();
1185 switch (Op) {
1186 case OO_Greater:
1187 case OO_GreaterEqual:
1188 case OO_Less:
1189 case OO_LessEqual:
1190 if (GetInitVarDecl(CE->getArg(0)) == Var)
1191 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1192 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1193 CE->getOperatorLoc());
1194 if (GetInitVarDecl(CE->getArg(1)) == Var)
1195 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1196 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1197 CE->getOperatorLoc());
1198 break;
1199 default:
1200 break;
1201 }
1202 }
1203 }
1204 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1205 << S->getSourceRange() << Var;
1206 return true;
1207}
1208
1209bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1210 // RHS of canonical loop form increment can be:
1211 // var + incr
1212 // incr + var
1213 // var - incr
1214 //
1215 RHS = RHS->IgnoreParenImpCasts();
1216 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1217 if (BO->isAdditiveOp()) {
1218 bool IsAdd = BO->getOpcode() == BO_Add;
1219 if (GetInitVarDecl(BO->getLHS()) == Var)
1220 return SetStep(BO->getRHS(), !IsAdd);
1221 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1222 return SetStep(BO->getLHS(), false);
1223 }
1224 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1225 bool IsAdd = CE->getOperator() == OO_Plus;
1226 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1227 if (GetInitVarDecl(CE->getArg(0)) == Var)
1228 return SetStep(CE->getArg(1), !IsAdd);
1229 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1230 return SetStep(CE->getArg(0), false);
1231 }
1232 }
1233 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1234 << RHS->getSourceRange() << Var;
1235 return true;
1236}
1237
1238bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1239 // Check incr-expr for canonical loop form and return true if it
1240 // does not conform.
1241 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1242 // ++var
1243 // var++
1244 // --var
1245 // var--
1246 // var += incr
1247 // var -= incr
1248 // var = var + incr
1249 // var = incr + var
1250 // var = var - incr
1251 //
1252 if (!S) {
1253 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1254 return true;
1255 }
1256 S = S->IgnoreParens();
1257 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1258 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1259 return SetStep(
1260 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1261 (UO->isDecrementOp() ? -1 : 1)).get(),
1262 false);
1263 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1264 switch (BO->getOpcode()) {
1265 case BO_AddAssign:
1266 case BO_SubAssign:
1267 if (GetInitVarDecl(BO->getLHS()) == Var)
1268 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1269 break;
1270 case BO_Assign:
1271 if (GetInitVarDecl(BO->getLHS()) == Var)
1272 return CheckIncRHS(BO->getRHS());
1273 break;
1274 default:
1275 break;
1276 }
1277 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1278 switch (CE->getOperator()) {
1279 case OO_PlusPlus:
1280 case OO_MinusMinus:
1281 if (GetInitVarDecl(CE->getArg(0)) == Var)
1282 return SetStep(
1283 SemaRef.ActOnIntegerConstant(
1284 CE->getLocStart(),
1285 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1286 false);
1287 break;
1288 case OO_PlusEqual:
1289 case OO_MinusEqual:
1290 if (GetInitVarDecl(CE->getArg(0)) == Var)
1291 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1292 break;
1293 case OO_Equal:
1294 if (GetInitVarDecl(CE->getArg(0)) == Var)
1295 return CheckIncRHS(CE->getArg(1));
1296 break;
1297 default:
1298 break;
1299 }
1300 }
1301 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1302 << S->getSourceRange() << Var;
1303 return true;
1304}
Alexey Bataev23b69422014-06-18 07:08:49 +00001305} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001306
1307/// \brief Called on a for stmt to check and extract its iteration space
1308/// for further processing (such as collapsing).
1309static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
1310 Sema &SemaRef, DSAStackTy &DSA) {
1311 // OpenMP [2.6, Canonical Loop Form]
1312 // for (init-expr; test-expr; incr-expr) structured-block
1313 auto For = dyn_cast_or_null<ForStmt>(S);
1314 if (!For) {
1315 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
1316 << getOpenMPDirectiveName(DKind);
1317 return true;
1318 }
1319 assert(For->getBody());
1320
1321 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1322
1323 // Check init.
1324 Stmt *Init = For->getInit();
1325 if (ISC.CheckInit(Init)) {
1326 return true;
1327 }
1328
1329 bool HasErrors = false;
1330
1331 // Check loop variable's type.
1332 VarDecl *Var = ISC.GetLoopVar();
1333
1334 // OpenMP [2.6, Canonical Loop Form]
1335 // Var is one of the following:
1336 // A variable of signed or unsigned integer type.
1337 // For C++, a variable of a random access iterator type.
1338 // For C, a variable of a pointer type.
1339 QualType VarType = Var->getType();
1340 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1341 !VarType->isPointerType() &&
1342 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1343 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1344 << SemaRef.getLangOpts().CPlusPlus;
1345 HasErrors = true;
1346 }
1347
1348 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1349 // a Construct, C/C++].
1350 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1351 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001352 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1353 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001354 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001355 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1356 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) ||
1357 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1358 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001359 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001360 // The loop iteration variable in the associated for-loop of a simd
1361 // construct with just one associated for-loop may be listed in a linear
1362 // clause with a constant-linear-step that is the increment of the
1363 // associated for-loop.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001364 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1365 << getOpenMPClauseName(DVar.CKind);
1366 if (DVar.RefExpr)
1367 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1368 << getOpenMPClauseName(DVar.CKind);
1369 else
1370 SemaRef.Diag(Var->getLocation(), diag::note_omp_predetermined_dsa)
1371 << getOpenMPClauseName(DVar.CKind);
1372 HasErrors = true;
1373 } else {
1374 // Make the loop iteration variable private by default.
1375 DSA.addDSA(Var, nullptr, OMPC_private);
1376 }
1377
Alexey Bataevf29276e2014-06-18 04:14:57 +00001378 assert(isOpenMPLoopDirective(DKind) && "DSA for non-simd loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001379
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001380 // Check test-expr.
1381 HasErrors |= ISC.CheckCond(For->getCond());
1382
1383 // Check incr-expr.
1384 HasErrors |= ISC.CheckInc(For->getInc());
1385
1386 if (ISC.Dependent())
1387 return HasErrors;
1388
1389 // FIXME: Build loop's iteration space representation.
1390 return HasErrors;
1391}
1392
1393/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1394/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1395/// to get the first for loop.
1396static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1397 if (IgnoreCaptured)
1398 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1399 S = CapS->getCapturedStmt();
1400 // OpenMP [2.8.1, simd construct, Restrictions]
1401 // All loops associated with the construct must be perfectly nested; that is,
1402 // there must be no intervening code nor any OpenMP directive between any two
1403 // loops.
1404 while (true) {
1405 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1406 S = AS->getSubStmt();
1407 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1408 if (CS->size() != 1)
1409 break;
1410 S = CS->body_back();
1411 } else
1412 break;
1413 }
1414 return S;
1415}
1416
1417/// \brief Called on a for stmt to check itself and nested loops (if any).
1418static bool CheckOpenMPLoop(OpenMPDirectiveKind DKind, unsigned NestedLoopCount,
1419 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA) {
1420 // This is helper routine for loop directives (e.g., 'for', 'simd',
1421 // 'for simd', etc.).
1422 assert(NestedLoopCount == 1);
1423 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1424 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
1425 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA))
1426 return true;
1427 // Move on to the next nested for loop, or to the loop body.
1428 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1429 }
1430
1431 // FIXME: Build resulting iteration space for IR generation (collapsing
1432 // iteration spaces when loop count > 1 ('collapse' clause)).
1433 return false;
1434}
1435
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001436StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001437 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001438 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001439 // In presence of clause 'collapse', it will define the nested loops number.
1440 // For now, pass default value of 1.
1441 if (CheckOpenMPLoop(OMPD_simd, 1, AStmt, *this, *DSAStack))
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001442 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001443
1444 getCurFunction()->setHasBranchProtectedScope();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001445 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001446}
1447
Alexey Bataevf29276e2014-06-18 04:14:57 +00001448StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses,
1449 Stmt *AStmt, SourceLocation StartLoc,
1450 SourceLocation EndLoc) {
1451 // In presence of clause 'collapse', it will define the nested loops number.
1452 // For now, pass default value of 1.
1453 if (CheckOpenMPLoop(OMPD_for, 1, AStmt, *this, *DSAStack))
1454 return StmtError();
1455
1456 getCurFunction()->setHasBranchProtectedScope();
1457 return OMPForDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1458}
1459
Alexey Bataeved09d242014-05-28 05:53:51 +00001460OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001461 SourceLocation StartLoc,
1462 SourceLocation LParenLoc,
1463 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001464 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001465 switch (Kind) {
1466 case OMPC_if:
1467 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1468 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001469 case OMPC_num_threads:
1470 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1471 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001472 case OMPC_safelen:
1473 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1474 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001475 case OMPC_collapse:
1476 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1477 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001478 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001479 case OMPC_proc_bind:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001480 case OMPC_private:
1481 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001482 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001483 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001484 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001485 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001486 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001487 case OMPC_copyin:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001488 case OMPC_threadprivate:
1489 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001490 llvm_unreachable("Clause is not allowed.");
1491 }
1492 return Res;
1493}
1494
Alexey Bataeved09d242014-05-28 05:53:51 +00001495OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001496 SourceLocation LParenLoc,
1497 SourceLocation EndLoc) {
1498 Expr *ValExpr = Condition;
1499 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1500 !Condition->isInstantiationDependent() &&
1501 !Condition->containsUnexpandedParameterPack()) {
1502 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001503 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001504 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001505 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001506
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001507 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001508 }
1509
1510 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1511}
1512
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001513ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1514 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001515 if (!Op)
1516 return ExprError();
1517
1518 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1519 public:
1520 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001521 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001522 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1523 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001524 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1525 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001526 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1527 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001528 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1529 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001530 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1531 QualType T,
1532 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001533 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1534 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001535 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1536 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001537 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001538 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001539 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001540 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1541 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001542 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1543 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001544 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1545 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001546 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001547 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001548 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001549 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1550 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001551 llvm_unreachable("conversion functions are permitted");
1552 }
1553 } ConvertDiagnoser;
1554 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1555}
1556
1557OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1558 SourceLocation StartLoc,
1559 SourceLocation LParenLoc,
1560 SourceLocation EndLoc) {
1561 Expr *ValExpr = NumThreads;
1562 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1563 !NumThreads->isInstantiationDependent() &&
1564 !NumThreads->containsUnexpandedParameterPack()) {
1565 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1566 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001567 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001568 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001569 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001570
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001571 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001572
1573 // OpenMP [2.5, Restrictions]
1574 // The num_threads expression must evaluate to a positive integer value.
1575 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001576 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1577 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001578 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1579 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001580 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001581 }
1582 }
1583
Alexey Bataeved09d242014-05-28 05:53:51 +00001584 return new (Context)
1585 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001586}
1587
Alexey Bataev62c87d22014-03-21 04:51:18 +00001588ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1589 OpenMPClauseKind CKind) {
1590 if (!E)
1591 return ExprError();
1592 if (E->isValueDependent() || E->isTypeDependent() ||
1593 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001594 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001595 llvm::APSInt Result;
1596 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1597 if (ICE.isInvalid())
1598 return ExprError();
1599 if (!Result.isStrictlyPositive()) {
1600 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1601 << getOpenMPClauseName(CKind) << E->getSourceRange();
1602 return ExprError();
1603 }
1604 return ICE;
1605}
1606
1607OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1608 SourceLocation LParenLoc,
1609 SourceLocation EndLoc) {
1610 // OpenMP [2.8.1, simd construct, Description]
1611 // The parameter of the safelen clause must be a constant
1612 // positive integer expression.
1613 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1614 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001615 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001616 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001617 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001618}
1619
Alexander Musman64d33f12014-06-04 07:53:32 +00001620OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1621 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001622 SourceLocation LParenLoc,
1623 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001624 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001625 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001626 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001627 // The parameter of the collapse clause must be a constant
1628 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001629 ExprResult NumForLoopsResult =
1630 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1631 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001632 return nullptr;
1633 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001634 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001635}
1636
Alexey Bataeved09d242014-05-28 05:53:51 +00001637OMPClause *Sema::ActOnOpenMPSimpleClause(
1638 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1639 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001640 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001641 switch (Kind) {
1642 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001643 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001644 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1645 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001646 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001647 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001648 Res = ActOnOpenMPProcBindClause(
1649 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1650 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001651 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001652 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001653 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001654 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001655 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001656 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001657 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001658 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001659 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001660 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001661 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001662 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001663 case OMPC_copyin:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001664 case OMPC_threadprivate:
1665 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001666 llvm_unreachable("Clause is not allowed.");
1667 }
1668 return Res;
1669}
1670
1671OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1672 SourceLocation KindKwLoc,
1673 SourceLocation StartLoc,
1674 SourceLocation LParenLoc,
1675 SourceLocation EndLoc) {
1676 if (Kind == OMPC_DEFAULT_unknown) {
1677 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001678 static_assert(OMPC_DEFAULT_unknown > 0,
1679 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001680 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001681 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001682 Values += "'";
1683 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1684 Values += "'";
1685 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001686 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001687 Values += " or ";
1688 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001689 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001690 break;
1691 default:
1692 Values += Sep;
1693 break;
1694 }
1695 }
1696 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001697 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001698 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001699 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001700 switch (Kind) {
1701 case OMPC_DEFAULT_none:
1702 DSAStack->setDefaultDSANone();
1703 break;
1704 case OMPC_DEFAULT_shared:
1705 DSAStack->setDefaultDSAShared();
1706 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001707 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001708 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001709 break;
1710 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001711 return new (Context)
1712 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001713}
1714
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001715OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1716 SourceLocation KindKwLoc,
1717 SourceLocation StartLoc,
1718 SourceLocation LParenLoc,
1719 SourceLocation EndLoc) {
1720 if (Kind == OMPC_PROC_BIND_unknown) {
1721 std::string Values;
1722 std::string Sep(", ");
1723 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1724 Values += "'";
1725 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1726 Values += "'";
1727 switch (i) {
1728 case OMPC_PROC_BIND_unknown - 2:
1729 Values += " or ";
1730 break;
1731 case OMPC_PROC_BIND_unknown - 1:
1732 break;
1733 default:
1734 Values += Sep;
1735 break;
1736 }
1737 }
1738 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001739 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001740 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001741 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001742 return new (Context)
1743 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001744}
1745
Alexey Bataevc5e02582014-06-16 07:08:35 +00001746OMPClause *Sema::ActOnOpenMPVarListClause(
1747 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
1748 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
1749 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
1750 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001751 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001752 switch (Kind) {
1753 case OMPC_private:
1754 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1755 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001756 case OMPC_firstprivate:
1757 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1758 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00001759 case OMPC_lastprivate:
1760 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1761 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001762 case OMPC_shared:
1763 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
1764 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00001765 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00001766 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
1767 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001768 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00001769 case OMPC_linear:
1770 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
1771 ColonLoc, EndLoc);
1772 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001773 case OMPC_aligned:
1774 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
1775 ColonLoc, EndLoc);
1776 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001777 case OMPC_copyin:
1778 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
1779 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001780 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001781 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001782 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001783 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001784 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001785 case OMPC_proc_bind:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001786 case OMPC_threadprivate:
1787 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001788 llvm_unreachable("Clause is not allowed.");
1789 }
1790 return Res;
1791}
1792
1793OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
1794 SourceLocation StartLoc,
1795 SourceLocation LParenLoc,
1796 SourceLocation EndLoc) {
1797 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001798 for (auto &RefExpr : VarList) {
1799 assert(RefExpr && "NULL expr in OpenMP private clause.");
1800 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001801 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001802 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001803 continue;
1804 }
1805
Alexey Bataeved09d242014-05-28 05:53:51 +00001806 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001807 // OpenMP [2.1, C/C++]
1808 // A list item is a variable name.
1809 // OpenMP [2.9.3.3, Restrictions, p.1]
1810 // A variable that is part of another variable (as an array or
1811 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001812 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001813 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001814 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001815 continue;
1816 }
1817 Decl *D = DE->getDecl();
1818 VarDecl *VD = cast<VarDecl>(D);
1819
1820 QualType Type = VD->getType();
1821 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1822 // It will be analyzed later.
1823 Vars.push_back(DE);
1824 continue;
1825 }
1826
1827 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1828 // A variable that appears in a private clause must not have an incomplete
1829 // type or a reference type.
1830 if (RequireCompleteType(ELoc, Type,
1831 diag::err_omp_private_incomplete_type)) {
1832 continue;
1833 }
1834 if (Type->isReferenceType()) {
1835 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001836 << getOpenMPClauseName(OMPC_private) << Type;
1837 bool IsDecl =
1838 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1839 Diag(VD->getLocation(),
1840 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1841 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001842 continue;
1843 }
1844
1845 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
1846 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00001847 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001848 // class type.
1849 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001850 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
1851 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001852 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001853 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1854 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1855 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00001856 // FIXME This code must be replaced by actual constructing/destructing of
1857 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001858 if (RD) {
1859 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
1860 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001861 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00001862 if (!CD ||
1863 CheckConstructorAccess(ELoc, CD,
1864 InitializedEntity::InitializeTemporary(Type),
1865 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001866 CD->isDeleted()) {
1867 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001868 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001869 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1870 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001871 Diag(VD->getLocation(),
1872 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1873 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001874 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1875 continue;
1876 }
1877 MarkFunctionReferenced(ELoc, CD);
1878 DiagnoseUseOfDecl(CD, ELoc);
1879
1880 CXXDestructorDecl *DD = RD->getDestructor();
1881 if (DD) {
1882 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1883 DD->isDeleted()) {
1884 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00001885 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001886 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1887 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00001888 Diag(VD->getLocation(),
1889 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1890 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001891 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1892 continue;
1893 }
1894 MarkFunctionReferenced(ELoc, DD);
1895 DiagnoseUseOfDecl(DD, ELoc);
1896 }
1897 }
1898
Alexey Bataev758e55e2013-09-06 18:03:48 +00001899 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1900 // in a Construct]
1901 // Variables with the predetermined data-sharing attributes may not be
1902 // listed in data-sharing attributes clauses, except for the cases
1903 // listed below. For these exceptions only, listing a predetermined
1904 // variable in a data-sharing attribute clause is allowed and overrides
1905 // the variable's predetermined data-sharing attributes.
1906 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1907 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001908 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
1909 << getOpenMPClauseName(OMPC_private);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001910 if (DVar.RefExpr) {
1911 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001912 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001913 } else {
1914 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00001915 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001916 }
1917 continue;
1918 }
1919
1920 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001921 Vars.push_back(DE);
1922 }
1923
Alexey Bataeved09d242014-05-28 05:53:51 +00001924 if (Vars.empty())
1925 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001926
1927 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1928}
1929
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001930OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
1931 SourceLocation StartLoc,
1932 SourceLocation LParenLoc,
1933 SourceLocation EndLoc) {
1934 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001935 for (auto &RefExpr : VarList) {
1936 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
1937 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001938 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00001939 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001940 continue;
1941 }
1942
Alexey Bataeved09d242014-05-28 05:53:51 +00001943 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001944 // OpenMP [2.1, C/C++]
1945 // A list item is a variable name.
1946 // OpenMP [2.9.3.3, Restrictions, p.1]
1947 // A variable that is part of another variable (as an array or
1948 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00001949 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001950 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001951 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001952 continue;
1953 }
1954 Decl *D = DE->getDecl();
1955 VarDecl *VD = cast<VarDecl>(D);
1956
1957 QualType Type = VD->getType();
1958 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1959 // It will be analyzed later.
1960 Vars.push_back(DE);
1961 continue;
1962 }
1963
1964 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
1965 // A variable that appears in a private clause must not have an incomplete
1966 // type or a reference type.
1967 if (RequireCompleteType(ELoc, Type,
1968 diag::err_omp_firstprivate_incomplete_type)) {
1969 continue;
1970 }
1971 if (Type->isReferenceType()) {
1972 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001973 << getOpenMPClauseName(OMPC_firstprivate) << Type;
1974 bool IsDecl =
1975 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1976 Diag(VD->getLocation(),
1977 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1978 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001979 continue;
1980 }
1981
1982 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
1983 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00001984 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001985 // class type.
1986 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001987 CXXRecordDecl *RD = getLangOpts().CPlusPlus
1988 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
1989 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00001990 // FIXME This code must be replaced by actual constructing/destructing of
1991 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001992 if (RD) {
1993 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
1994 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00001995 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00001996 if (!CD ||
1997 CheckConstructorAccess(ELoc, CD,
1998 InitializedEntity::InitializeTemporary(Type),
1999 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002000 CD->isDeleted()) {
2001 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002002 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002003 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2004 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002005 Diag(VD->getLocation(),
2006 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2007 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002008 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2009 continue;
2010 }
2011 MarkFunctionReferenced(ELoc, CD);
2012 DiagnoseUseOfDecl(CD, ELoc);
2013
2014 CXXDestructorDecl *DD = RD->getDestructor();
2015 if (DD) {
2016 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2017 DD->isDeleted()) {
2018 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002019 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002020 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2021 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002022 Diag(VD->getLocation(),
2023 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2024 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002025 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2026 continue;
2027 }
2028 MarkFunctionReferenced(ELoc, DD);
2029 DiagnoseUseOfDecl(DD, ELoc);
2030 }
2031 }
2032
2033 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2034 // variable and it was checked already.
2035 if (StartLoc.isValid() && EndLoc.isValid()) {
2036 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2037 Type = Type.getNonReferenceType().getCanonicalType();
2038 bool IsConstant = Type.isConstant(Context);
2039 Type = Context.getBaseElementType(Type);
2040 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2041 // A list item that specifies a given variable may not appear in more
2042 // than one clause on the same directive, except that a variable may be
2043 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002044 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002045 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002046 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002047 << getOpenMPClauseName(DVar.CKind)
2048 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002049 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002050 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002051 continue;
2052 }
2053
2054 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2055 // in a Construct]
2056 // Variables with the predetermined data-sharing attributes may not be
2057 // listed in data-sharing attributes clauses, except for the cases
2058 // listed below. For these exceptions only, listing a predetermined
2059 // variable in a data-sharing attribute clause is allowed and overrides
2060 // the variable's predetermined data-sharing attributes.
2061 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2062 // in a Construct, C/C++, p.2]
2063 // Variables with const-qualified type having no mutable member may be
2064 // listed in a firstprivate clause, even if they are static data members.
2065 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2066 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2067 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002068 << getOpenMPClauseName(DVar.CKind)
2069 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002070 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002071 << getOpenMPClauseName(DVar.CKind);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002072 continue;
2073 }
2074
Alexey Bataevf29276e2014-06-18 04:14:57 +00002075 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002076 // OpenMP [2.9.3.4, Restrictions, p.2]
2077 // A list item that is private within a parallel region must not appear
2078 // in a firstprivate clause on a worksharing construct if any of the
2079 // worksharing regions arising from the worksharing construct ever bind
2080 // to any of the parallel regions arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002081 if (isOpenMPWorksharingDirective(CurrDir)) {
2082 DVar = DSAStack->getImplicitDSA(VD);
2083 if (DVar.CKind != OMPC_shared) {
2084 Diag(ELoc, diag::err_omp_required_access)
2085 << getOpenMPClauseName(OMPC_firstprivate)
2086 << getOpenMPClauseName(OMPC_shared);
2087 if (DVar.RefExpr) {
2088 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2089 << getOpenMPClauseName(DVar.CKind);
2090 }
2091 continue;
2092 }
2093 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002094 // OpenMP [2.9.3.4, Restrictions, p.3]
2095 // A list item that appears in a reduction clause of a parallel construct
2096 // must not appear in a firstprivate clause on a worksharing or task
2097 // construct if any of the worksharing or task regions arising from the
2098 // worksharing or task construct ever bind to any of the parallel regions
2099 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002100 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002101 // OpenMP [2.9.3.4, Restrictions, p.4]
2102 // A list item that appears in a reduction clause in worksharing
2103 // construct must not appear in a firstprivate clause in a task construct
2104 // encountered during execution of any of the worksharing regions arising
2105 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002106 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002107 }
2108
2109 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2110 Vars.push_back(DE);
2111 }
2112
Alexey Bataeved09d242014-05-28 05:53:51 +00002113 if (Vars.empty())
2114 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002115
2116 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2117 Vars);
2118}
2119
Alexander Musman1bb328c2014-06-04 13:06:39 +00002120OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2121 SourceLocation StartLoc,
2122 SourceLocation LParenLoc,
2123 SourceLocation EndLoc) {
2124 SmallVector<Expr *, 8> Vars;
2125 for (auto &RefExpr : VarList) {
2126 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2127 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2128 // It will be analyzed later.
2129 Vars.push_back(RefExpr);
2130 continue;
2131 }
2132
2133 SourceLocation ELoc = RefExpr->getExprLoc();
2134 // OpenMP [2.1, C/C++]
2135 // A list item is a variable name.
2136 // OpenMP [2.14.3.5, Restrictions, p.1]
2137 // A variable that is part of another variable (as an array or structure
2138 // element) cannot appear in a lastprivate clause.
2139 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2140 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2141 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2142 continue;
2143 }
2144 Decl *D = DE->getDecl();
2145 VarDecl *VD = cast<VarDecl>(D);
2146
2147 QualType Type = VD->getType();
2148 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2149 // It will be analyzed later.
2150 Vars.push_back(DE);
2151 continue;
2152 }
2153
2154 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2155 // A variable that appears in a lastprivate clause must not have an
2156 // incomplete type or a reference type.
2157 if (RequireCompleteType(ELoc, Type,
2158 diag::err_omp_lastprivate_incomplete_type)) {
2159 continue;
2160 }
2161 if (Type->isReferenceType()) {
2162 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2163 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2164 bool IsDecl =
2165 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2166 Diag(VD->getLocation(),
2167 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2168 << VD;
2169 continue;
2170 }
2171
2172 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2173 // in a Construct]
2174 // Variables with the predetermined data-sharing attributes may not be
2175 // listed in data-sharing attributes clauses, except for the cases
2176 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002177 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2178 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2179 DVar.CKind != OMPC_firstprivate &&
2180 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2181 Diag(ELoc, diag::err_omp_wrong_dsa)
2182 << getOpenMPClauseName(DVar.CKind)
2183 << getOpenMPClauseName(OMPC_lastprivate);
2184 if (DVar.RefExpr)
2185 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2186 << getOpenMPClauseName(DVar.CKind);
2187 else
2188 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
2189 << getOpenMPClauseName(DVar.CKind);
2190 continue;
2191 }
2192
Alexey Bataevf29276e2014-06-18 04:14:57 +00002193 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2194 // OpenMP [2.14.3.5, Restrictions, p.2]
2195 // A list item that is private within a parallel region, or that appears in
2196 // the reduction clause of a parallel construct, must not appear in a
2197 // lastprivate clause on a worksharing construct if any of the corresponding
2198 // worksharing regions ever binds to any of the corresponding parallel
2199 // regions.
2200 if (isOpenMPWorksharingDirective(CurrDir)) {
2201 DVar = DSAStack->getImplicitDSA(VD);
2202 if (DVar.CKind != OMPC_shared) {
2203 Diag(ELoc, diag::err_omp_required_access)
2204 << getOpenMPClauseName(OMPC_lastprivate)
2205 << getOpenMPClauseName(OMPC_shared);
2206 if (DVar.RefExpr)
2207 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2208 << getOpenMPClauseName(DVar.CKind);
2209 else
2210 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
2211 << getOpenMPClauseName(DVar.CKind);
2212 continue;
2213 }
2214 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002215 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002216 // A variable of class type (or array thereof) that appears in a
2217 // lastprivate clause requires an accessible, unambiguous default
2218 // constructor for the class type, unless the list item is also specified
2219 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002220 // A variable of class type (or array thereof) that appears in a
2221 // lastprivate clause requires an accessible, unambiguous copy assignment
2222 // operator for the class type.
2223 while (Type.getNonReferenceType()->isArrayType())
2224 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2225 ->getElementType();
2226 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2227 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2228 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002229 // FIXME This code must be replaced by actual copying and destructing of the
2230 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002231 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002232 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2233 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002234 if (MD) {
2235 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2236 MD->isDeleted()) {
2237 Diag(ELoc, diag::err_omp_required_method)
2238 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2239 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2240 VarDecl::DeclarationOnly;
2241 Diag(VD->getLocation(),
2242 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2243 << VD;
2244 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2245 continue;
2246 }
2247 MarkFunctionReferenced(ELoc, MD);
2248 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002249 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002250
2251 CXXDestructorDecl *DD = RD->getDestructor();
2252 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002253 PartialDiagnostic PD =
2254 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002255 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2256 DD->isDeleted()) {
2257 Diag(ELoc, diag::err_omp_required_method)
2258 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2259 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2260 VarDecl::DeclarationOnly;
2261 Diag(VD->getLocation(),
2262 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2263 << VD;
2264 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2265 continue;
2266 }
2267 MarkFunctionReferenced(ELoc, DD);
2268 DiagnoseUseOfDecl(DD, ELoc);
2269 }
2270 }
2271
Alexey Bataevf29276e2014-06-18 04:14:57 +00002272 if (DVar.CKind != OMPC_firstprivate)
2273 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002274 Vars.push_back(DE);
2275 }
2276
2277 if (Vars.empty())
2278 return nullptr;
2279
2280 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2281 Vars);
2282}
2283
Alexey Bataev758e55e2013-09-06 18:03:48 +00002284OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2285 SourceLocation StartLoc,
2286 SourceLocation LParenLoc,
2287 SourceLocation EndLoc) {
2288 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002289 for (auto &RefExpr : VarList) {
2290 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2291 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002292 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002293 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002294 continue;
2295 }
2296
Alexey Bataeved09d242014-05-28 05:53:51 +00002297 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002298 // OpenMP [2.1, C/C++]
2299 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002300 // OpenMP [2.14.3.2, Restrictions, p.1]
2301 // A variable that is part of another variable (as an array or structure
2302 // element) cannot appear in a shared unless it is a static data member
2303 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002304 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002305 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002306 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002307 continue;
2308 }
2309 Decl *D = DE->getDecl();
2310 VarDecl *VD = cast<VarDecl>(D);
2311
2312 QualType Type = VD->getType();
2313 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2314 // It will be analyzed later.
2315 Vars.push_back(DE);
2316 continue;
2317 }
2318
2319 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2320 // in a Construct]
2321 // Variables with the predetermined data-sharing attributes may not be
2322 // listed in data-sharing attributes clauses, except for the cases
2323 // listed below. For these exceptions only, listing a predetermined
2324 // variable in a data-sharing attribute clause is allowed and overrides
2325 // the variable's predetermined data-sharing attributes.
2326 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002327 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2328 DVar.RefExpr) {
2329 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2330 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002331 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002332 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002333 continue;
2334 }
2335
2336 DSAStack->addDSA(VD, DE, OMPC_shared);
2337 Vars.push_back(DE);
2338 }
2339
Alexey Bataeved09d242014-05-28 05:53:51 +00002340 if (Vars.empty())
2341 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002342
2343 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2344}
2345
Alexey Bataevc5e02582014-06-16 07:08:35 +00002346namespace {
2347class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2348 DSAStackTy *Stack;
2349
2350public:
2351 bool VisitDeclRefExpr(DeclRefExpr *E) {
2352 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2353 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2354 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2355 return false;
2356 if (DVar.CKind != OMPC_unknown)
2357 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002358 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002359 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002360 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002361 return true;
2362 return false;
2363 }
2364 return false;
2365 }
2366 bool VisitStmt(Stmt *S) {
2367 for (auto Child : S->children()) {
2368 if (Child && Visit(Child))
2369 return true;
2370 }
2371 return false;
2372 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002373 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002374};
Alexey Bataev23b69422014-06-18 07:08:49 +00002375} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002376
2377OMPClause *Sema::ActOnOpenMPReductionClause(
2378 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2379 SourceLocation ColonLoc, SourceLocation EndLoc,
2380 CXXScopeSpec &ReductionIdScopeSpec,
2381 const DeclarationNameInfo &ReductionId) {
2382 // TODO: Allow scope specification search when 'declare reduction' is
2383 // supported.
2384 assert(ReductionIdScopeSpec.isEmpty() &&
2385 "No support for scoped reduction identifiers yet.");
2386
2387 auto DN = ReductionId.getName();
2388 auto OOK = DN.getCXXOverloadedOperator();
2389 BinaryOperatorKind BOK = BO_Comma;
2390
2391 // OpenMP [2.14.3.6, reduction clause]
2392 // C
2393 // reduction-identifier is either an identifier or one of the following
2394 // operators: +, -, *, &, |, ^, && and ||
2395 // C++
2396 // reduction-identifier is either an id-expression or one of the following
2397 // operators: +, -, *, &, |, ^, && and ||
2398 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2399 switch (OOK) {
2400 case OO_Plus:
2401 case OO_Minus:
2402 BOK = BO_AddAssign;
2403 break;
2404 case OO_Star:
2405 BOK = BO_MulAssign;
2406 break;
2407 case OO_Amp:
2408 BOK = BO_AndAssign;
2409 break;
2410 case OO_Pipe:
2411 BOK = BO_OrAssign;
2412 break;
2413 case OO_Caret:
2414 BOK = BO_XorAssign;
2415 break;
2416 case OO_AmpAmp:
2417 BOK = BO_LAnd;
2418 break;
2419 case OO_PipePipe:
2420 BOK = BO_LOr;
2421 break;
2422 default:
2423 if (auto II = DN.getAsIdentifierInfo()) {
2424 if (II->isStr("max"))
2425 BOK = BO_GT;
2426 else if (II->isStr("min"))
2427 BOK = BO_LT;
2428 }
2429 break;
2430 }
2431 SourceRange ReductionIdRange;
2432 if (ReductionIdScopeSpec.isValid()) {
2433 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2434 }
2435 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2436 if (BOK == BO_Comma) {
2437 // Not allowed reduction identifier is found.
2438 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2439 << ReductionIdRange;
2440 return nullptr;
2441 }
2442
2443 SmallVector<Expr *, 8> Vars;
2444 for (auto RefExpr : VarList) {
2445 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2446 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2447 // It will be analyzed later.
2448 Vars.push_back(RefExpr);
2449 continue;
2450 }
2451
2452 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2453 RefExpr->isInstantiationDependent() ||
2454 RefExpr->containsUnexpandedParameterPack()) {
2455 // It will be analyzed later.
2456 Vars.push_back(RefExpr);
2457 continue;
2458 }
2459
2460 auto ELoc = RefExpr->getExprLoc();
2461 auto ERange = RefExpr->getSourceRange();
2462 // OpenMP [2.1, C/C++]
2463 // A list item is a variable or array section, subject to the restrictions
2464 // specified in Section 2.4 on page 42 and in each of the sections
2465 // describing clauses and directives for which a list appears.
2466 // OpenMP [2.14.3.3, Restrictions, p.1]
2467 // A variable that is part of another variable (as an array or
2468 // structure element) cannot appear in a private clause.
2469 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2470 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2471 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2472 continue;
2473 }
2474 auto D = DE->getDecl();
2475 auto VD = cast<VarDecl>(D);
2476 auto Type = VD->getType();
2477 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2478 // A variable that appears in a private clause must not have an incomplete
2479 // type or a reference type.
2480 if (RequireCompleteType(ELoc, Type,
2481 diag::err_omp_reduction_incomplete_type))
2482 continue;
2483 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2484 // Arrays may not appear in a reduction clause.
2485 if (Type.getNonReferenceType()->isArrayType()) {
2486 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2487 bool IsDecl =
2488 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2489 Diag(VD->getLocation(),
2490 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2491 << VD;
2492 continue;
2493 }
2494 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2495 // A list item that appears in a reduction clause must not be
2496 // const-qualified.
2497 if (Type.getNonReferenceType().isConstant(Context)) {
2498 Diag(ELoc, diag::err_omp_const_variable)
2499 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2500 bool IsDecl =
2501 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2502 Diag(VD->getLocation(),
2503 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2504 << VD;
2505 continue;
2506 }
2507 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2508 // If a list-item is a reference type then it must bind to the same object
2509 // for all threads of the team.
2510 VarDecl *VDDef = VD->getDefinition();
2511 if (Type->isReferenceType() && VDDef) {
2512 DSARefChecker Check(DSAStack);
2513 if (Check.Visit(VDDef->getInit())) {
2514 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2515 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2516 continue;
2517 }
2518 }
2519 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2520 // The type of a list item that appears in a reduction clause must be valid
2521 // for the reduction-identifier. For a max or min reduction in C, the type
2522 // of the list item must be an allowed arithmetic data type: char, int,
2523 // float, double, or _Bool, possibly modified with long, short, signed, or
2524 // unsigned. For a max or min reduction in C++, the type of the list item
2525 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2526 // double, or bool, possibly modified with long, short, signed, or unsigned.
2527 if ((BOK == BO_GT || BOK == BO_LT) &&
2528 !(Type->isScalarType() ||
2529 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2530 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2531 << getLangOpts().CPlusPlus;
2532 bool IsDecl =
2533 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2534 Diag(VD->getLocation(),
2535 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2536 << VD;
2537 continue;
2538 }
2539 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2540 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2541 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2542 bool IsDecl =
2543 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2544 Diag(VD->getLocation(),
2545 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2546 << VD;
2547 continue;
2548 }
2549 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2550 getDiagnostics().setSuppressAllDiagnostics(true);
2551 ExprResult ReductionOp =
2552 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2553 RefExpr, RefExpr);
2554 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2555 if (ReductionOp.isInvalid()) {
2556 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002557 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002558 bool IsDecl =
2559 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2560 Diag(VD->getLocation(),
2561 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2562 << VD;
2563 continue;
2564 }
2565
2566 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2567 // in a Construct]
2568 // Variables with the predetermined data-sharing attributes may not be
2569 // listed in data-sharing attributes clauses, except for the cases
2570 // listed below. For these exceptions only, listing a predetermined
2571 // variable in a data-sharing attribute clause is allowed and overrides
2572 // the variable's predetermined data-sharing attributes.
2573 // OpenMP [2.14.3.6, Restrictions, p.3]
2574 // Any number of reduction clauses can be specified on the directive,
2575 // but a list item can appear only once in the reduction clauses for that
2576 // directive.
2577 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2578 if (DVar.CKind == OMPC_reduction) {
2579 Diag(ELoc, diag::err_omp_once_referenced)
2580 << getOpenMPClauseName(OMPC_reduction);
2581 if (DVar.RefExpr) {
2582 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2583 }
2584 } else if (DVar.CKind != OMPC_unknown) {
2585 Diag(ELoc, diag::err_omp_wrong_dsa)
2586 << getOpenMPClauseName(DVar.CKind)
2587 << getOpenMPClauseName(OMPC_reduction);
2588 if (DVar.RefExpr) {
2589 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2590 << getOpenMPClauseName(DVar.CKind);
2591 } else {
2592 Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
2593 << getOpenMPClauseName(DVar.CKind);
2594 }
2595 continue;
2596 }
2597
2598 // OpenMP [2.14.3.6, Restrictions, p.1]
2599 // A list item that appears in a reduction clause of a worksharing
2600 // construct must be shared in the parallel regions to which any of the
2601 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002602 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2603 if (isOpenMPWorksharingDirective(CurrDir)) {
2604 DVar = DSAStack->getImplicitDSA(VD);
2605 if (DVar.CKind != OMPC_shared) {
2606 Diag(ELoc, diag::err_omp_required_access)
2607 << getOpenMPClauseName(OMPC_reduction)
2608 << getOpenMPClauseName(OMPC_shared);
2609 if (DVar.RefExpr) {
2610 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2611 << getOpenMPClauseName(DVar.CKind);
2612 }
2613 continue;
2614 }
2615 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00002616
2617 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2618 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2619 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002620 // FIXME This code must be replaced by actual constructing/destructing of
2621 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00002622 if (RD) {
2623 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2624 PartialDiagnostic PD =
2625 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00002626 if (!CD ||
2627 CheckConstructorAccess(ELoc, CD,
2628 InitializedEntity::InitializeTemporary(Type),
2629 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00002630 CD->isDeleted()) {
2631 Diag(ELoc, diag::err_omp_required_method)
2632 << getOpenMPClauseName(OMPC_reduction) << 0;
2633 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2634 VarDecl::DeclarationOnly;
2635 Diag(VD->getLocation(),
2636 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2637 << VD;
2638 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2639 continue;
2640 }
2641 MarkFunctionReferenced(ELoc, CD);
2642 DiagnoseUseOfDecl(CD, ELoc);
2643
2644 CXXDestructorDecl *DD = RD->getDestructor();
2645 if (DD) {
2646 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2647 DD->isDeleted()) {
2648 Diag(ELoc, diag::err_omp_required_method)
2649 << getOpenMPClauseName(OMPC_reduction) << 4;
2650 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2651 VarDecl::DeclarationOnly;
2652 Diag(VD->getLocation(),
2653 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2654 << VD;
2655 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2656 continue;
2657 }
2658 MarkFunctionReferenced(ELoc, DD);
2659 DiagnoseUseOfDecl(DD, ELoc);
2660 }
2661 }
2662
2663 DSAStack->addDSA(VD, DE, OMPC_reduction);
2664 Vars.push_back(DE);
2665 }
2666
2667 if (Vars.empty())
2668 return nullptr;
2669
2670 return OMPReductionClause::Create(
2671 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
2672 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
2673}
2674
Alexander Musman8dba6642014-04-22 13:09:42 +00002675OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2676 SourceLocation StartLoc,
2677 SourceLocation LParenLoc,
2678 SourceLocation ColonLoc,
2679 SourceLocation EndLoc) {
2680 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002681 for (auto &RefExpr : VarList) {
2682 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2683 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002684 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002685 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002686 continue;
2687 }
2688
2689 // OpenMP [2.14.3.7, linear clause]
2690 // A list item that appears in a linear clause is subject to the private
2691 // clause semantics described in Section 2.14.3.3 on page 159 except as
2692 // noted. In addition, the value of the new list item on each iteration
2693 // of the associated loop(s) corresponds to the value of the original
2694 // list item before entering the construct plus the logical number of
2695 // the iteration times linear-step.
2696
Alexey Bataeved09d242014-05-28 05:53:51 +00002697 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00002698 // OpenMP [2.1, C/C++]
2699 // A list item is a variable name.
2700 // OpenMP [2.14.3.3, Restrictions, p.1]
2701 // A variable that is part of another variable (as an array or
2702 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002703 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002704 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002705 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00002706 continue;
2707 }
2708
2709 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2710
2711 // OpenMP [2.14.3.7, linear clause]
2712 // A list-item cannot appear in more than one linear clause.
2713 // A list-item that appears in a linear clause cannot appear in any
2714 // other data-sharing attribute clause.
2715 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2716 if (DVar.RefExpr) {
2717 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2718 << getOpenMPClauseName(OMPC_linear);
2719 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2720 << getOpenMPClauseName(DVar.CKind);
2721 continue;
2722 }
2723
2724 QualType QType = VD->getType();
2725 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2726 // It will be analyzed later.
2727 Vars.push_back(DE);
2728 continue;
2729 }
2730
2731 // A variable must not have an incomplete type or a reference type.
2732 if (RequireCompleteType(ELoc, QType,
2733 diag::err_omp_linear_incomplete_type)) {
2734 continue;
2735 }
2736 if (QType->isReferenceType()) {
2737 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2738 << getOpenMPClauseName(OMPC_linear) << QType;
2739 bool IsDecl =
2740 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2741 Diag(VD->getLocation(),
2742 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2743 << VD;
2744 continue;
2745 }
2746
2747 // A list item must not be const-qualified.
2748 if (QType.isConstant(Context)) {
2749 Diag(ELoc, diag::err_omp_const_variable)
2750 << getOpenMPClauseName(OMPC_linear);
2751 bool IsDecl =
2752 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2753 Diag(VD->getLocation(),
2754 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2755 << VD;
2756 continue;
2757 }
2758
2759 // A list item must be of integral or pointer type.
2760 QType = QType.getUnqualifiedType().getCanonicalType();
2761 const Type *Ty = QType.getTypePtrOrNull();
2762 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
2763 !Ty->isPointerType())) {
2764 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
2765 bool IsDecl =
2766 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2767 Diag(VD->getLocation(),
2768 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2769 << VD;
2770 continue;
2771 }
2772
2773 DSAStack->addDSA(VD, DE, OMPC_linear);
2774 Vars.push_back(DE);
2775 }
2776
2777 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002778 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00002779
2780 Expr *StepExpr = Step;
2781 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2782 !Step->isInstantiationDependent() &&
2783 !Step->containsUnexpandedParameterPack()) {
2784 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002785 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00002786 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002787 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002788 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00002789
2790 // Warn about zero linear step (it would be probably better specified as
2791 // making corresponding variables 'const').
2792 llvm::APSInt Result;
2793 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
2794 !Result.isNegative() && !Result.isStrictlyPositive())
2795 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
2796 << (Vars.size() > 1);
2797 }
2798
2799 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
2800 Vars, StepExpr);
2801}
2802
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002803OMPClause *Sema::ActOnOpenMPAlignedClause(
2804 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
2805 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
2806
2807 SmallVector<Expr *, 8> Vars;
2808 for (auto &RefExpr : VarList) {
2809 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
2810 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2811 // It will be analyzed later.
2812 Vars.push_back(RefExpr);
2813 continue;
2814 }
2815
2816 SourceLocation ELoc = RefExpr->getExprLoc();
2817 // OpenMP [2.1, C/C++]
2818 // A list item is a variable name.
2819 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
2820 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2821 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2822 continue;
2823 }
2824
2825 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2826
2827 // OpenMP [2.8.1, simd construct, Restrictions]
2828 // The type of list items appearing in the aligned clause must be
2829 // array, pointer, reference to array, or reference to pointer.
2830 QualType QType = DE->getType()
2831 .getNonReferenceType()
2832 .getUnqualifiedType()
2833 .getCanonicalType();
2834 const Type *Ty = QType.getTypePtrOrNull();
2835 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
2836 !Ty->isPointerType())) {
2837 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
2838 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
2839 bool IsDecl =
2840 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2841 Diag(VD->getLocation(),
2842 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2843 << VD;
2844 continue;
2845 }
2846
2847 // OpenMP [2.8.1, simd construct, Restrictions]
2848 // A list-item cannot appear in more than one aligned clause.
2849 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
2850 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
2851 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
2852 << getOpenMPClauseName(OMPC_aligned);
2853 continue;
2854 }
2855
2856 Vars.push_back(DE);
2857 }
2858
2859 // OpenMP [2.8.1, simd construct, Description]
2860 // The parameter of the aligned clause, alignment, must be a constant
2861 // positive integer expression.
2862 // If no optional parameter is specified, implementation-defined default
2863 // alignments for SIMD instructions on the target platforms are assumed.
2864 if (Alignment != nullptr) {
2865 ExprResult AlignResult =
2866 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
2867 if (AlignResult.isInvalid())
2868 return nullptr;
2869 Alignment = AlignResult.get();
2870 }
2871 if (Vars.empty())
2872 return nullptr;
2873
2874 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
2875 EndLoc, Vars, Alignment);
2876}
2877
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002878OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
2879 SourceLocation StartLoc,
2880 SourceLocation LParenLoc,
2881 SourceLocation EndLoc) {
2882 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002883 for (auto &RefExpr : VarList) {
2884 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
2885 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002886 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002887 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002888 continue;
2889 }
2890
Alexey Bataeved09d242014-05-28 05:53:51 +00002891 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002892 // OpenMP [2.1, C/C++]
2893 // A list item is a variable name.
2894 // OpenMP [2.14.4.1, Restrictions, p.1]
2895 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00002896 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002897 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002898 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002899 continue;
2900 }
2901
2902 Decl *D = DE->getDecl();
2903 VarDecl *VD = cast<VarDecl>(D);
2904
2905 QualType Type = VD->getType();
2906 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2907 // It will be analyzed later.
2908 Vars.push_back(DE);
2909 continue;
2910 }
2911
2912 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
2913 // A list item that appears in a copyin clause must be threadprivate.
2914 if (!DSAStack->isThreadPrivate(VD)) {
2915 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00002916 << getOpenMPClauseName(OMPC_copyin)
2917 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002918 continue;
2919 }
2920
2921 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
2922 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00002923 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002924 // operator for the class type.
2925 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002926 CXXRecordDecl *RD =
2927 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002928 // FIXME This code must be replaced by actual assignment of the
2929 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002930 if (RD) {
2931 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2932 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002933 if (MD) {
2934 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2935 MD->isDeleted()) {
2936 Diag(ELoc, diag::err_omp_required_method)
2937 << getOpenMPClauseName(OMPC_copyin) << 2;
2938 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2939 VarDecl::DeclarationOnly;
2940 Diag(VD->getLocation(),
2941 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2942 << VD;
2943 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2944 continue;
2945 }
2946 MarkFunctionReferenced(ELoc, MD);
2947 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002948 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002949 }
2950
2951 DSAStack->addDSA(VD, DE, OMPC_copyin);
2952 Vars.push_back(DE);
2953 }
2954
Alexey Bataeved09d242014-05-28 05:53:51 +00002955 if (Vars.empty())
2956 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002957
2958 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2959}
2960
Alexey Bataev758e55e2013-09-06 18:03:48 +00002961#undef DSAStack