blob: 5cf47c0ac12a32c8bba44d44e2b68ab75c5f8cc9 [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 Bataev7ff55242014-06-19 09:13:45 +000042
Alexey Bataevf29276e2014-06-18 04:14:57 +000043template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000044 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000045 bool operator()(T Kind) {
46 for (auto KindEl : Arr)
47 if (KindEl == Kind)
48 return true;
49 return false;
50 }
51
52private:
53 ArrayRef<T> Arr;
54};
Alexey Bataev23b69422014-06-18 07:08:49 +000055struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000056 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000057 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000058};
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;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000103 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104
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 Bataev7ff55242014-06-19 09:13:45 +0000113 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(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 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000155 /// \brief Returns parent directive.
156 OpenMPDirectiveKind getParentDirective() const {
157 if (Stack.size() > 2)
158 return Stack[Stack.size() - 2].Directive;
159 return OMPD_unknown;
160 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000161
162 /// \brief Set default data sharing attribute to none.
163 void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
164 /// \brief Set default data sharing attribute to shared.
165 void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
166
167 DefaultDataSharingAttributes getDefaultDSA() const {
168 return Stack.back().DefaultAttr;
169 }
170
Alexey Bataevf29276e2014-06-18 04:14:57 +0000171 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000172 bool isThreadPrivate(VarDecl *D) {
173 DSAVarData DVar = getTopDSA(D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000174 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000175 }
176
177 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000178 Scope *getCurScope() { return Stack.back().CurScope; }
179};
Alexey Bataeved09d242014-05-28 05:53:51 +0000180} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000181
182DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
183 VarDecl *D) {
184 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000185 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000186 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
187 // in a region but not in construct]
188 // File-scope or namespace-scope variables referenced in called routines
189 // in the region are shared unless they appear in a threadprivate
190 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000191 if (!D->isFunctionOrMethodVarDecl())
192 DVar.CKind = OMPC_shared;
193
194 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
195 // in a region but not in construct]
196 // Variables with static storage duration that are declared in called
197 // routines in the region are shared.
198 if (D->hasGlobalStorage())
199 DVar.CKind = OMPC_shared;
200
Alexey Bataev758e55e2013-09-06 18:03:48 +0000201 return DVar;
202 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000203
Alexey Bataev758e55e2013-09-06 18:03:48 +0000204 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000205 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
206 // in a Construct, C/C++, predetermined, p.1]
207 // Variables with automatic storage duration that are declared in a scope
208 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000209 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
210 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
211 DVar.CKind = OMPC_private;
212 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000213 }
214
Alexey Bataev758e55e2013-09-06 18:03:48 +0000215 // Explicitly specified attributes and local variables with predetermined
216 // attributes.
217 if (Iter->SharingMap.count(D)) {
218 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
219 DVar.CKind = Iter->SharingMap[D].Attributes;
220 return DVar;
221 }
222
223 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
224 // in a Construct, C/C++, implicitly determined, p.1]
225 // In a parallel or task construct, the data-sharing attributes of these
226 // variables are determined by the default clause, if present.
227 switch (Iter->DefaultAttr) {
228 case DSA_shared:
229 DVar.CKind = OMPC_shared;
230 return DVar;
231 case DSA_none:
232 return DVar;
233 case DSA_unspecified:
234 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
235 // in a Construct, implicitly determined, p.2]
236 // In a parallel construct, if no default clause is present, these
237 // variables are shared.
Alexey Bataevcefffae2014-06-23 08:21:53 +0000238 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000239 DVar.CKind = OMPC_shared;
240 return DVar;
241 }
242
243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
244 // in a Construct, implicitly determined, p.4]
245 // In a task construct, if no default clause is present, a variable that in
246 // the enclosing context is determined to be shared by all implicit tasks
247 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000248 if (DVar.DKind == OMPD_task) {
249 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000250 for (StackTy::reverse_iterator I = std::next(Iter),
251 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000252 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000253 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
254 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000255 // in a Construct, implicitly determined, p.6]
256 // In a task construct, if no default clause is present, a variable
257 // whose data-sharing attribute is not determined by the rules above is
258 // firstprivate.
259 DVarTemp = getDSA(I, D);
260 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000261 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000262 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000263 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264 return DVar;
265 }
Alexey Bataevcefffae2014-06-23 08:21:53 +0000266 if (isOpenMPParallelDirective(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000267 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000268 }
269 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000271 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000272 return DVar;
273 }
274 }
275 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
276 // in a Construct, implicitly determined, p.3]
277 // For constructs other than task, if no default clause is present, these
278 // variables inherit their data-sharing attributes from the enclosing
279 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000280 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000281}
282
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000283DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
284 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
285 auto It = Stack.back().AlignedMap.find(D);
286 if (It == Stack.back().AlignedMap.end()) {
287 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
288 Stack.back().AlignedMap[D] = NewDE;
289 return nullptr;
290 } else {
291 assert(It->second && "Unexpected nullptr expr in the aligned map");
292 return It->second;
293 }
294 return nullptr;
295}
296
Alexey Bataev758e55e2013-09-06 18:03:48 +0000297void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
298 if (A == OMPC_threadprivate) {
299 Stack[0].SharingMap[D].Attributes = A;
300 Stack[0].SharingMap[D].RefExpr = E;
301 } else {
302 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
303 Stack.back().SharingMap[D].Attributes = A;
304 Stack.back().SharingMap[D].RefExpr = E;
305 }
306}
307
Alexey Bataeved09d242014-05-28 05:53:51 +0000308bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000309 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000310 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000311 Scope *TopScope = nullptr;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000312 while (I != E && !isOpenMPParallelDirective(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000313 ++I;
314 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000315 if (I == E)
316 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000317 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000318 Scope *CurScope = getCurScope();
319 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000320 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000321 }
322 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000324 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000325}
326
327DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
328 DSAVarData DVar;
329
330 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
331 // in a Construct, C/C++, predetermined, p.1]
332 // Variables appearing in threadprivate directives are threadprivate.
333 if (D->getTLSKind() != VarDecl::TLS_None) {
334 DVar.CKind = OMPC_threadprivate;
335 return DVar;
336 }
337 if (Stack[0].SharingMap.count(D)) {
338 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
339 DVar.CKind = OMPC_threadprivate;
340 return DVar;
341 }
342
343 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
344 // in a Construct, C/C++, predetermined, p.1]
345 // Variables with automatic storage duration that are declared in a scope
346 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000347 OpenMPDirectiveKind Kind = getCurrentDirective();
Alexey Bataevcefffae2014-06-23 08:21:53 +0000348 if (!isOpenMPParallelDirective(Kind)) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000349 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000350 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000351 DVar.CKind = OMPC_private;
352 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000353 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000354 }
355
356 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
357 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000358 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000359 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000360 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000361 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ff55242014-06-19 09:13:45 +0000362 DSAVarData DVarTemp =
363 hasDSA(D, MatchesAnyClause(OMPC_firstprivate), MatchesAlways());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000364 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
365 return DVar;
366
Alexey Bataev758e55e2013-09-06 18:03:48 +0000367 DVar.CKind = OMPC_shared;
368 return DVar;
369 }
370
371 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000372 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 while (Type->isArrayType()) {
374 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
375 Type = ElemType.getNonReferenceType().getCanonicalType();
376 }
377 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
378 // in a Construct, C/C++, predetermined, p.6]
379 // Variables with const qualified type having no mutable member are
380 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000381 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000382 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000384 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385 // Variables with const-qualified type having no mutable member may be
386 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ff55242014-06-19 09:13:45 +0000387 DSAVarData DVarTemp =
388 hasDSA(D, MatchesAnyClause(OMPC_firstprivate), MatchesAlways());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000389 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
390 return DVar;
391
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392 DVar.CKind = OMPC_shared;
393 return DVar;
394 }
395
396 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
397 // in a Construct, C/C++, predetermined, p.7]
398 // Variables with static storage duration that are declared in a scope
399 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000400 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401 DVar.CKind = OMPC_shared;
402 return DVar;
403 }
404
405 // Explicitly specified attributes and local variables with predetermined
406 // attributes.
407 if (Stack.back().SharingMap.count(D)) {
408 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
409 DVar.CKind = Stack.back().SharingMap[D].Attributes;
410 }
411
412 return DVar;
413}
414
415DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000416 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000417}
418
Alexey Bataevf29276e2014-06-18 04:14:57 +0000419template <class ClausesPredicate, class DirectivesPredicate>
420DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
421 DirectivesPredicate DPred) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000422 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
423 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000424 I != E; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000425 if (!DPred(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000426 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000427 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000428 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000429 return DVar;
430 }
431 return DSAVarData();
432}
433
Alexey Bataevf29276e2014-06-18 04:14:57 +0000434template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataevc5e02582014-06-16 07:08:35 +0000435DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(VarDecl *D,
Alexey Bataevf29276e2014-06-18 04:14:57 +0000436 ClausesPredicate CPred,
437 DirectivesPredicate DPred) {
Alexey Bataevc5e02582014-06-16 07:08:35 +0000438 for (auto I = Stack.rbegin(), EE = std::prev(Stack.rend()); I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000439 if (!DPred(I->Directive))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000440 continue;
441 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000442 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000443 return DVar;
444 return DSAVarData();
445 }
446 return DSAVarData();
447}
448
Alexey Bataev758e55e2013-09-06 18:03:48 +0000449void Sema::InitDataSharingAttributesStack() {
450 VarDataSharingAttributesStack = new DSAStackTy(*this);
451}
452
453#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
454
Alexey Bataeved09d242014-05-28 05:53:51 +0000455void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456
457void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
458 const DeclarationNameInfo &DirName,
459 Scope *CurScope) {
460 DSAStack->push(DKind, DirName, CurScope);
461 PushExpressionEvaluationContext(PotentiallyEvaluated);
462}
463
464void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000465 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
466 // A variable of class type (or array thereof) that appears in a lastprivate
467 // clause requires an accessible, unambiguous default constructor for the
468 // class type, unless the list item is also specified in a firstprivate
469 // clause.
470 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
471 for (auto C : D->clauses()) {
472 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
473 for (auto VarRef : Clause->varlists()) {
474 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
475 continue;
476 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
477 auto DVar = DSAStack->getTopDSA(VD);
478 if (DVar.CKind == OMPC_lastprivate) {
479 SourceLocation ELoc = VarRef->getExprLoc();
480 auto Type = VarRef->getType();
481 if (Type->isArrayType())
482 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
483 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000484 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
485 // FIXME This code must be replaced by actual constructing of the
486 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000487 if (RD) {
488 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
489 PartialDiagnostic PD =
490 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
491 if (!CD ||
492 CheckConstructorAccess(
493 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
494 CD->getAccess(), PD) == AR_inaccessible ||
495 CD->isDeleted()) {
496 Diag(ELoc, diag::err_omp_required_method)
497 << getOpenMPClauseName(OMPC_lastprivate) << 0;
498 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
499 VarDecl::DeclarationOnly;
500 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
501 : diag::note_defined_here)
502 << VD;
503 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
504 continue;
505 }
506 MarkFunctionReferenced(ELoc, CD);
507 DiagnoseUseOfDecl(CD, ELoc);
508 }
509 }
510 }
511 }
512 }
513 }
514
Alexey Bataev758e55e2013-09-06 18:03:48 +0000515 DSAStack->pop();
516 DiscardCleanupsInEvaluationContext();
517 PopExpressionEvaluationContext();
518}
519
Alexey Bataeva769e072013-03-22 06:34:35 +0000520namespace {
521
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000522class VarDeclFilterCCC : public CorrectionCandidateCallback {
523private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000524 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000525
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000526public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000527 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000528 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000529 NamedDecl *ND = Candidate.getCorrectionDecl();
530 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
531 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000532 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
533 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000534 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000535 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000536 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000537};
Alexey Bataeved09d242014-05-28 05:53:51 +0000538} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000539
540ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
541 CXXScopeSpec &ScopeSpec,
542 const DeclarationNameInfo &Id) {
543 LookupResult Lookup(*this, Id, LookupOrdinaryName);
544 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
545
546 if (Lookup.isAmbiguous())
547 return ExprError();
548
549 VarDecl *VD;
550 if (!Lookup.isSingleResult()) {
551 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000552 if (TypoCorrection Corrected =
553 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
554 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000555 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000556 PDiag(Lookup.empty()
557 ? diag::err_undeclared_var_use_suggest
558 : diag::err_omp_expected_var_arg_suggest)
559 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000560 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000561 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000562 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
563 : diag::err_omp_expected_var_arg)
564 << Id.getName();
565 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000566 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000567 } else {
568 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000569 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000570 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
571 return ExprError();
572 }
573 }
574 Lookup.suppressDiagnostics();
575
576 // OpenMP [2.9.2, Syntax, C/C++]
577 // Variables must be file-scope, namespace-scope, or static block-scope.
578 if (!VD->hasGlobalStorage()) {
579 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000580 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
581 bool IsDecl =
582 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000583 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000584 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
585 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000586 return ExprError();
587 }
588
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000589 VarDecl *CanonicalVD = VD->getCanonicalDecl();
590 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000591 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
592 // A threadprivate directive for file-scope variables must appear outside
593 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000594 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
595 !getCurLexicalContext()->isTranslationUnit()) {
596 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000597 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
598 bool IsDecl =
599 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
600 Diag(VD->getLocation(),
601 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
602 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000603 return ExprError();
604 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000605 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
606 // A threadprivate directive for static class member variables must appear
607 // in the class definition, in the same scope in which the member
608 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000609 if (CanonicalVD->isStaticDataMember() &&
610 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
611 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000612 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
613 bool IsDecl =
614 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
615 Diag(VD->getLocation(),
616 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
617 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000618 return ExprError();
619 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000620 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
621 // A threadprivate directive for namespace-scope variables must appear
622 // outside any definition or declaration other than the namespace
623 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000624 if (CanonicalVD->getDeclContext()->isNamespace() &&
625 (!getCurLexicalContext()->isFileContext() ||
626 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
627 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000628 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
629 bool IsDecl =
630 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
631 Diag(VD->getLocation(),
632 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
633 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000634 return ExprError();
635 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000636 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
637 // A threadprivate directive for static block-scope variables must appear
638 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000639 if (CanonicalVD->isStaticLocal() && CurScope &&
640 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000641 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000642 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
643 bool IsDecl =
644 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
645 Diag(VD->getLocation(),
646 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
647 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000648 return ExprError();
649 }
650
651 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
652 // A threadprivate directive must lexically precede all references to any
653 // of the variables in its list.
654 if (VD->isUsed()) {
655 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000656 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000657 return ExprError();
658 }
659
660 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000661 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000662 return DE;
663}
664
Alexey Bataeved09d242014-05-28 05:53:51 +0000665Sema::DeclGroupPtrTy
666Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
667 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000668 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000669 CurContext->addDecl(D);
670 return DeclGroupPtrTy::make(DeclGroupRef(D));
671 }
672 return DeclGroupPtrTy();
673}
674
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000675namespace {
676class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
677 Sema &SemaRef;
678
679public:
680 bool VisitDeclRefExpr(const DeclRefExpr *E) {
681 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
682 if (VD->hasLocalStorage()) {
683 SemaRef.Diag(E->getLocStart(),
684 diag::err_omp_local_var_in_threadprivate_init)
685 << E->getSourceRange();
686 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
687 << VD << VD->getSourceRange();
688 return true;
689 }
690 }
691 return false;
692 }
693 bool VisitStmt(const Stmt *S) {
694 for (auto Child : S->children()) {
695 if (Child && Visit(Child))
696 return true;
697 }
698 return false;
699 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000700 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000701};
702} // namespace
703
Alexey Bataeved09d242014-05-28 05:53:51 +0000704OMPThreadPrivateDecl *
705Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000706 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000707 for (auto &RefExpr : VarList) {
708 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000709 VarDecl *VD = cast<VarDecl>(DE->getDecl());
710 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000711
712 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
713 // A threadprivate variable must not have an incomplete type.
714 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000715 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000716 continue;
717 }
718
719 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
720 // A threadprivate variable must not have a reference type.
721 if (VD->getType()->isReferenceType()) {
722 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000723 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
724 bool IsDecl =
725 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
726 Diag(VD->getLocation(),
727 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
728 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000729 continue;
730 }
731
Richard Smithfd3834f2013-04-13 02:43:54 +0000732 // Check if this is a TLS variable.
733 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000734 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000735 bool IsDecl =
736 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
737 Diag(VD->getLocation(),
738 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
739 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000740 continue;
741 }
742
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000743 // Check if initial value of threadprivate variable reference variable with
744 // local storage (it is not supported by runtime).
745 if (auto Init = VD->getAnyInitializer()) {
746 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000747 if (Checker.Visit(Init))
748 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000749 }
750
Alexey Bataeved09d242014-05-28 05:53:51 +0000751 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000752 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000753 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000754 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000755 if (!Vars.empty()) {
756 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
757 Vars);
758 D->setAccess(AS_public);
759 }
760 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000761}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000762
Alexey Bataev7ff55242014-06-19 09:13:45 +0000763static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
764 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
765 bool IsLoopIterVar = false) {
766 if (DVar.RefExpr) {
767 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
768 << getOpenMPClauseName(DVar.CKind);
769 return;
770 }
771 enum {
772 PDSA_StaticMemberShared,
773 PDSA_StaticLocalVarShared,
774 PDSA_LoopIterVarPrivate,
775 PDSA_LoopIterVarLinear,
776 PDSA_LoopIterVarLastprivate,
777 PDSA_ConstVarShared,
778 PDSA_GlobalVarShared,
779 PDSA_LocalVarPrivate
780 } Reason;
781 bool ReportHint = false;
782 if (IsLoopIterVar) {
783 if (DVar.CKind == OMPC_private)
784 Reason = PDSA_LoopIterVarPrivate;
785 else if (DVar.CKind == OMPC_lastprivate)
786 Reason = PDSA_LoopIterVarLastprivate;
787 else
788 Reason = PDSA_LoopIterVarLinear;
789 } else if (VD->isStaticLocal())
790 Reason = PDSA_StaticLocalVarShared;
791 else if (VD->isStaticDataMember())
792 Reason = PDSA_StaticMemberShared;
793 else if (VD->isFileVarDecl())
794 Reason = PDSA_GlobalVarShared;
795 else if (VD->getType().isConstant(SemaRef.getASTContext()))
796 Reason = PDSA_ConstVarShared;
797 else {
798 ReportHint = true;
799 Reason = PDSA_LocalVarPrivate;
800 }
801
802 SemaRef.Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
803 << Reason << ReportHint
804 << getOpenMPDirectiveName(Stack->getCurrentDirective());
805}
806
Alexey Bataev758e55e2013-09-06 18:03:48 +0000807namespace {
808class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
809 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000810 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000811 bool ErrorFound;
812 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000813 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataeved09d242014-05-28 05:53:51 +0000814
Alexey Bataev758e55e2013-09-06 18:03:48 +0000815public:
816 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000817 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000818 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000819 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
820 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000821
822 SourceLocation ELoc = E->getExprLoc();
823
824 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
825 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
826 if (DVar.CKind != OMPC_unknown) {
827 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000828 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000829 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000830 return;
831 }
832 // The default(none) clause requires that each variable that is referenced
833 // in the construct, and does not have a predetermined data-sharing
834 // attribute, must have its data-sharing attribute explicitly determined
835 // by being listed in a data-sharing attribute clause.
836 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataevf29276e2014-06-18 04:14:57 +0000837 (isOpenMPParallelDirective(DKind) || DKind == OMPD_task)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000838 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000839 SemaRef.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000840 return;
841 }
842
843 // OpenMP [2.9.3.6, Restrictions, p.2]
844 // A list item that appears in a reduction clause of the innermost
845 // enclosing worksharing or parallel construct may not be accessed in an
846 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000847 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev23b69422014-06-18 07:08:49 +0000848 MatchesAlways());
Alexey Bataevc5e02582014-06-16 07:08:35 +0000849 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
850 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000851 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
852 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000853 return;
854 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000855
856 // Define implicit data-sharing attributes for task.
857 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000858 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
859 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000860 }
861 }
862 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000863 for (auto C : S->clauses())
864 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000865 for (StmtRange R = C->children(); R; ++R)
866 if (Stmt *Child = *R)
867 Visit(Child);
868 }
869 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000870 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
871 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000872 if (Stmt *Child = *I)
873 if (!isa<OMPExecutableDirective>(Child))
874 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000875 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000876
877 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000878 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000879
Alexey Bataev7ff55242014-06-19 09:13:45 +0000880 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
881 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000882};
Alexey Bataeved09d242014-05-28 05:53:51 +0000883} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000884
Alexey Bataev9959db52014-05-06 10:08:46 +0000885void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
886 Scope *CurScope) {
887 switch (DKind) {
888 case OMPD_parallel: {
889 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
890 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000891 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000892 std::make_pair(".global_tid.", KmpInt32PtrTy),
893 std::make_pair(".bound_tid.", KmpInt32PtrTy),
894 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000895 };
896 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
897 break;
898 }
899 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000900 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000901 std::make_pair(StringRef(), QualType()) // __context with shared vars
902 };
903 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
904 break;
905 }
906 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000907 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000908 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000909 };
910 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
911 break;
912 }
913 case OMPD_threadprivate:
914 case OMPD_task:
915 llvm_unreachable("OpenMP Directive is not allowed");
916 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000917 llvm_unreachable("Unknown OpenMP directive");
918 }
919}
920
Alexey Bataev549210e2014-06-24 04:39:47 +0000921bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
922 OpenMPDirectiveKind CurrentRegion,
923 SourceLocation StartLoc) {
924 if (Stack->getCurScope()) {
925 auto ParentRegion = Stack->getParentDirective();
926 bool NestingProhibited = false;
927 bool CloseNesting = true;
928 bool ShouldBeInParallelRegion = false;
929 if (isOpenMPSimdDirective(ParentRegion)) {
930 // OpenMP [2.16, Nesting of Regions]
931 // OpenMP constructs may not be nested inside a simd region.
932 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
933 return true;
934 }
935 if (isOpenMPWorksharingDirective(CurrentRegion) &&
936 !isOpenMPParallelDirective(CurrentRegion) &&
937 !isOpenMPSimdDirective(CurrentRegion)) {
938 // OpenMP [2.16, Nesting of Regions]
939 // A worksharing region may not be closely nested inside a worksharing,
940 // explicit task, critical, ordered, atomic, or master region.
941 // TODO
942 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) &&
943 !isOpenMPSimdDirective(ParentRegion);
944 ShouldBeInParallelRegion = true;
945 }
946 if (NestingProhibited) {
947 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
948 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << true
949 << getOpenMPDirectiveName(CurrentRegion) << ShouldBeInParallelRegion;
950 return true;
951 }
952 }
953 return false;
954}
955
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000956StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
957 ArrayRef<OMPClause *> Clauses,
958 Stmt *AStmt,
959 SourceLocation StartLoc,
960 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000961 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
962
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000963 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +0000964 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
965 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000966
967 // Check default data sharing attributes for referenced variables.
968 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
969 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
970 if (DSAChecker.isErrorFound())
971 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000972 // Generate list of implicitly defined firstprivate variables.
973 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
974 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
975
976 bool ErrorFound = false;
977 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000978 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
979 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
980 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000981 ClausesWithImplicit.push_back(Implicit);
982 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +0000983 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000984 } else
985 ErrorFound = true;
986 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000987
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000988 switch (Kind) {
989 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +0000990 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
991 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000992 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000993 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +0000994 Res =
995 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000996 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 case OMPD_for:
998 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
999 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001000 case OMPD_threadprivate:
1001 case OMPD_task:
1002 llvm_unreachable("OpenMP Directive is not allowed");
1003 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001004 llvm_unreachable("Unknown OpenMP directive");
1005 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001006
Alexey Bataeved09d242014-05-28 05:53:51 +00001007 if (ErrorFound)
1008 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001009 return Res;
1010}
1011
1012StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1013 Stmt *AStmt,
1014 SourceLocation StartLoc,
1015 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001016 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1017 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1018 // 1.2.2 OpenMP Language Terminology
1019 // Structured block - An executable statement with a single entry at the
1020 // top and a single exit at the bottom.
1021 // The point of exit cannot be a branch out of the structured block.
1022 // longjmp() and throw() must not violate the entry/exit criteria.
1023 CS->getCapturedDecl()->setNothrow();
1024
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001025 getCurFunction()->setHasBranchProtectedScope();
1026
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001027 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1028 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001029}
1030
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001031namespace {
1032/// \brief Helper class for checking canonical form of the OpenMP loops and
1033/// extracting iteration space of each loop in the loop nest, that will be used
1034/// for IR generation.
1035class OpenMPIterationSpaceChecker {
1036 /// \brief Reference to Sema.
1037 Sema &SemaRef;
1038 /// \brief A location for diagnostics (when there is no some better location).
1039 SourceLocation DefaultLoc;
1040 /// \brief A location for diagnostics (when increment is not compatible).
1041 SourceLocation ConditionLoc;
1042 /// \brief A source location for referring to condition later.
1043 SourceRange ConditionSrcRange;
1044 /// \brief Loop variable.
1045 VarDecl *Var;
1046 /// \brief Lower bound (initializer for the var).
1047 Expr *LB;
1048 /// \brief Upper bound.
1049 Expr *UB;
1050 /// \brief Loop step (increment).
1051 Expr *Step;
1052 /// \brief This flag is true when condition is one of:
1053 /// Var < UB
1054 /// Var <= UB
1055 /// UB > Var
1056 /// UB >= Var
1057 bool TestIsLessOp;
1058 /// \brief This flag is true when condition is strict ( < or > ).
1059 bool TestIsStrictOp;
1060 /// \brief This flag is true when step is subtracted on each iteration.
1061 bool SubtractStep;
1062
1063public:
1064 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1065 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1066 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1067 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1068 SubtractStep(false) {}
1069 /// \brief Check init-expr for canonical loop form and save loop counter
1070 /// variable - #Var and its initialization value - #LB.
1071 bool CheckInit(Stmt *S);
1072 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1073 /// for less/greater and for strict/non-strict comparison.
1074 bool CheckCond(Expr *S);
1075 /// \brief Check incr-expr for canonical loop form and return true if it
1076 /// does not conform, otherwise save loop step (#Step).
1077 bool CheckInc(Expr *S);
1078 /// \brief Return the loop counter variable.
1079 VarDecl *GetLoopVar() const { return Var; }
1080 /// \brief Return true if any expression is dependent.
1081 bool Dependent() const;
1082
1083private:
1084 /// \brief Check the right-hand side of an assignment in the increment
1085 /// expression.
1086 bool CheckIncRHS(Expr *RHS);
1087 /// \brief Helper to set loop counter variable and its initializer.
1088 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1089 /// \brief Helper to set upper bound.
1090 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1091 const SourceLocation &SL);
1092 /// \brief Helper to set loop increment.
1093 bool SetStep(Expr *NewStep, bool Subtract);
1094};
1095
1096bool OpenMPIterationSpaceChecker::Dependent() const {
1097 if (!Var) {
1098 assert(!LB && !UB && !Step);
1099 return false;
1100 }
1101 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1102 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1103}
1104
1105bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1106 // State consistency checking to ensure correct usage.
1107 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1108 !TestIsLessOp && !TestIsStrictOp);
1109 if (!NewVar || !NewLB)
1110 return true;
1111 Var = NewVar;
1112 LB = NewLB;
1113 return false;
1114}
1115
1116bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1117 const SourceRange &SR,
1118 const SourceLocation &SL) {
1119 // State consistency checking to ensure correct usage.
1120 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1121 !TestIsLessOp && !TestIsStrictOp);
1122 if (!NewUB)
1123 return true;
1124 UB = NewUB;
1125 TestIsLessOp = LessOp;
1126 TestIsStrictOp = StrictOp;
1127 ConditionSrcRange = SR;
1128 ConditionLoc = SL;
1129 return false;
1130}
1131
1132bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1133 // State consistency checking to ensure correct usage.
1134 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1135 if (!NewStep)
1136 return true;
1137 if (!NewStep->isValueDependent()) {
1138 // Check that the step is integer expression.
1139 SourceLocation StepLoc = NewStep->getLocStart();
1140 ExprResult Val =
1141 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1142 if (Val.isInvalid())
1143 return true;
1144 NewStep = Val.get();
1145
1146 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1147 // If test-expr is of form var relational-op b and relational-op is < or
1148 // <= then incr-expr must cause var to increase on each iteration of the
1149 // loop. If test-expr is of form var relational-op b and relational-op is
1150 // > or >= then incr-expr must cause var to decrease on each iteration of
1151 // the loop.
1152 // If test-expr is of form b relational-op var and relational-op is < or
1153 // <= then incr-expr must cause var to decrease on each iteration of the
1154 // loop. If test-expr is of form b relational-op var and relational-op is
1155 // > or >= then incr-expr must cause var to increase on each iteration of
1156 // the loop.
1157 llvm::APSInt Result;
1158 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1159 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1160 bool IsConstNeg =
1161 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1162 bool IsConstZero = IsConstant && !Result.getBoolValue();
1163 if (UB && (IsConstZero ||
1164 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1165 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1166 SemaRef.Diag(NewStep->getExprLoc(),
1167 diag::err_omp_loop_incr_not_compatible)
1168 << Var << TestIsLessOp << NewStep->getSourceRange();
1169 SemaRef.Diag(ConditionLoc,
1170 diag::note_omp_loop_cond_requres_compatible_incr)
1171 << TestIsLessOp << ConditionSrcRange;
1172 return true;
1173 }
1174 }
1175
1176 Step = NewStep;
1177 SubtractStep = Subtract;
1178 return false;
1179}
1180
1181bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1182 // Check init-expr for canonical loop form and save loop counter
1183 // variable - #Var and its initialization value - #LB.
1184 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1185 // var = lb
1186 // integer-type var = lb
1187 // random-access-iterator-type var = lb
1188 // pointer-type var = lb
1189 //
1190 if (!S) {
1191 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1192 return true;
1193 }
1194 if (Expr *E = dyn_cast<Expr>(S))
1195 S = E->IgnoreParens();
1196 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1197 if (BO->getOpcode() == BO_Assign)
1198 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1199 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1200 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1201 if (DS->isSingleDecl()) {
1202 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1203 if (Var->hasInit()) {
1204 // Accept non-canonical init form here but emit ext. warning.
1205 if (Var->getInitStyle() != VarDecl::CInit)
1206 SemaRef.Diag(S->getLocStart(),
1207 diag::ext_omp_loop_not_canonical_init)
1208 << S->getSourceRange();
1209 return SetVarAndLB(Var, Var->getInit());
1210 }
1211 }
1212 }
1213 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1214 if (CE->getOperator() == OO_Equal)
1215 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1216 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1217
1218 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1219 << S->getSourceRange();
1220 return true;
1221}
1222
Alexey Bataev23b69422014-06-18 07:08:49 +00001223/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001224/// variable (which may be the loop variable) if possible.
1225static const VarDecl *GetInitVarDecl(const Expr *E) {
1226 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001227 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001228 E = E->IgnoreParenImpCasts();
1229 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1230 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1231 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1232 CE->getArg(0) != nullptr)
1233 E = CE->getArg(0)->IgnoreParenImpCasts();
1234 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1235 if (!DRE)
1236 return nullptr;
1237 return dyn_cast<VarDecl>(DRE->getDecl());
1238}
1239
1240bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1241 // Check test-expr for canonical form, save upper-bound UB, flags for
1242 // less/greater and for strict/non-strict comparison.
1243 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1244 // var relational-op b
1245 // b relational-op var
1246 //
1247 if (!S) {
1248 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1249 return true;
1250 }
1251 S = S->IgnoreParenImpCasts();
1252 SourceLocation CondLoc = S->getLocStart();
1253 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1254 if (BO->isRelationalOp()) {
1255 if (GetInitVarDecl(BO->getLHS()) == Var)
1256 return SetUB(BO->getRHS(),
1257 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1258 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1259 BO->getSourceRange(), BO->getOperatorLoc());
1260 if (GetInitVarDecl(BO->getRHS()) == Var)
1261 return SetUB(BO->getLHS(),
1262 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1263 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1264 BO->getSourceRange(), BO->getOperatorLoc());
1265 }
1266 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1267 if (CE->getNumArgs() == 2) {
1268 auto Op = CE->getOperator();
1269 switch (Op) {
1270 case OO_Greater:
1271 case OO_GreaterEqual:
1272 case OO_Less:
1273 case OO_LessEqual:
1274 if (GetInitVarDecl(CE->getArg(0)) == Var)
1275 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1276 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1277 CE->getOperatorLoc());
1278 if (GetInitVarDecl(CE->getArg(1)) == Var)
1279 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1280 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1281 CE->getOperatorLoc());
1282 break;
1283 default:
1284 break;
1285 }
1286 }
1287 }
1288 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1289 << S->getSourceRange() << Var;
1290 return true;
1291}
1292
1293bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1294 // RHS of canonical loop form increment can be:
1295 // var + incr
1296 // incr + var
1297 // var - incr
1298 //
1299 RHS = RHS->IgnoreParenImpCasts();
1300 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1301 if (BO->isAdditiveOp()) {
1302 bool IsAdd = BO->getOpcode() == BO_Add;
1303 if (GetInitVarDecl(BO->getLHS()) == Var)
1304 return SetStep(BO->getRHS(), !IsAdd);
1305 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1306 return SetStep(BO->getLHS(), false);
1307 }
1308 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1309 bool IsAdd = CE->getOperator() == OO_Plus;
1310 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1311 if (GetInitVarDecl(CE->getArg(0)) == Var)
1312 return SetStep(CE->getArg(1), !IsAdd);
1313 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1314 return SetStep(CE->getArg(0), false);
1315 }
1316 }
1317 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1318 << RHS->getSourceRange() << Var;
1319 return true;
1320}
1321
1322bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1323 // Check incr-expr for canonical loop form and return true if it
1324 // does not conform.
1325 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1326 // ++var
1327 // var++
1328 // --var
1329 // var--
1330 // var += incr
1331 // var -= incr
1332 // var = var + incr
1333 // var = incr + var
1334 // var = var - incr
1335 //
1336 if (!S) {
1337 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1338 return true;
1339 }
1340 S = S->IgnoreParens();
1341 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1342 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1343 return SetStep(
1344 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1345 (UO->isDecrementOp() ? -1 : 1)).get(),
1346 false);
1347 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1348 switch (BO->getOpcode()) {
1349 case BO_AddAssign:
1350 case BO_SubAssign:
1351 if (GetInitVarDecl(BO->getLHS()) == Var)
1352 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1353 break;
1354 case BO_Assign:
1355 if (GetInitVarDecl(BO->getLHS()) == Var)
1356 return CheckIncRHS(BO->getRHS());
1357 break;
1358 default:
1359 break;
1360 }
1361 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1362 switch (CE->getOperator()) {
1363 case OO_PlusPlus:
1364 case OO_MinusMinus:
1365 if (GetInitVarDecl(CE->getArg(0)) == Var)
1366 return SetStep(
1367 SemaRef.ActOnIntegerConstant(
1368 CE->getLocStart(),
1369 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1370 false);
1371 break;
1372 case OO_PlusEqual:
1373 case OO_MinusEqual:
1374 if (GetInitVarDecl(CE->getArg(0)) == Var)
1375 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1376 break;
1377 case OO_Equal:
1378 if (GetInitVarDecl(CE->getArg(0)) == Var)
1379 return CheckIncRHS(CE->getArg(1));
1380 break;
1381 default:
1382 break;
1383 }
1384 }
1385 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1386 << S->getSourceRange() << Var;
1387 return true;
1388}
Alexey Bataev23b69422014-06-18 07:08:49 +00001389} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001390
1391/// \brief Called on a for stmt to check and extract its iteration space
1392/// for further processing (such as collapsing).
1393static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001394 Sema &SemaRef, DSAStackTy &DSA,
1395 unsigned CurrentNestedLoopCount,
1396 unsigned NestedLoopCount,
1397 Expr *NestedLoopCountExpr) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001398 // OpenMP [2.6, Canonical Loop Form]
1399 // for (init-expr; test-expr; incr-expr) structured-block
1400 auto For = dyn_cast_or_null<ForStmt>(S);
1401 if (!For) {
1402 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001403 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1404 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1405 << CurrentNestedLoopCount;
1406 if (NestedLoopCount > 1)
1407 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1408 diag::note_omp_collapse_expr)
1409 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001410 return true;
1411 }
1412 assert(For->getBody());
1413
1414 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1415
1416 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001417 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001418 if (ISC.CheckInit(Init)) {
1419 return true;
1420 }
1421
1422 bool HasErrors = false;
1423
1424 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001425 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001426
1427 // OpenMP [2.6, Canonical Loop Form]
1428 // Var is one of the following:
1429 // A variable of signed or unsigned integer type.
1430 // For C++, a variable of a random access iterator type.
1431 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001432 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001433 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1434 !VarType->isPointerType() &&
1435 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1436 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1437 << SemaRef.getLangOpts().CPlusPlus;
1438 HasErrors = true;
1439 }
1440
1441 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1442 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001443 // The loop iteration variable in the associated for-loop of a simd construct
1444 // with just one associated for-loop may be listed in a linear clause with a
1445 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001446 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1447 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001448 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001449 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1450 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) ||
1451 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1452 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001453 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001454 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1455 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001456 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001457 HasErrors = true;
1458 } else {
1459 // Make the loop iteration variable private by default.
1460 DSA.addDSA(Var, nullptr, OMPC_private);
1461 }
1462
Alexey Bataev7ff55242014-06-19 09:13:45 +00001463 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001464
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001465 // Check test-expr.
1466 HasErrors |= ISC.CheckCond(For->getCond());
1467
1468 // Check incr-expr.
1469 HasErrors |= ISC.CheckInc(For->getInc());
1470
1471 if (ISC.Dependent())
1472 return HasErrors;
1473
1474 // FIXME: Build loop's iteration space representation.
1475 return HasErrors;
1476}
1477
1478/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1479/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1480/// to get the first for loop.
1481static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1482 if (IgnoreCaptured)
1483 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1484 S = CapS->getCapturedStmt();
1485 // OpenMP [2.8.1, simd construct, Restrictions]
1486 // All loops associated with the construct must be perfectly nested; that is,
1487 // there must be no intervening code nor any OpenMP directive between any two
1488 // loops.
1489 while (true) {
1490 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1491 S = AS->getSubStmt();
1492 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1493 if (CS->size() != 1)
1494 break;
1495 S = CS->body_back();
1496 } else
1497 break;
1498 }
1499 return S;
1500}
1501
1502/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001503static bool CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001504 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001505 unsigned NestedLoopCount = 1;
1506 if (NestedLoopCountExpr) {
1507 // Found 'collapse' clause - calculate collapse number.
1508 llvm::APSInt Result;
1509 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1510 NestedLoopCount = Result.getLimitedValue();
1511 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001512 // This is helper routine for loop directives (e.g., 'for', 'simd',
1513 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001514 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1515 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001516 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
1517 NestedLoopCount, NestedLoopCountExpr))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001518 return true;
1519 // Move on to the next nested for loop, or to the loop body.
1520 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1521 }
1522
1523 // FIXME: Build resulting iteration space for IR generation (collapsing
1524 // iteration spaces when loop count > 1 ('collapse' clause)).
1525 return false;
1526}
1527
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001528static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001529 auto CollapseFilter = [](const OMPClause *C) -> bool {
1530 return C->getClauseKind() == OMPC_collapse;
1531 };
1532 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1533 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001534 if (I)
1535 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1536 return nullptr;
1537}
1538
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001539StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001540 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001541 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001542 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001543 if (CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses),
1544 AStmt, *this, *DSAStack))
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001545 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001546
1547 getCurFunction()->setHasBranchProtectedScope();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001548 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001549}
1550
Alexey Bataevf29276e2014-06-18 04:14:57 +00001551StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses,
1552 Stmt *AStmt, SourceLocation StartLoc,
1553 SourceLocation EndLoc) {
1554 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001555 if (CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses),
1556 AStmt, *this, *DSAStack))
Alexey Bataevf29276e2014-06-18 04:14:57 +00001557 return StmtError();
1558
1559 getCurFunction()->setHasBranchProtectedScope();
1560 return OMPForDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1561}
1562
Alexey Bataeved09d242014-05-28 05:53:51 +00001563OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001564 SourceLocation StartLoc,
1565 SourceLocation LParenLoc,
1566 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001567 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001568 switch (Kind) {
1569 case OMPC_if:
1570 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1571 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001572 case OMPC_num_threads:
1573 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1574 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001575 case OMPC_safelen:
1576 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1577 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001578 case OMPC_collapse:
1579 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1580 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001581 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001582 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001583 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001584 case OMPC_private:
1585 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001586 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001587 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001588 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001589 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001590 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001591 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001592 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001593 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001594 case OMPC_threadprivate:
1595 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001596 llvm_unreachable("Clause is not allowed.");
1597 }
1598 return Res;
1599}
1600
Alexey Bataeved09d242014-05-28 05:53:51 +00001601OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001602 SourceLocation LParenLoc,
1603 SourceLocation EndLoc) {
1604 Expr *ValExpr = Condition;
1605 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1606 !Condition->isInstantiationDependent() &&
1607 !Condition->containsUnexpandedParameterPack()) {
1608 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001609 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001610 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001611 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001612
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001613 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001614 }
1615
1616 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1617}
1618
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001619ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1620 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001621 if (!Op)
1622 return ExprError();
1623
1624 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1625 public:
1626 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001627 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001628 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1629 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001630 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1631 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001632 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1633 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001634 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1635 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001636 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1637 QualType T,
1638 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001639 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1640 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001641 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1642 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001643 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001644 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001645 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001646 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1647 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001648 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1649 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001650 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1651 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001652 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001653 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001654 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001655 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1656 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001657 llvm_unreachable("conversion functions are permitted");
1658 }
1659 } ConvertDiagnoser;
1660 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1661}
1662
1663OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1664 SourceLocation StartLoc,
1665 SourceLocation LParenLoc,
1666 SourceLocation EndLoc) {
1667 Expr *ValExpr = NumThreads;
1668 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1669 !NumThreads->isInstantiationDependent() &&
1670 !NumThreads->containsUnexpandedParameterPack()) {
1671 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1672 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001673 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001674 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001675 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001676
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001677 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001678
1679 // OpenMP [2.5, Restrictions]
1680 // The num_threads expression must evaluate to a positive integer value.
1681 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001682 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1683 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001684 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1685 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001686 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001687 }
1688 }
1689
Alexey Bataeved09d242014-05-28 05:53:51 +00001690 return new (Context)
1691 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001692}
1693
Alexey Bataev62c87d22014-03-21 04:51:18 +00001694ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1695 OpenMPClauseKind CKind) {
1696 if (!E)
1697 return ExprError();
1698 if (E->isValueDependent() || E->isTypeDependent() ||
1699 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001700 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001701 llvm::APSInt Result;
1702 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1703 if (ICE.isInvalid())
1704 return ExprError();
1705 if (!Result.isStrictlyPositive()) {
1706 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1707 << getOpenMPClauseName(CKind) << E->getSourceRange();
1708 return ExprError();
1709 }
1710 return ICE;
1711}
1712
1713OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1714 SourceLocation LParenLoc,
1715 SourceLocation EndLoc) {
1716 // OpenMP [2.8.1, simd construct, Description]
1717 // The parameter of the safelen clause must be a constant
1718 // positive integer expression.
1719 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1720 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001721 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001722 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001723 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001724}
1725
Alexander Musman64d33f12014-06-04 07:53:32 +00001726OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1727 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001728 SourceLocation LParenLoc,
1729 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001730 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001731 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001732 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001733 // The parameter of the collapse clause must be a constant
1734 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001735 ExprResult NumForLoopsResult =
1736 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1737 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001738 return nullptr;
1739 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001740 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001741}
1742
Alexey Bataeved09d242014-05-28 05:53:51 +00001743OMPClause *Sema::ActOnOpenMPSimpleClause(
1744 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1745 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001746 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001747 switch (Kind) {
1748 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001749 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001750 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1751 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001752 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001753 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001754 Res = ActOnOpenMPProcBindClause(
1755 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1756 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001757 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001758 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001759 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001760 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001761 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001762 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001763 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001764 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001765 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001766 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001767 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001768 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001769 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001770 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001771 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001772 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001773 case OMPC_threadprivate:
1774 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001775 llvm_unreachable("Clause is not allowed.");
1776 }
1777 return Res;
1778}
1779
1780OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1781 SourceLocation KindKwLoc,
1782 SourceLocation StartLoc,
1783 SourceLocation LParenLoc,
1784 SourceLocation EndLoc) {
1785 if (Kind == OMPC_DEFAULT_unknown) {
1786 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001787 static_assert(OMPC_DEFAULT_unknown > 0,
1788 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001789 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001790 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001791 Values += "'";
1792 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1793 Values += "'";
1794 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001795 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001796 Values += " or ";
1797 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001798 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001799 break;
1800 default:
1801 Values += Sep;
1802 break;
1803 }
1804 }
1805 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001806 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001807 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001808 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001809 switch (Kind) {
1810 case OMPC_DEFAULT_none:
1811 DSAStack->setDefaultDSANone();
1812 break;
1813 case OMPC_DEFAULT_shared:
1814 DSAStack->setDefaultDSAShared();
1815 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001816 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001817 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001818 break;
1819 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001820 return new (Context)
1821 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001822}
1823
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001824OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1825 SourceLocation KindKwLoc,
1826 SourceLocation StartLoc,
1827 SourceLocation LParenLoc,
1828 SourceLocation EndLoc) {
1829 if (Kind == OMPC_PROC_BIND_unknown) {
1830 std::string Values;
1831 std::string Sep(", ");
1832 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1833 Values += "'";
1834 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1835 Values += "'";
1836 switch (i) {
1837 case OMPC_PROC_BIND_unknown - 2:
1838 Values += " or ";
1839 break;
1840 case OMPC_PROC_BIND_unknown - 1:
1841 break;
1842 default:
1843 Values += Sep;
1844 break;
1845 }
1846 }
1847 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001848 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001849 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001850 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001851 return new (Context)
1852 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001853}
1854
Alexey Bataev56dafe82014-06-20 07:16:17 +00001855OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1856 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
1857 SourceLocation StartLoc, SourceLocation LParenLoc,
1858 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
1859 SourceLocation EndLoc) {
1860 OMPClause *Res = nullptr;
1861 switch (Kind) {
1862 case OMPC_schedule:
1863 Res = ActOnOpenMPScheduleClause(
1864 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
1865 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
1866 break;
1867 case OMPC_if:
1868 case OMPC_num_threads:
1869 case OMPC_safelen:
1870 case OMPC_collapse:
1871 case OMPC_default:
1872 case OMPC_proc_bind:
1873 case OMPC_private:
1874 case OMPC_firstprivate:
1875 case OMPC_lastprivate:
1876 case OMPC_shared:
1877 case OMPC_reduction:
1878 case OMPC_linear:
1879 case OMPC_aligned:
1880 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001881 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001882 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001883 case OMPC_threadprivate:
1884 case OMPC_unknown:
1885 llvm_unreachable("Clause is not allowed.");
1886 }
1887 return Res;
1888}
1889
1890OMPClause *Sema::ActOnOpenMPScheduleClause(
1891 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
1892 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
1893 SourceLocation EndLoc) {
1894 if (Kind == OMPC_SCHEDULE_unknown) {
1895 std::string Values;
1896 std::string Sep(", ");
1897 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
1898 Values += "'";
1899 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
1900 Values += "'";
1901 switch (i) {
1902 case OMPC_SCHEDULE_unknown - 2:
1903 Values += " or ";
1904 break;
1905 case OMPC_SCHEDULE_unknown - 1:
1906 break;
1907 default:
1908 Values += Sep;
1909 break;
1910 }
1911 }
1912 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
1913 << Values << getOpenMPClauseName(OMPC_schedule);
1914 return nullptr;
1915 }
1916 Expr *ValExpr = ChunkSize;
1917 if (ChunkSize) {
1918 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
1919 !ChunkSize->isInstantiationDependent() &&
1920 !ChunkSize->containsUnexpandedParameterPack()) {
1921 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
1922 ExprResult Val =
1923 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
1924 if (Val.isInvalid())
1925 return nullptr;
1926
1927 ValExpr = Val.get();
1928
1929 // OpenMP [2.7.1, Restrictions]
1930 // chunk_size must be a loop invariant integer expression with a positive
1931 // value.
1932 llvm::APSInt Result;
1933 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
1934 Result.isSigned() && !Result.isStrictlyPositive()) {
1935 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
1936 << "schedule" << ChunkSize->getSourceRange();
1937 return nullptr;
1938 }
1939 }
1940 }
1941
1942 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
1943 EndLoc, Kind, ValExpr);
1944}
1945
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001946OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
1947 SourceLocation StartLoc,
1948 SourceLocation EndLoc) {
1949 OMPClause *Res = nullptr;
1950 switch (Kind) {
1951 case OMPC_ordered:
1952 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
1953 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001954 case OMPC_nowait:
1955 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
1956 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001957 case OMPC_if:
1958 case OMPC_num_threads:
1959 case OMPC_safelen:
1960 case OMPC_collapse:
1961 case OMPC_schedule:
1962 case OMPC_private:
1963 case OMPC_firstprivate:
1964 case OMPC_lastprivate:
1965 case OMPC_shared:
1966 case OMPC_reduction:
1967 case OMPC_linear:
1968 case OMPC_aligned:
1969 case OMPC_copyin:
1970 case OMPC_default:
1971 case OMPC_proc_bind:
1972 case OMPC_threadprivate:
1973 case OMPC_unknown:
1974 llvm_unreachable("Clause is not allowed.");
1975 }
1976 return Res;
1977}
1978
1979OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
1980 SourceLocation EndLoc) {
1981 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
1982}
1983
Alexey Bataev236070f2014-06-20 11:19:47 +00001984OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
1985 SourceLocation EndLoc) {
1986 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
1987}
1988
Alexey Bataevc5e02582014-06-16 07:08:35 +00001989OMPClause *Sema::ActOnOpenMPVarListClause(
1990 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
1991 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
1992 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
1993 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001994 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001995 switch (Kind) {
1996 case OMPC_private:
1997 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
1998 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001999 case OMPC_firstprivate:
2000 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2001 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002002 case OMPC_lastprivate:
2003 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2004 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002005 case OMPC_shared:
2006 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2007 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002008 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002009 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2010 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002011 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002012 case OMPC_linear:
2013 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2014 ColonLoc, EndLoc);
2015 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002016 case OMPC_aligned:
2017 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2018 ColonLoc, EndLoc);
2019 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002020 case OMPC_copyin:
2021 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2022 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002023 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002024 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002025 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002026 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002027 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002028 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002029 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002030 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002031 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002032 case OMPC_threadprivate:
2033 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002034 llvm_unreachable("Clause is not allowed.");
2035 }
2036 return Res;
2037}
2038
2039OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2040 SourceLocation StartLoc,
2041 SourceLocation LParenLoc,
2042 SourceLocation EndLoc) {
2043 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002044 for (auto &RefExpr : VarList) {
2045 assert(RefExpr && "NULL expr in OpenMP private clause.");
2046 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002047 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002048 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002049 continue;
2050 }
2051
Alexey Bataeved09d242014-05-28 05:53:51 +00002052 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002053 // OpenMP [2.1, C/C++]
2054 // A list item is a variable name.
2055 // OpenMP [2.9.3.3, Restrictions, p.1]
2056 // A variable that is part of another variable (as an array or
2057 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002058 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002059 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002060 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002061 continue;
2062 }
2063 Decl *D = DE->getDecl();
2064 VarDecl *VD = cast<VarDecl>(D);
2065
2066 QualType Type = VD->getType();
2067 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2068 // It will be analyzed later.
2069 Vars.push_back(DE);
2070 continue;
2071 }
2072
2073 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2074 // A variable that appears in a private clause must not have an incomplete
2075 // type or a reference type.
2076 if (RequireCompleteType(ELoc, Type,
2077 diag::err_omp_private_incomplete_type)) {
2078 continue;
2079 }
2080 if (Type->isReferenceType()) {
2081 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002082 << getOpenMPClauseName(OMPC_private) << Type;
2083 bool IsDecl =
2084 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2085 Diag(VD->getLocation(),
2086 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2087 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002088 continue;
2089 }
2090
2091 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2092 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002093 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002094 // class type.
2095 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002096 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2097 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002098 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002099 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2100 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2101 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002102 // FIXME This code must be replaced by actual constructing/destructing of
2103 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002104 if (RD) {
2105 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2106 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002107 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002108 if (!CD ||
2109 CheckConstructorAccess(ELoc, CD,
2110 InitializedEntity::InitializeTemporary(Type),
2111 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002112 CD->isDeleted()) {
2113 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002114 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002115 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2116 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002117 Diag(VD->getLocation(),
2118 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2119 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002120 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2121 continue;
2122 }
2123 MarkFunctionReferenced(ELoc, CD);
2124 DiagnoseUseOfDecl(CD, ELoc);
2125
2126 CXXDestructorDecl *DD = RD->getDestructor();
2127 if (DD) {
2128 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2129 DD->isDeleted()) {
2130 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002131 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002132 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2133 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002134 Diag(VD->getLocation(),
2135 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2136 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002137 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2138 continue;
2139 }
2140 MarkFunctionReferenced(ELoc, DD);
2141 DiagnoseUseOfDecl(DD, ELoc);
2142 }
2143 }
2144
Alexey Bataev758e55e2013-09-06 18:03:48 +00002145 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2146 // in a Construct]
2147 // Variables with the predetermined data-sharing attributes may not be
2148 // listed in data-sharing attributes clauses, except for the cases
2149 // listed below. For these exceptions only, listing a predetermined
2150 // variable in a data-sharing attribute clause is allowed and overrides
2151 // the variable's predetermined data-sharing attributes.
2152 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2153 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002154 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2155 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002156 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002157 continue;
2158 }
2159
2160 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002161 Vars.push_back(DE);
2162 }
2163
Alexey Bataeved09d242014-05-28 05:53:51 +00002164 if (Vars.empty())
2165 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002166
2167 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2168}
2169
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002170OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2171 SourceLocation StartLoc,
2172 SourceLocation LParenLoc,
2173 SourceLocation EndLoc) {
2174 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002175 for (auto &RefExpr : VarList) {
2176 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2177 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002178 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002179 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002180 continue;
2181 }
2182
Alexey Bataeved09d242014-05-28 05:53:51 +00002183 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002184 // OpenMP [2.1, C/C++]
2185 // A list item is a variable name.
2186 // OpenMP [2.9.3.3, Restrictions, p.1]
2187 // A variable that is part of another variable (as an array or
2188 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002189 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002190 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002191 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002192 continue;
2193 }
2194 Decl *D = DE->getDecl();
2195 VarDecl *VD = cast<VarDecl>(D);
2196
2197 QualType Type = VD->getType();
2198 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2199 // It will be analyzed later.
2200 Vars.push_back(DE);
2201 continue;
2202 }
2203
2204 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2205 // A variable that appears in a private clause must not have an incomplete
2206 // type or a reference type.
2207 if (RequireCompleteType(ELoc, Type,
2208 diag::err_omp_firstprivate_incomplete_type)) {
2209 continue;
2210 }
2211 if (Type->isReferenceType()) {
2212 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002213 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2214 bool IsDecl =
2215 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2216 Diag(VD->getLocation(),
2217 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2218 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002219 continue;
2220 }
2221
2222 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2223 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002224 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002225 // class type.
2226 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002227 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2228 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2229 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002230 // FIXME This code must be replaced by actual constructing/destructing of
2231 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002232 if (RD) {
2233 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2234 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002235 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002236 if (!CD ||
2237 CheckConstructorAccess(ELoc, CD,
2238 InitializedEntity::InitializeTemporary(Type),
2239 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002240 CD->isDeleted()) {
2241 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002242 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002243 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2244 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002245 Diag(VD->getLocation(),
2246 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2247 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002248 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2249 continue;
2250 }
2251 MarkFunctionReferenced(ELoc, CD);
2252 DiagnoseUseOfDecl(CD, ELoc);
2253
2254 CXXDestructorDecl *DD = RD->getDestructor();
2255 if (DD) {
2256 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2257 DD->isDeleted()) {
2258 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002259 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002260 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2261 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002262 Diag(VD->getLocation(),
2263 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2264 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002265 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2266 continue;
2267 }
2268 MarkFunctionReferenced(ELoc, DD);
2269 DiagnoseUseOfDecl(DD, ELoc);
2270 }
2271 }
2272
2273 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2274 // variable and it was checked already.
2275 if (StartLoc.isValid() && EndLoc.isValid()) {
2276 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2277 Type = Type.getNonReferenceType().getCanonicalType();
2278 bool IsConstant = Type.isConstant(Context);
2279 Type = Context.getBaseElementType(Type);
2280 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2281 // A list item that specifies a given variable may not appear in more
2282 // than one clause on the same directive, except that a variable may be
2283 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002284 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002285 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002286 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002287 << getOpenMPClauseName(DVar.CKind)
2288 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002289 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002290 continue;
2291 }
2292
2293 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2294 // in a Construct]
2295 // Variables with the predetermined data-sharing attributes may not be
2296 // listed in data-sharing attributes clauses, except for the cases
2297 // listed below. For these exceptions only, listing a predetermined
2298 // variable in a data-sharing attribute clause is allowed and overrides
2299 // the variable's predetermined data-sharing attributes.
2300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2301 // in a Construct, C/C++, p.2]
2302 // Variables with const-qualified type having no mutable member may be
2303 // listed in a firstprivate clause, even if they are static data members.
2304 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2305 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2306 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002307 << getOpenMPClauseName(DVar.CKind)
2308 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002309 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002310 continue;
2311 }
2312
Alexey Bataevf29276e2014-06-18 04:14:57 +00002313 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002314 // OpenMP [2.9.3.4, Restrictions, p.2]
2315 // A list item that is private within a parallel region must not appear
2316 // in a firstprivate clause on a worksharing construct if any of the
2317 // worksharing regions arising from the worksharing construct ever bind
2318 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002319 if (isOpenMPWorksharingDirective(CurrDir) &&
2320 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002321 DVar = DSAStack->getImplicitDSA(VD);
2322 if (DVar.CKind != OMPC_shared) {
2323 Diag(ELoc, diag::err_omp_required_access)
2324 << getOpenMPClauseName(OMPC_firstprivate)
2325 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002326 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002327 continue;
2328 }
2329 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002330 // OpenMP [2.9.3.4, Restrictions, p.3]
2331 // A list item that appears in a reduction clause of a parallel construct
2332 // must not appear in a firstprivate clause on a worksharing or task
2333 // construct if any of the worksharing or task regions arising from the
2334 // worksharing or task construct ever bind to any of the parallel regions
2335 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002336 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002337 // OpenMP [2.9.3.4, Restrictions, p.4]
2338 // A list item that appears in a reduction clause in worksharing
2339 // construct must not appear in a firstprivate clause in a task construct
2340 // encountered during execution of any of the worksharing regions arising
2341 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002342 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002343 }
2344
2345 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2346 Vars.push_back(DE);
2347 }
2348
Alexey Bataeved09d242014-05-28 05:53:51 +00002349 if (Vars.empty())
2350 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002351
2352 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2353 Vars);
2354}
2355
Alexander Musman1bb328c2014-06-04 13:06:39 +00002356OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2357 SourceLocation StartLoc,
2358 SourceLocation LParenLoc,
2359 SourceLocation EndLoc) {
2360 SmallVector<Expr *, 8> Vars;
2361 for (auto &RefExpr : VarList) {
2362 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2363 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2364 // It will be analyzed later.
2365 Vars.push_back(RefExpr);
2366 continue;
2367 }
2368
2369 SourceLocation ELoc = RefExpr->getExprLoc();
2370 // OpenMP [2.1, C/C++]
2371 // A list item is a variable name.
2372 // OpenMP [2.14.3.5, Restrictions, p.1]
2373 // A variable that is part of another variable (as an array or structure
2374 // element) cannot appear in a lastprivate clause.
2375 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2376 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2377 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2378 continue;
2379 }
2380 Decl *D = DE->getDecl();
2381 VarDecl *VD = cast<VarDecl>(D);
2382
2383 QualType Type = VD->getType();
2384 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2385 // It will be analyzed later.
2386 Vars.push_back(DE);
2387 continue;
2388 }
2389
2390 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2391 // A variable that appears in a lastprivate clause must not have an
2392 // incomplete type or a reference type.
2393 if (RequireCompleteType(ELoc, Type,
2394 diag::err_omp_lastprivate_incomplete_type)) {
2395 continue;
2396 }
2397 if (Type->isReferenceType()) {
2398 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2399 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2400 bool IsDecl =
2401 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2402 Diag(VD->getLocation(),
2403 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2404 << VD;
2405 continue;
2406 }
2407
2408 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2409 // in a Construct]
2410 // Variables with the predetermined data-sharing attributes may not be
2411 // listed in data-sharing attributes clauses, except for the cases
2412 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002413 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2414 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2415 DVar.CKind != OMPC_firstprivate &&
2416 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2417 Diag(ELoc, diag::err_omp_wrong_dsa)
2418 << getOpenMPClauseName(DVar.CKind)
2419 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002420 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002421 continue;
2422 }
2423
Alexey Bataevf29276e2014-06-18 04:14:57 +00002424 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2425 // OpenMP [2.14.3.5, Restrictions, p.2]
2426 // A list item that is private within a parallel region, or that appears in
2427 // the reduction clause of a parallel construct, must not appear in a
2428 // lastprivate clause on a worksharing construct if any of the corresponding
2429 // worksharing regions ever binds to any of the corresponding parallel
2430 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002431 if (isOpenMPWorksharingDirective(CurrDir) &&
2432 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002433 DVar = DSAStack->getImplicitDSA(VD);
2434 if (DVar.CKind != OMPC_shared) {
2435 Diag(ELoc, diag::err_omp_required_access)
2436 << getOpenMPClauseName(OMPC_lastprivate)
2437 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002438 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002439 continue;
2440 }
2441 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002442 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002443 // A variable of class type (or array thereof) that appears in a
2444 // lastprivate clause requires an accessible, unambiguous default
2445 // constructor for the class type, unless the list item is also specified
2446 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002447 // A variable of class type (or array thereof) that appears in a
2448 // lastprivate clause requires an accessible, unambiguous copy assignment
2449 // operator for the class type.
2450 while (Type.getNonReferenceType()->isArrayType())
2451 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2452 ->getElementType();
2453 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2454 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2455 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002456 // FIXME This code must be replaced by actual copying and destructing of the
2457 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002458 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002459 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2460 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002461 if (MD) {
2462 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2463 MD->isDeleted()) {
2464 Diag(ELoc, diag::err_omp_required_method)
2465 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2466 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2467 VarDecl::DeclarationOnly;
2468 Diag(VD->getLocation(),
2469 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2470 << VD;
2471 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2472 continue;
2473 }
2474 MarkFunctionReferenced(ELoc, MD);
2475 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002476 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002477
2478 CXXDestructorDecl *DD = RD->getDestructor();
2479 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002480 PartialDiagnostic PD =
2481 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002482 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2483 DD->isDeleted()) {
2484 Diag(ELoc, diag::err_omp_required_method)
2485 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2486 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2487 VarDecl::DeclarationOnly;
2488 Diag(VD->getLocation(),
2489 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2490 << VD;
2491 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2492 continue;
2493 }
2494 MarkFunctionReferenced(ELoc, DD);
2495 DiagnoseUseOfDecl(DD, ELoc);
2496 }
2497 }
2498
Alexey Bataevf29276e2014-06-18 04:14:57 +00002499 if (DVar.CKind != OMPC_firstprivate)
2500 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002501 Vars.push_back(DE);
2502 }
2503
2504 if (Vars.empty())
2505 return nullptr;
2506
2507 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2508 Vars);
2509}
2510
Alexey Bataev758e55e2013-09-06 18:03:48 +00002511OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2512 SourceLocation StartLoc,
2513 SourceLocation LParenLoc,
2514 SourceLocation EndLoc) {
2515 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002516 for (auto &RefExpr : VarList) {
2517 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2518 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002519 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002520 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002521 continue;
2522 }
2523
Alexey Bataeved09d242014-05-28 05:53:51 +00002524 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002525 // OpenMP [2.1, C/C++]
2526 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002527 // OpenMP [2.14.3.2, Restrictions, p.1]
2528 // A variable that is part of another variable (as an array or structure
2529 // element) cannot appear in a shared unless it is a static data member
2530 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002531 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002532 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002533 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002534 continue;
2535 }
2536 Decl *D = DE->getDecl();
2537 VarDecl *VD = cast<VarDecl>(D);
2538
2539 QualType Type = VD->getType();
2540 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2541 // It will be analyzed later.
2542 Vars.push_back(DE);
2543 continue;
2544 }
2545
2546 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2547 // in a Construct]
2548 // Variables with the predetermined data-sharing attributes may not be
2549 // listed in data-sharing attributes clauses, except for the cases
2550 // listed below. For these exceptions only, listing a predetermined
2551 // variable in a data-sharing attribute clause is allowed and overrides
2552 // the variable's predetermined data-sharing attributes.
2553 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002554 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2555 DVar.RefExpr) {
2556 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2557 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002558 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002559 continue;
2560 }
2561
2562 DSAStack->addDSA(VD, DE, OMPC_shared);
2563 Vars.push_back(DE);
2564 }
2565
Alexey Bataeved09d242014-05-28 05:53:51 +00002566 if (Vars.empty())
2567 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002568
2569 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2570}
2571
Alexey Bataevc5e02582014-06-16 07:08:35 +00002572namespace {
2573class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2574 DSAStackTy *Stack;
2575
2576public:
2577 bool VisitDeclRefExpr(DeclRefExpr *E) {
2578 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2579 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2580 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2581 return false;
2582 if (DVar.CKind != OMPC_unknown)
2583 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002584 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002585 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002586 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002587 return true;
2588 return false;
2589 }
2590 return false;
2591 }
2592 bool VisitStmt(Stmt *S) {
2593 for (auto Child : S->children()) {
2594 if (Child && Visit(Child))
2595 return true;
2596 }
2597 return false;
2598 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002599 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002600};
Alexey Bataev23b69422014-06-18 07:08:49 +00002601} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002602
2603OMPClause *Sema::ActOnOpenMPReductionClause(
2604 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2605 SourceLocation ColonLoc, SourceLocation EndLoc,
2606 CXXScopeSpec &ReductionIdScopeSpec,
2607 const DeclarationNameInfo &ReductionId) {
2608 // TODO: Allow scope specification search when 'declare reduction' is
2609 // supported.
2610 assert(ReductionIdScopeSpec.isEmpty() &&
2611 "No support for scoped reduction identifiers yet.");
2612
2613 auto DN = ReductionId.getName();
2614 auto OOK = DN.getCXXOverloadedOperator();
2615 BinaryOperatorKind BOK = BO_Comma;
2616
2617 // OpenMP [2.14.3.6, reduction clause]
2618 // C
2619 // reduction-identifier is either an identifier or one of the following
2620 // operators: +, -, *, &, |, ^, && and ||
2621 // C++
2622 // reduction-identifier is either an id-expression or one of the following
2623 // operators: +, -, *, &, |, ^, && and ||
2624 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2625 switch (OOK) {
2626 case OO_Plus:
2627 case OO_Minus:
2628 BOK = BO_AddAssign;
2629 break;
2630 case OO_Star:
2631 BOK = BO_MulAssign;
2632 break;
2633 case OO_Amp:
2634 BOK = BO_AndAssign;
2635 break;
2636 case OO_Pipe:
2637 BOK = BO_OrAssign;
2638 break;
2639 case OO_Caret:
2640 BOK = BO_XorAssign;
2641 break;
2642 case OO_AmpAmp:
2643 BOK = BO_LAnd;
2644 break;
2645 case OO_PipePipe:
2646 BOK = BO_LOr;
2647 break;
2648 default:
2649 if (auto II = DN.getAsIdentifierInfo()) {
2650 if (II->isStr("max"))
2651 BOK = BO_GT;
2652 else if (II->isStr("min"))
2653 BOK = BO_LT;
2654 }
2655 break;
2656 }
2657 SourceRange ReductionIdRange;
2658 if (ReductionIdScopeSpec.isValid()) {
2659 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2660 }
2661 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2662 if (BOK == BO_Comma) {
2663 // Not allowed reduction identifier is found.
2664 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2665 << ReductionIdRange;
2666 return nullptr;
2667 }
2668
2669 SmallVector<Expr *, 8> Vars;
2670 for (auto RefExpr : VarList) {
2671 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2672 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2673 // It will be analyzed later.
2674 Vars.push_back(RefExpr);
2675 continue;
2676 }
2677
2678 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2679 RefExpr->isInstantiationDependent() ||
2680 RefExpr->containsUnexpandedParameterPack()) {
2681 // It will be analyzed later.
2682 Vars.push_back(RefExpr);
2683 continue;
2684 }
2685
2686 auto ELoc = RefExpr->getExprLoc();
2687 auto ERange = RefExpr->getSourceRange();
2688 // OpenMP [2.1, C/C++]
2689 // A list item is a variable or array section, subject to the restrictions
2690 // specified in Section 2.4 on page 42 and in each of the sections
2691 // describing clauses and directives for which a list appears.
2692 // OpenMP [2.14.3.3, Restrictions, p.1]
2693 // A variable that is part of another variable (as an array or
2694 // structure element) cannot appear in a private clause.
2695 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2696 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2697 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2698 continue;
2699 }
2700 auto D = DE->getDecl();
2701 auto VD = cast<VarDecl>(D);
2702 auto Type = VD->getType();
2703 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2704 // A variable that appears in a private clause must not have an incomplete
2705 // type or a reference type.
2706 if (RequireCompleteType(ELoc, Type,
2707 diag::err_omp_reduction_incomplete_type))
2708 continue;
2709 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2710 // Arrays may not appear in a reduction clause.
2711 if (Type.getNonReferenceType()->isArrayType()) {
2712 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2713 bool IsDecl =
2714 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2715 Diag(VD->getLocation(),
2716 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2717 << VD;
2718 continue;
2719 }
2720 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2721 // A list item that appears in a reduction clause must not be
2722 // const-qualified.
2723 if (Type.getNonReferenceType().isConstant(Context)) {
2724 Diag(ELoc, diag::err_omp_const_variable)
2725 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2726 bool IsDecl =
2727 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2728 Diag(VD->getLocation(),
2729 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2730 << VD;
2731 continue;
2732 }
2733 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2734 // If a list-item is a reference type then it must bind to the same object
2735 // for all threads of the team.
2736 VarDecl *VDDef = VD->getDefinition();
2737 if (Type->isReferenceType() && VDDef) {
2738 DSARefChecker Check(DSAStack);
2739 if (Check.Visit(VDDef->getInit())) {
2740 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2741 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2742 continue;
2743 }
2744 }
2745 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2746 // The type of a list item that appears in a reduction clause must be valid
2747 // for the reduction-identifier. For a max or min reduction in C, the type
2748 // of the list item must be an allowed arithmetic data type: char, int,
2749 // float, double, or _Bool, possibly modified with long, short, signed, or
2750 // unsigned. For a max or min reduction in C++, the type of the list item
2751 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2752 // double, or bool, possibly modified with long, short, signed, or unsigned.
2753 if ((BOK == BO_GT || BOK == BO_LT) &&
2754 !(Type->isScalarType() ||
2755 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2756 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2757 << getLangOpts().CPlusPlus;
2758 bool IsDecl =
2759 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2760 Diag(VD->getLocation(),
2761 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2762 << VD;
2763 continue;
2764 }
2765 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2766 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2767 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2768 bool IsDecl =
2769 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2770 Diag(VD->getLocation(),
2771 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2772 << VD;
2773 continue;
2774 }
2775 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2776 getDiagnostics().setSuppressAllDiagnostics(true);
2777 ExprResult ReductionOp =
2778 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2779 RefExpr, RefExpr);
2780 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2781 if (ReductionOp.isInvalid()) {
2782 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002783 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002784 bool IsDecl =
2785 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2786 Diag(VD->getLocation(),
2787 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2788 << VD;
2789 continue;
2790 }
2791
2792 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2793 // in a Construct]
2794 // Variables with the predetermined data-sharing attributes may not be
2795 // listed in data-sharing attributes clauses, except for the cases
2796 // listed below. For these exceptions only, listing a predetermined
2797 // variable in a data-sharing attribute clause is allowed and overrides
2798 // the variable's predetermined data-sharing attributes.
2799 // OpenMP [2.14.3.6, Restrictions, p.3]
2800 // Any number of reduction clauses can be specified on the directive,
2801 // but a list item can appear only once in the reduction clauses for that
2802 // directive.
2803 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2804 if (DVar.CKind == OMPC_reduction) {
2805 Diag(ELoc, diag::err_omp_once_referenced)
2806 << getOpenMPClauseName(OMPC_reduction);
2807 if (DVar.RefExpr) {
2808 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2809 }
2810 } else if (DVar.CKind != OMPC_unknown) {
2811 Diag(ELoc, diag::err_omp_wrong_dsa)
2812 << getOpenMPClauseName(DVar.CKind)
2813 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002814 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002815 continue;
2816 }
2817
2818 // OpenMP [2.14.3.6, Restrictions, p.1]
2819 // A list item that appears in a reduction clause of a worksharing
2820 // construct must be shared in the parallel regions to which any of the
2821 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002822 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00002823 if (isOpenMPWorksharingDirective(CurrDir) &&
2824 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002825 DVar = DSAStack->getImplicitDSA(VD);
2826 if (DVar.CKind != OMPC_shared) {
2827 Diag(ELoc, diag::err_omp_required_access)
2828 << getOpenMPClauseName(OMPC_reduction)
2829 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002830 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002831 continue;
2832 }
2833 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00002834
2835 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2836 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2837 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002838 // FIXME This code must be replaced by actual constructing/destructing of
2839 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00002840 if (RD) {
2841 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2842 PartialDiagnostic PD =
2843 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00002844 if (!CD ||
2845 CheckConstructorAccess(ELoc, CD,
2846 InitializedEntity::InitializeTemporary(Type),
2847 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00002848 CD->isDeleted()) {
2849 Diag(ELoc, diag::err_omp_required_method)
2850 << getOpenMPClauseName(OMPC_reduction) << 0;
2851 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2852 VarDecl::DeclarationOnly;
2853 Diag(VD->getLocation(),
2854 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2855 << VD;
2856 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2857 continue;
2858 }
2859 MarkFunctionReferenced(ELoc, CD);
2860 DiagnoseUseOfDecl(CD, ELoc);
2861
2862 CXXDestructorDecl *DD = RD->getDestructor();
2863 if (DD) {
2864 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2865 DD->isDeleted()) {
2866 Diag(ELoc, diag::err_omp_required_method)
2867 << getOpenMPClauseName(OMPC_reduction) << 4;
2868 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2869 VarDecl::DeclarationOnly;
2870 Diag(VD->getLocation(),
2871 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2872 << VD;
2873 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2874 continue;
2875 }
2876 MarkFunctionReferenced(ELoc, DD);
2877 DiagnoseUseOfDecl(DD, ELoc);
2878 }
2879 }
2880
2881 DSAStack->addDSA(VD, DE, OMPC_reduction);
2882 Vars.push_back(DE);
2883 }
2884
2885 if (Vars.empty())
2886 return nullptr;
2887
2888 return OMPReductionClause::Create(
2889 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
2890 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
2891}
2892
Alexander Musman8dba6642014-04-22 13:09:42 +00002893OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2894 SourceLocation StartLoc,
2895 SourceLocation LParenLoc,
2896 SourceLocation ColonLoc,
2897 SourceLocation EndLoc) {
2898 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002899 for (auto &RefExpr : VarList) {
2900 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2901 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002902 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002903 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002904 continue;
2905 }
2906
2907 // OpenMP [2.14.3.7, linear clause]
2908 // A list item that appears in a linear clause is subject to the private
2909 // clause semantics described in Section 2.14.3.3 on page 159 except as
2910 // noted. In addition, the value of the new list item on each iteration
2911 // of the associated loop(s) corresponds to the value of the original
2912 // list item before entering the construct plus the logical number of
2913 // the iteration times linear-step.
2914
Alexey Bataeved09d242014-05-28 05:53:51 +00002915 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00002916 // OpenMP [2.1, C/C++]
2917 // A list item is a variable name.
2918 // OpenMP [2.14.3.3, Restrictions, p.1]
2919 // A variable that is part of another variable (as an array or
2920 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002921 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002922 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002923 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00002924 continue;
2925 }
2926
2927 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2928
2929 // OpenMP [2.14.3.7, linear clause]
2930 // A list-item cannot appear in more than one linear clause.
2931 // A list-item that appears in a linear clause cannot appear in any
2932 // other data-sharing attribute clause.
2933 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2934 if (DVar.RefExpr) {
2935 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2936 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002937 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00002938 continue;
2939 }
2940
2941 QualType QType = VD->getType();
2942 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2943 // It will be analyzed later.
2944 Vars.push_back(DE);
2945 continue;
2946 }
2947
2948 // A variable must not have an incomplete type or a reference type.
2949 if (RequireCompleteType(ELoc, QType,
2950 diag::err_omp_linear_incomplete_type)) {
2951 continue;
2952 }
2953 if (QType->isReferenceType()) {
2954 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2955 << getOpenMPClauseName(OMPC_linear) << QType;
2956 bool IsDecl =
2957 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2958 Diag(VD->getLocation(),
2959 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2960 << VD;
2961 continue;
2962 }
2963
2964 // A list item must not be const-qualified.
2965 if (QType.isConstant(Context)) {
2966 Diag(ELoc, diag::err_omp_const_variable)
2967 << getOpenMPClauseName(OMPC_linear);
2968 bool IsDecl =
2969 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2970 Diag(VD->getLocation(),
2971 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2972 << VD;
2973 continue;
2974 }
2975
2976 // A list item must be of integral or pointer type.
2977 QType = QType.getUnqualifiedType().getCanonicalType();
2978 const Type *Ty = QType.getTypePtrOrNull();
2979 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
2980 !Ty->isPointerType())) {
2981 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
2982 bool IsDecl =
2983 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2984 Diag(VD->getLocation(),
2985 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2986 << VD;
2987 continue;
2988 }
2989
2990 DSAStack->addDSA(VD, DE, OMPC_linear);
2991 Vars.push_back(DE);
2992 }
2993
2994 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002995 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00002996
2997 Expr *StepExpr = Step;
2998 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
2999 !Step->isInstantiationDependent() &&
3000 !Step->containsUnexpandedParameterPack()) {
3001 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003002 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003003 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003004 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003005 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003006
3007 // Warn about zero linear step (it would be probably better specified as
3008 // making corresponding variables 'const').
3009 llvm::APSInt Result;
3010 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3011 !Result.isNegative() && !Result.isStrictlyPositive())
3012 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3013 << (Vars.size() > 1);
3014 }
3015
3016 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3017 Vars, StepExpr);
3018}
3019
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003020OMPClause *Sema::ActOnOpenMPAlignedClause(
3021 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3022 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3023
3024 SmallVector<Expr *, 8> Vars;
3025 for (auto &RefExpr : VarList) {
3026 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3027 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3028 // It will be analyzed later.
3029 Vars.push_back(RefExpr);
3030 continue;
3031 }
3032
3033 SourceLocation ELoc = RefExpr->getExprLoc();
3034 // OpenMP [2.1, C/C++]
3035 // A list item is a variable name.
3036 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3037 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3038 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3039 continue;
3040 }
3041
3042 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3043
3044 // OpenMP [2.8.1, simd construct, Restrictions]
3045 // The type of list items appearing in the aligned clause must be
3046 // array, pointer, reference to array, or reference to pointer.
3047 QualType QType = DE->getType()
3048 .getNonReferenceType()
3049 .getUnqualifiedType()
3050 .getCanonicalType();
3051 const Type *Ty = QType.getTypePtrOrNull();
3052 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3053 !Ty->isPointerType())) {
3054 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3055 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3056 bool IsDecl =
3057 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3058 Diag(VD->getLocation(),
3059 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3060 << VD;
3061 continue;
3062 }
3063
3064 // OpenMP [2.8.1, simd construct, Restrictions]
3065 // A list-item cannot appear in more than one aligned clause.
3066 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3067 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3068 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3069 << getOpenMPClauseName(OMPC_aligned);
3070 continue;
3071 }
3072
3073 Vars.push_back(DE);
3074 }
3075
3076 // OpenMP [2.8.1, simd construct, Description]
3077 // The parameter of the aligned clause, alignment, must be a constant
3078 // positive integer expression.
3079 // If no optional parameter is specified, implementation-defined default
3080 // alignments for SIMD instructions on the target platforms are assumed.
3081 if (Alignment != nullptr) {
3082 ExprResult AlignResult =
3083 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3084 if (AlignResult.isInvalid())
3085 return nullptr;
3086 Alignment = AlignResult.get();
3087 }
3088 if (Vars.empty())
3089 return nullptr;
3090
3091 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3092 EndLoc, Vars, Alignment);
3093}
3094
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003095OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3096 SourceLocation StartLoc,
3097 SourceLocation LParenLoc,
3098 SourceLocation EndLoc) {
3099 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003100 for (auto &RefExpr : VarList) {
3101 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3102 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003103 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003104 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003105 continue;
3106 }
3107
Alexey Bataeved09d242014-05-28 05:53:51 +00003108 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003109 // OpenMP [2.1, C/C++]
3110 // A list item is a variable name.
3111 // OpenMP [2.14.4.1, Restrictions, p.1]
3112 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003113 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003114 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003115 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003116 continue;
3117 }
3118
3119 Decl *D = DE->getDecl();
3120 VarDecl *VD = cast<VarDecl>(D);
3121
3122 QualType Type = VD->getType();
3123 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3124 // It will be analyzed later.
3125 Vars.push_back(DE);
3126 continue;
3127 }
3128
3129 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3130 // A list item that appears in a copyin clause must be threadprivate.
3131 if (!DSAStack->isThreadPrivate(VD)) {
3132 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003133 << getOpenMPClauseName(OMPC_copyin)
3134 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003135 continue;
3136 }
3137
3138 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3139 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003140 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003141 // operator for the class type.
3142 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003143 CXXRecordDecl *RD =
3144 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003145 // FIXME This code must be replaced by actual assignment of the
3146 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003147 if (RD) {
3148 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3149 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003150 if (MD) {
3151 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3152 MD->isDeleted()) {
3153 Diag(ELoc, diag::err_omp_required_method)
3154 << getOpenMPClauseName(OMPC_copyin) << 2;
3155 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3156 VarDecl::DeclarationOnly;
3157 Diag(VD->getLocation(),
3158 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3159 << VD;
3160 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3161 continue;
3162 }
3163 MarkFunctionReferenced(ELoc, MD);
3164 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003165 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003166 }
3167
3168 DSAStack->addDSA(VD, DE, OMPC_copyin);
3169 Vars.push_back(DE);
3170 }
3171
Alexey Bataeved09d242014-05-28 05:53:51 +00003172 if (Vars.empty())
3173 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003174
3175 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3176}
3177
Alexey Bataev758e55e2013-09-06 18:03:48 +00003178#undef DSAStack