blob: db471b88adf32a90007bd8ad6e3927adc284fc0a [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 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000913 case OMPD_sections: {
914 Sema::CapturedParamNameType Params[] = {
915 std::make_pair(StringRef(), QualType()) // __context with shared vars
916 };
917 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
918 break;
919 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000920 case OMPD_threadprivate:
921 case OMPD_task:
922 llvm_unreachable("OpenMP Directive is not allowed");
923 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000924 llvm_unreachable("Unknown OpenMP directive");
925 }
926}
927
Alexey Bataev549210e2014-06-24 04:39:47 +0000928bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
929 OpenMPDirectiveKind CurrentRegion,
930 SourceLocation StartLoc) {
931 if (Stack->getCurScope()) {
932 auto ParentRegion = Stack->getParentDirective();
933 bool NestingProhibited = false;
934 bool CloseNesting = true;
935 bool ShouldBeInParallelRegion = false;
936 if (isOpenMPSimdDirective(ParentRegion)) {
937 // OpenMP [2.16, Nesting of Regions]
938 // OpenMP constructs may not be nested inside a simd region.
939 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
940 return true;
941 }
942 if (isOpenMPWorksharingDirective(CurrentRegion) &&
943 !isOpenMPParallelDirective(CurrentRegion) &&
944 !isOpenMPSimdDirective(CurrentRegion)) {
945 // OpenMP [2.16, Nesting of Regions]
946 // A worksharing region may not be closely nested inside a worksharing,
947 // explicit task, critical, ordered, atomic, or master region.
948 // TODO
949 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) &&
950 !isOpenMPSimdDirective(ParentRegion);
951 ShouldBeInParallelRegion = true;
952 }
953 if (NestingProhibited) {
954 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
955 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << true
956 << getOpenMPDirectiveName(CurrentRegion) << ShouldBeInParallelRegion;
957 return true;
958 }
959 }
960 return false;
961}
962
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000963StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
964 ArrayRef<OMPClause *> Clauses,
965 Stmt *AStmt,
966 SourceLocation StartLoc,
967 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000968 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
969
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000970 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +0000971 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
972 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000973
974 // Check default data sharing attributes for referenced variables.
975 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
976 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
977 if (DSAChecker.isErrorFound())
978 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000979 // Generate list of implicitly defined firstprivate variables.
980 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
981 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
982
983 bool ErrorFound = false;
984 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000985 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
986 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
987 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000988 ClausesWithImplicit.push_back(Implicit);
989 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +0000990 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000991 } else
992 ErrorFound = true;
993 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000994
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000995 switch (Kind) {
996 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +0000997 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
998 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000999 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001000 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +00001001 Res =
1002 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001003 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001004 case OMPD_for:
1005 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1006 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001007 case OMPD_sections:
1008 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1009 EndLoc);
1010 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001011 case OMPD_threadprivate:
1012 case OMPD_task:
1013 llvm_unreachable("OpenMP Directive is not allowed");
1014 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001015 llvm_unreachable("Unknown OpenMP directive");
1016 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001017
Alexey Bataeved09d242014-05-28 05:53:51 +00001018 if (ErrorFound)
1019 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001020 return Res;
1021}
1022
1023StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1024 Stmt *AStmt,
1025 SourceLocation StartLoc,
1026 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001027 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1028 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1029 // 1.2.2 OpenMP Language Terminology
1030 // Structured block - An executable statement with a single entry at the
1031 // top and a single exit at the bottom.
1032 // The point of exit cannot be a branch out of the structured block.
1033 // longjmp() and throw() must not violate the entry/exit criteria.
1034 CS->getCapturedDecl()->setNothrow();
1035
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001036 getCurFunction()->setHasBranchProtectedScope();
1037
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001038 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1039 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001040}
1041
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001042namespace {
1043/// \brief Helper class for checking canonical form of the OpenMP loops and
1044/// extracting iteration space of each loop in the loop nest, that will be used
1045/// for IR generation.
1046class OpenMPIterationSpaceChecker {
1047 /// \brief Reference to Sema.
1048 Sema &SemaRef;
1049 /// \brief A location for diagnostics (when there is no some better location).
1050 SourceLocation DefaultLoc;
1051 /// \brief A location for diagnostics (when increment is not compatible).
1052 SourceLocation ConditionLoc;
1053 /// \brief A source location for referring to condition later.
1054 SourceRange ConditionSrcRange;
1055 /// \brief Loop variable.
1056 VarDecl *Var;
1057 /// \brief Lower bound (initializer for the var).
1058 Expr *LB;
1059 /// \brief Upper bound.
1060 Expr *UB;
1061 /// \brief Loop step (increment).
1062 Expr *Step;
1063 /// \brief This flag is true when condition is one of:
1064 /// Var < UB
1065 /// Var <= UB
1066 /// UB > Var
1067 /// UB >= Var
1068 bool TestIsLessOp;
1069 /// \brief This flag is true when condition is strict ( < or > ).
1070 bool TestIsStrictOp;
1071 /// \brief This flag is true when step is subtracted on each iteration.
1072 bool SubtractStep;
1073
1074public:
1075 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1076 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1077 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1078 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1079 SubtractStep(false) {}
1080 /// \brief Check init-expr for canonical loop form and save loop counter
1081 /// variable - #Var and its initialization value - #LB.
1082 bool CheckInit(Stmt *S);
1083 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1084 /// for less/greater and for strict/non-strict comparison.
1085 bool CheckCond(Expr *S);
1086 /// \brief Check incr-expr for canonical loop form and return true if it
1087 /// does not conform, otherwise save loop step (#Step).
1088 bool CheckInc(Expr *S);
1089 /// \brief Return the loop counter variable.
1090 VarDecl *GetLoopVar() const { return Var; }
1091 /// \brief Return true if any expression is dependent.
1092 bool Dependent() const;
1093
1094private:
1095 /// \brief Check the right-hand side of an assignment in the increment
1096 /// expression.
1097 bool CheckIncRHS(Expr *RHS);
1098 /// \brief Helper to set loop counter variable and its initializer.
1099 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1100 /// \brief Helper to set upper bound.
1101 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1102 const SourceLocation &SL);
1103 /// \brief Helper to set loop increment.
1104 bool SetStep(Expr *NewStep, bool Subtract);
1105};
1106
1107bool OpenMPIterationSpaceChecker::Dependent() const {
1108 if (!Var) {
1109 assert(!LB && !UB && !Step);
1110 return false;
1111 }
1112 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1113 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1114}
1115
1116bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1117 // State consistency checking to ensure correct usage.
1118 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1119 !TestIsLessOp && !TestIsStrictOp);
1120 if (!NewVar || !NewLB)
1121 return true;
1122 Var = NewVar;
1123 LB = NewLB;
1124 return false;
1125}
1126
1127bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1128 const SourceRange &SR,
1129 const SourceLocation &SL) {
1130 // State consistency checking to ensure correct usage.
1131 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1132 !TestIsLessOp && !TestIsStrictOp);
1133 if (!NewUB)
1134 return true;
1135 UB = NewUB;
1136 TestIsLessOp = LessOp;
1137 TestIsStrictOp = StrictOp;
1138 ConditionSrcRange = SR;
1139 ConditionLoc = SL;
1140 return false;
1141}
1142
1143bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1144 // State consistency checking to ensure correct usage.
1145 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1146 if (!NewStep)
1147 return true;
1148 if (!NewStep->isValueDependent()) {
1149 // Check that the step is integer expression.
1150 SourceLocation StepLoc = NewStep->getLocStart();
1151 ExprResult Val =
1152 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1153 if (Val.isInvalid())
1154 return true;
1155 NewStep = Val.get();
1156
1157 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1158 // If test-expr is of form var relational-op b and relational-op is < or
1159 // <= then incr-expr must cause var to increase on each iteration of the
1160 // loop. If test-expr is of form var relational-op b and relational-op is
1161 // > or >= then incr-expr must cause var to decrease on each iteration of
1162 // the loop.
1163 // If test-expr is of form b relational-op var and relational-op is < or
1164 // <= then incr-expr must cause var to decrease on each iteration of the
1165 // loop. If test-expr is of form b relational-op var and relational-op is
1166 // > or >= then incr-expr must cause var to increase on each iteration of
1167 // the loop.
1168 llvm::APSInt Result;
1169 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1170 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1171 bool IsConstNeg =
1172 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1173 bool IsConstZero = IsConstant && !Result.getBoolValue();
1174 if (UB && (IsConstZero ||
1175 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1176 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1177 SemaRef.Diag(NewStep->getExprLoc(),
1178 diag::err_omp_loop_incr_not_compatible)
1179 << Var << TestIsLessOp << NewStep->getSourceRange();
1180 SemaRef.Diag(ConditionLoc,
1181 diag::note_omp_loop_cond_requres_compatible_incr)
1182 << TestIsLessOp << ConditionSrcRange;
1183 return true;
1184 }
1185 }
1186
1187 Step = NewStep;
1188 SubtractStep = Subtract;
1189 return false;
1190}
1191
1192bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1193 // Check init-expr for canonical loop form and save loop counter
1194 // variable - #Var and its initialization value - #LB.
1195 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1196 // var = lb
1197 // integer-type var = lb
1198 // random-access-iterator-type var = lb
1199 // pointer-type var = lb
1200 //
1201 if (!S) {
1202 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1203 return true;
1204 }
1205 if (Expr *E = dyn_cast<Expr>(S))
1206 S = E->IgnoreParens();
1207 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1208 if (BO->getOpcode() == BO_Assign)
1209 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1210 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1211 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1212 if (DS->isSingleDecl()) {
1213 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1214 if (Var->hasInit()) {
1215 // Accept non-canonical init form here but emit ext. warning.
1216 if (Var->getInitStyle() != VarDecl::CInit)
1217 SemaRef.Diag(S->getLocStart(),
1218 diag::ext_omp_loop_not_canonical_init)
1219 << S->getSourceRange();
1220 return SetVarAndLB(Var, Var->getInit());
1221 }
1222 }
1223 }
1224 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1225 if (CE->getOperator() == OO_Equal)
1226 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1227 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1228
1229 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1230 << S->getSourceRange();
1231 return true;
1232}
1233
Alexey Bataev23b69422014-06-18 07:08:49 +00001234/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001235/// variable (which may be the loop variable) if possible.
1236static const VarDecl *GetInitVarDecl(const Expr *E) {
1237 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001238 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001239 E = E->IgnoreParenImpCasts();
1240 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1241 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1242 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1243 CE->getArg(0) != nullptr)
1244 E = CE->getArg(0)->IgnoreParenImpCasts();
1245 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1246 if (!DRE)
1247 return nullptr;
1248 return dyn_cast<VarDecl>(DRE->getDecl());
1249}
1250
1251bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1252 // Check test-expr for canonical form, save upper-bound UB, flags for
1253 // less/greater and for strict/non-strict comparison.
1254 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1255 // var relational-op b
1256 // b relational-op var
1257 //
1258 if (!S) {
1259 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1260 return true;
1261 }
1262 S = S->IgnoreParenImpCasts();
1263 SourceLocation CondLoc = S->getLocStart();
1264 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1265 if (BO->isRelationalOp()) {
1266 if (GetInitVarDecl(BO->getLHS()) == Var)
1267 return SetUB(BO->getRHS(),
1268 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1269 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1270 BO->getSourceRange(), BO->getOperatorLoc());
1271 if (GetInitVarDecl(BO->getRHS()) == Var)
1272 return SetUB(BO->getLHS(),
1273 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1274 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1275 BO->getSourceRange(), BO->getOperatorLoc());
1276 }
1277 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1278 if (CE->getNumArgs() == 2) {
1279 auto Op = CE->getOperator();
1280 switch (Op) {
1281 case OO_Greater:
1282 case OO_GreaterEqual:
1283 case OO_Less:
1284 case OO_LessEqual:
1285 if (GetInitVarDecl(CE->getArg(0)) == Var)
1286 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1287 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1288 CE->getOperatorLoc());
1289 if (GetInitVarDecl(CE->getArg(1)) == Var)
1290 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1291 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1292 CE->getOperatorLoc());
1293 break;
1294 default:
1295 break;
1296 }
1297 }
1298 }
1299 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1300 << S->getSourceRange() << Var;
1301 return true;
1302}
1303
1304bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1305 // RHS of canonical loop form increment can be:
1306 // var + incr
1307 // incr + var
1308 // var - incr
1309 //
1310 RHS = RHS->IgnoreParenImpCasts();
1311 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1312 if (BO->isAdditiveOp()) {
1313 bool IsAdd = BO->getOpcode() == BO_Add;
1314 if (GetInitVarDecl(BO->getLHS()) == Var)
1315 return SetStep(BO->getRHS(), !IsAdd);
1316 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1317 return SetStep(BO->getLHS(), false);
1318 }
1319 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1320 bool IsAdd = CE->getOperator() == OO_Plus;
1321 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1322 if (GetInitVarDecl(CE->getArg(0)) == Var)
1323 return SetStep(CE->getArg(1), !IsAdd);
1324 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1325 return SetStep(CE->getArg(0), false);
1326 }
1327 }
1328 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1329 << RHS->getSourceRange() << Var;
1330 return true;
1331}
1332
1333bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1334 // Check incr-expr for canonical loop form and return true if it
1335 // does not conform.
1336 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1337 // ++var
1338 // var++
1339 // --var
1340 // var--
1341 // var += incr
1342 // var -= incr
1343 // var = var + incr
1344 // var = incr + var
1345 // var = var - incr
1346 //
1347 if (!S) {
1348 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1349 return true;
1350 }
1351 S = S->IgnoreParens();
1352 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1353 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1354 return SetStep(
1355 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1356 (UO->isDecrementOp() ? -1 : 1)).get(),
1357 false);
1358 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1359 switch (BO->getOpcode()) {
1360 case BO_AddAssign:
1361 case BO_SubAssign:
1362 if (GetInitVarDecl(BO->getLHS()) == Var)
1363 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1364 break;
1365 case BO_Assign:
1366 if (GetInitVarDecl(BO->getLHS()) == Var)
1367 return CheckIncRHS(BO->getRHS());
1368 break;
1369 default:
1370 break;
1371 }
1372 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1373 switch (CE->getOperator()) {
1374 case OO_PlusPlus:
1375 case OO_MinusMinus:
1376 if (GetInitVarDecl(CE->getArg(0)) == Var)
1377 return SetStep(
1378 SemaRef.ActOnIntegerConstant(
1379 CE->getLocStart(),
1380 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1381 false);
1382 break;
1383 case OO_PlusEqual:
1384 case OO_MinusEqual:
1385 if (GetInitVarDecl(CE->getArg(0)) == Var)
1386 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1387 break;
1388 case OO_Equal:
1389 if (GetInitVarDecl(CE->getArg(0)) == Var)
1390 return CheckIncRHS(CE->getArg(1));
1391 break;
1392 default:
1393 break;
1394 }
1395 }
1396 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1397 << S->getSourceRange() << Var;
1398 return true;
1399}
Alexey Bataev23b69422014-06-18 07:08:49 +00001400} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001401
1402/// \brief Called on a for stmt to check and extract its iteration space
1403/// for further processing (such as collapsing).
1404static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001405 Sema &SemaRef, DSAStackTy &DSA,
1406 unsigned CurrentNestedLoopCount,
1407 unsigned NestedLoopCount,
1408 Expr *NestedLoopCountExpr) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001409 // OpenMP [2.6, Canonical Loop Form]
1410 // for (init-expr; test-expr; incr-expr) structured-block
1411 auto For = dyn_cast_or_null<ForStmt>(S);
1412 if (!For) {
1413 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001414 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1415 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1416 << CurrentNestedLoopCount;
1417 if (NestedLoopCount > 1)
1418 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1419 diag::note_omp_collapse_expr)
1420 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001421 return true;
1422 }
1423 assert(For->getBody());
1424
1425 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1426
1427 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001428 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001429 if (ISC.CheckInit(Init)) {
1430 return true;
1431 }
1432
1433 bool HasErrors = false;
1434
1435 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001436 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001437
1438 // OpenMP [2.6, Canonical Loop Form]
1439 // Var is one of the following:
1440 // A variable of signed or unsigned integer type.
1441 // For C++, a variable of a random access iterator type.
1442 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001443 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001444 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1445 !VarType->isPointerType() &&
1446 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1447 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1448 << SemaRef.getLangOpts().CPlusPlus;
1449 HasErrors = true;
1450 }
1451
1452 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1453 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001454 // The loop iteration variable in the associated for-loop of a simd construct
1455 // with just one associated for-loop may be listed in a linear clause with a
1456 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001457 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1458 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001459 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001460 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1461 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) ||
1462 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1463 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001464 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001465 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1466 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001467 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001468 HasErrors = true;
1469 } else {
1470 // Make the loop iteration variable private by default.
1471 DSA.addDSA(Var, nullptr, OMPC_private);
1472 }
1473
Alexey Bataev7ff55242014-06-19 09:13:45 +00001474 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001475
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001476 // Check test-expr.
1477 HasErrors |= ISC.CheckCond(For->getCond());
1478
1479 // Check incr-expr.
1480 HasErrors |= ISC.CheckInc(For->getInc());
1481
1482 if (ISC.Dependent())
1483 return HasErrors;
1484
1485 // FIXME: Build loop's iteration space representation.
1486 return HasErrors;
1487}
1488
1489/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1490/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1491/// to get the first for loop.
1492static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1493 if (IgnoreCaptured)
1494 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1495 S = CapS->getCapturedStmt();
1496 // OpenMP [2.8.1, simd construct, Restrictions]
1497 // All loops associated with the construct must be perfectly nested; that is,
1498 // there must be no intervening code nor any OpenMP directive between any two
1499 // loops.
1500 while (true) {
1501 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1502 S = AS->getSubStmt();
1503 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1504 if (CS->size() != 1)
1505 break;
1506 S = CS->body_back();
1507 } else
1508 break;
1509 }
1510 return S;
1511}
1512
1513/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001514/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1515/// number of collapsed loops otherwise.
1516static unsigned CheckOpenMPLoop(OpenMPDirectiveKind DKind,
1517 Expr *NestedLoopCountExpr, Stmt *AStmt,
1518 Sema &SemaRef, DSAStackTy &DSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001519 unsigned NestedLoopCount = 1;
1520 if (NestedLoopCountExpr) {
1521 // Found 'collapse' clause - calculate collapse number.
1522 llvm::APSInt Result;
1523 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1524 NestedLoopCount = Result.getLimitedValue();
1525 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001526 // This is helper routine for loop directives (e.g., 'for', 'simd',
1527 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001528 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1529 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001530 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
1531 NestedLoopCount, NestedLoopCountExpr))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001532 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001533 // Move on to the next nested for loop, or to the loop body.
1534 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1535 }
1536
1537 // FIXME: Build resulting iteration space for IR generation (collapsing
1538 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001539 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001540}
1541
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001542static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001543 auto CollapseFilter = [](const OMPClause *C) -> bool {
1544 return C->getClauseKind() == OMPC_collapse;
1545 };
1546 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1547 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001548 if (I)
1549 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1550 return nullptr;
1551}
1552
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001553StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001554 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001555 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001556 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataevabfc0692014-06-25 06:52:00 +00001557 unsigned NestedLoopCount = CheckOpenMPLoop(
1558 OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack);
1559 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001560 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001561
1562 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001563 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1564 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001565}
1566
Alexey Bataevf29276e2014-06-18 04:14:57 +00001567StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses,
1568 Stmt *AStmt, SourceLocation StartLoc,
1569 SourceLocation EndLoc) {
1570 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataevabfc0692014-06-25 06:52:00 +00001571 unsigned NestedLoopCount = CheckOpenMPLoop(
1572 OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack);
1573 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001574 return StmtError();
1575
1576 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001577 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1578 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001579}
1580
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001581StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1582 Stmt *AStmt,
1583 SourceLocation StartLoc,
1584 SourceLocation EndLoc) {
1585 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1586 auto BaseStmt = AStmt;
1587 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1588 BaseStmt = CS->getCapturedStmt();
1589 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1590 auto S = C->children();
1591 if (!S)
1592 return StmtError();
1593 // All associated statements must be '#pragma omp section' except for
1594 // the first one.
1595 // TODO
1596 } else {
1597 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1598 return StmtError();
1599 }
1600
1601 getCurFunction()->setHasBranchProtectedScope();
1602
1603 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1604 AStmt);
1605}
1606
Alexey Bataeved09d242014-05-28 05:53:51 +00001607OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001608 SourceLocation StartLoc,
1609 SourceLocation LParenLoc,
1610 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001611 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001612 switch (Kind) {
1613 case OMPC_if:
1614 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1615 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001616 case OMPC_num_threads:
1617 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1618 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001619 case OMPC_safelen:
1620 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1621 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001622 case OMPC_collapse:
1623 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1624 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001625 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001626 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001627 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001628 case OMPC_private:
1629 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001630 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001631 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001632 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001633 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001634 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001635 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001636 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001637 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001638 case OMPC_threadprivate:
1639 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001640 llvm_unreachable("Clause is not allowed.");
1641 }
1642 return Res;
1643}
1644
Alexey Bataeved09d242014-05-28 05:53:51 +00001645OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001646 SourceLocation LParenLoc,
1647 SourceLocation EndLoc) {
1648 Expr *ValExpr = Condition;
1649 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1650 !Condition->isInstantiationDependent() &&
1651 !Condition->containsUnexpandedParameterPack()) {
1652 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001653 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001654 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001655 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001656
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001657 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001658 }
1659
1660 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1661}
1662
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001663ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1664 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001665 if (!Op)
1666 return ExprError();
1667
1668 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1669 public:
1670 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001671 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001672 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1673 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001674 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1675 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001676 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1677 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001678 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1679 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001680 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1681 QualType T,
1682 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001683 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1684 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001685 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1686 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001687 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001688 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001689 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001690 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1691 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001692 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1693 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001694 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1695 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001696 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001697 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001698 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001699 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1700 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001701 llvm_unreachable("conversion functions are permitted");
1702 }
1703 } ConvertDiagnoser;
1704 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1705}
1706
1707OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1708 SourceLocation StartLoc,
1709 SourceLocation LParenLoc,
1710 SourceLocation EndLoc) {
1711 Expr *ValExpr = NumThreads;
1712 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1713 !NumThreads->isInstantiationDependent() &&
1714 !NumThreads->containsUnexpandedParameterPack()) {
1715 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1716 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001717 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001718 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001719 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001720
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001721 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001722
1723 // OpenMP [2.5, Restrictions]
1724 // The num_threads expression must evaluate to a positive integer value.
1725 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001726 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1727 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001728 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1729 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001730 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001731 }
1732 }
1733
Alexey Bataeved09d242014-05-28 05:53:51 +00001734 return new (Context)
1735 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001736}
1737
Alexey Bataev62c87d22014-03-21 04:51:18 +00001738ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1739 OpenMPClauseKind CKind) {
1740 if (!E)
1741 return ExprError();
1742 if (E->isValueDependent() || E->isTypeDependent() ||
1743 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001744 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001745 llvm::APSInt Result;
1746 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1747 if (ICE.isInvalid())
1748 return ExprError();
1749 if (!Result.isStrictlyPositive()) {
1750 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1751 << getOpenMPClauseName(CKind) << E->getSourceRange();
1752 return ExprError();
1753 }
1754 return ICE;
1755}
1756
1757OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1758 SourceLocation LParenLoc,
1759 SourceLocation EndLoc) {
1760 // OpenMP [2.8.1, simd construct, Description]
1761 // The parameter of the safelen clause must be a constant
1762 // positive integer expression.
1763 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1764 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001765 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001766 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001767 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001768}
1769
Alexander Musman64d33f12014-06-04 07:53:32 +00001770OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1771 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001772 SourceLocation LParenLoc,
1773 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001774 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001775 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001776 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001777 // The parameter of the collapse clause must be a constant
1778 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001779 ExprResult NumForLoopsResult =
1780 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1781 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001782 return nullptr;
1783 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001784 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001785}
1786
Alexey Bataeved09d242014-05-28 05:53:51 +00001787OMPClause *Sema::ActOnOpenMPSimpleClause(
1788 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1789 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001790 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001791 switch (Kind) {
1792 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001793 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001794 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1795 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001796 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001797 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001798 Res = ActOnOpenMPProcBindClause(
1799 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1800 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001801 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001802 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001803 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001804 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001805 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001806 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001807 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001808 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001809 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001810 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001811 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001812 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001813 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001814 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001815 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001816 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001817 case OMPC_threadprivate:
1818 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001819 llvm_unreachable("Clause is not allowed.");
1820 }
1821 return Res;
1822}
1823
1824OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1825 SourceLocation KindKwLoc,
1826 SourceLocation StartLoc,
1827 SourceLocation LParenLoc,
1828 SourceLocation EndLoc) {
1829 if (Kind == OMPC_DEFAULT_unknown) {
1830 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001831 static_assert(OMPC_DEFAULT_unknown > 0,
1832 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001833 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001834 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001835 Values += "'";
1836 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1837 Values += "'";
1838 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001839 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001840 Values += " or ";
1841 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001842 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001843 break;
1844 default:
1845 Values += Sep;
1846 break;
1847 }
1848 }
1849 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001850 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001851 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001852 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001853 switch (Kind) {
1854 case OMPC_DEFAULT_none:
1855 DSAStack->setDefaultDSANone();
1856 break;
1857 case OMPC_DEFAULT_shared:
1858 DSAStack->setDefaultDSAShared();
1859 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001860 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001861 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001862 break;
1863 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001864 return new (Context)
1865 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001866}
1867
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001868OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1869 SourceLocation KindKwLoc,
1870 SourceLocation StartLoc,
1871 SourceLocation LParenLoc,
1872 SourceLocation EndLoc) {
1873 if (Kind == OMPC_PROC_BIND_unknown) {
1874 std::string Values;
1875 std::string Sep(", ");
1876 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1877 Values += "'";
1878 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1879 Values += "'";
1880 switch (i) {
1881 case OMPC_PROC_BIND_unknown - 2:
1882 Values += " or ";
1883 break;
1884 case OMPC_PROC_BIND_unknown - 1:
1885 break;
1886 default:
1887 Values += Sep;
1888 break;
1889 }
1890 }
1891 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001892 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001893 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001894 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001895 return new (Context)
1896 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001897}
1898
Alexey Bataev56dafe82014-06-20 07:16:17 +00001899OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1900 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
1901 SourceLocation StartLoc, SourceLocation LParenLoc,
1902 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
1903 SourceLocation EndLoc) {
1904 OMPClause *Res = nullptr;
1905 switch (Kind) {
1906 case OMPC_schedule:
1907 Res = ActOnOpenMPScheduleClause(
1908 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
1909 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
1910 break;
1911 case OMPC_if:
1912 case OMPC_num_threads:
1913 case OMPC_safelen:
1914 case OMPC_collapse:
1915 case OMPC_default:
1916 case OMPC_proc_bind:
1917 case OMPC_private:
1918 case OMPC_firstprivate:
1919 case OMPC_lastprivate:
1920 case OMPC_shared:
1921 case OMPC_reduction:
1922 case OMPC_linear:
1923 case OMPC_aligned:
1924 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001925 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001926 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001927 case OMPC_threadprivate:
1928 case OMPC_unknown:
1929 llvm_unreachable("Clause is not allowed.");
1930 }
1931 return Res;
1932}
1933
1934OMPClause *Sema::ActOnOpenMPScheduleClause(
1935 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
1936 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
1937 SourceLocation EndLoc) {
1938 if (Kind == OMPC_SCHEDULE_unknown) {
1939 std::string Values;
1940 std::string Sep(", ");
1941 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
1942 Values += "'";
1943 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
1944 Values += "'";
1945 switch (i) {
1946 case OMPC_SCHEDULE_unknown - 2:
1947 Values += " or ";
1948 break;
1949 case OMPC_SCHEDULE_unknown - 1:
1950 break;
1951 default:
1952 Values += Sep;
1953 break;
1954 }
1955 }
1956 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
1957 << Values << getOpenMPClauseName(OMPC_schedule);
1958 return nullptr;
1959 }
1960 Expr *ValExpr = ChunkSize;
1961 if (ChunkSize) {
1962 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
1963 !ChunkSize->isInstantiationDependent() &&
1964 !ChunkSize->containsUnexpandedParameterPack()) {
1965 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
1966 ExprResult Val =
1967 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
1968 if (Val.isInvalid())
1969 return nullptr;
1970
1971 ValExpr = Val.get();
1972
1973 // OpenMP [2.7.1, Restrictions]
1974 // chunk_size must be a loop invariant integer expression with a positive
1975 // value.
1976 llvm::APSInt Result;
1977 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
1978 Result.isSigned() && !Result.isStrictlyPositive()) {
1979 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
1980 << "schedule" << ChunkSize->getSourceRange();
1981 return nullptr;
1982 }
1983 }
1984 }
1985
1986 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
1987 EndLoc, Kind, ValExpr);
1988}
1989
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001990OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
1991 SourceLocation StartLoc,
1992 SourceLocation EndLoc) {
1993 OMPClause *Res = nullptr;
1994 switch (Kind) {
1995 case OMPC_ordered:
1996 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
1997 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00001998 case OMPC_nowait:
1999 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2000 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002001 case OMPC_if:
2002 case OMPC_num_threads:
2003 case OMPC_safelen:
2004 case OMPC_collapse:
2005 case OMPC_schedule:
2006 case OMPC_private:
2007 case OMPC_firstprivate:
2008 case OMPC_lastprivate:
2009 case OMPC_shared:
2010 case OMPC_reduction:
2011 case OMPC_linear:
2012 case OMPC_aligned:
2013 case OMPC_copyin:
2014 case OMPC_default:
2015 case OMPC_proc_bind:
2016 case OMPC_threadprivate:
2017 case OMPC_unknown:
2018 llvm_unreachable("Clause is not allowed.");
2019 }
2020 return Res;
2021}
2022
2023OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2024 SourceLocation EndLoc) {
2025 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2026}
2027
Alexey Bataev236070f2014-06-20 11:19:47 +00002028OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2029 SourceLocation EndLoc) {
2030 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2031}
2032
Alexey Bataevc5e02582014-06-16 07:08:35 +00002033OMPClause *Sema::ActOnOpenMPVarListClause(
2034 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2035 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2036 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2037 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002038 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002039 switch (Kind) {
2040 case OMPC_private:
2041 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2042 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002043 case OMPC_firstprivate:
2044 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2045 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002046 case OMPC_lastprivate:
2047 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2048 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002049 case OMPC_shared:
2050 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2051 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002052 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002053 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2054 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002055 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002056 case OMPC_linear:
2057 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2058 ColonLoc, EndLoc);
2059 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002060 case OMPC_aligned:
2061 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2062 ColonLoc, EndLoc);
2063 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002064 case OMPC_copyin:
2065 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2066 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002067 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002068 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002069 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002070 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002071 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002072 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002073 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002074 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002075 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002076 case OMPC_threadprivate:
2077 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002078 llvm_unreachable("Clause is not allowed.");
2079 }
2080 return Res;
2081}
2082
2083OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2084 SourceLocation StartLoc,
2085 SourceLocation LParenLoc,
2086 SourceLocation EndLoc) {
2087 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002088 for (auto &RefExpr : VarList) {
2089 assert(RefExpr && "NULL expr in OpenMP private clause.");
2090 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002091 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002092 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002093 continue;
2094 }
2095
Alexey Bataeved09d242014-05-28 05:53:51 +00002096 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002097 // OpenMP [2.1, C/C++]
2098 // A list item is a variable name.
2099 // OpenMP [2.9.3.3, Restrictions, p.1]
2100 // A variable that is part of another variable (as an array or
2101 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002102 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002103 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002104 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002105 continue;
2106 }
2107 Decl *D = DE->getDecl();
2108 VarDecl *VD = cast<VarDecl>(D);
2109
2110 QualType Type = VD->getType();
2111 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2112 // It will be analyzed later.
2113 Vars.push_back(DE);
2114 continue;
2115 }
2116
2117 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2118 // A variable that appears in a private clause must not have an incomplete
2119 // type or a reference type.
2120 if (RequireCompleteType(ELoc, Type,
2121 diag::err_omp_private_incomplete_type)) {
2122 continue;
2123 }
2124 if (Type->isReferenceType()) {
2125 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002126 << getOpenMPClauseName(OMPC_private) << Type;
2127 bool IsDecl =
2128 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2129 Diag(VD->getLocation(),
2130 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2131 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002132 continue;
2133 }
2134
2135 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2136 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002137 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002138 // class type.
2139 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002140 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2141 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002142 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002143 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2144 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2145 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002146 // FIXME This code must be replaced by actual constructing/destructing of
2147 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002148 if (RD) {
2149 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2150 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002151 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002152 if (!CD ||
2153 CheckConstructorAccess(ELoc, CD,
2154 InitializedEntity::InitializeTemporary(Type),
2155 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002156 CD->isDeleted()) {
2157 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002158 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002159 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2160 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002161 Diag(VD->getLocation(),
2162 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2163 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002164 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2165 continue;
2166 }
2167 MarkFunctionReferenced(ELoc, CD);
2168 DiagnoseUseOfDecl(CD, ELoc);
2169
2170 CXXDestructorDecl *DD = RD->getDestructor();
2171 if (DD) {
2172 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2173 DD->isDeleted()) {
2174 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002175 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002176 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2177 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002178 Diag(VD->getLocation(),
2179 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2180 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002181 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2182 continue;
2183 }
2184 MarkFunctionReferenced(ELoc, DD);
2185 DiagnoseUseOfDecl(DD, ELoc);
2186 }
2187 }
2188
Alexey Bataev758e55e2013-09-06 18:03:48 +00002189 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2190 // in a Construct]
2191 // Variables with the predetermined data-sharing attributes may not be
2192 // listed in data-sharing attributes clauses, except for the cases
2193 // listed below. For these exceptions only, listing a predetermined
2194 // variable in a data-sharing attribute clause is allowed and overrides
2195 // the variable's predetermined data-sharing attributes.
2196 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2197 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002198 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2199 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002200 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002201 continue;
2202 }
2203
2204 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002205 Vars.push_back(DE);
2206 }
2207
Alexey Bataeved09d242014-05-28 05:53:51 +00002208 if (Vars.empty())
2209 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002210
2211 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2212}
2213
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002214OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2215 SourceLocation StartLoc,
2216 SourceLocation LParenLoc,
2217 SourceLocation EndLoc) {
2218 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002219 for (auto &RefExpr : VarList) {
2220 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2221 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002222 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002223 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002224 continue;
2225 }
2226
Alexey Bataeved09d242014-05-28 05:53:51 +00002227 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002228 // OpenMP [2.1, C/C++]
2229 // A list item is a variable name.
2230 // OpenMP [2.9.3.3, Restrictions, p.1]
2231 // A variable that is part of another variable (as an array or
2232 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002233 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002234 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002235 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002236 continue;
2237 }
2238 Decl *D = DE->getDecl();
2239 VarDecl *VD = cast<VarDecl>(D);
2240
2241 QualType Type = VD->getType();
2242 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2243 // It will be analyzed later.
2244 Vars.push_back(DE);
2245 continue;
2246 }
2247
2248 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2249 // A variable that appears in a private clause must not have an incomplete
2250 // type or a reference type.
2251 if (RequireCompleteType(ELoc, Type,
2252 diag::err_omp_firstprivate_incomplete_type)) {
2253 continue;
2254 }
2255 if (Type->isReferenceType()) {
2256 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002257 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2258 bool IsDecl =
2259 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2260 Diag(VD->getLocation(),
2261 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2262 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002263 continue;
2264 }
2265
2266 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2267 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002268 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002269 // class type.
2270 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002271 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2272 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2273 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002274 // FIXME This code must be replaced by actual constructing/destructing of
2275 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002276 if (RD) {
2277 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2278 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002279 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002280 if (!CD ||
2281 CheckConstructorAccess(ELoc, CD,
2282 InitializedEntity::InitializeTemporary(Type),
2283 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002284 CD->isDeleted()) {
2285 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002286 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002287 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2288 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002289 Diag(VD->getLocation(),
2290 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2291 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002292 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2293 continue;
2294 }
2295 MarkFunctionReferenced(ELoc, CD);
2296 DiagnoseUseOfDecl(CD, ELoc);
2297
2298 CXXDestructorDecl *DD = RD->getDestructor();
2299 if (DD) {
2300 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2301 DD->isDeleted()) {
2302 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002303 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002304 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2305 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002306 Diag(VD->getLocation(),
2307 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2308 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002309 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2310 continue;
2311 }
2312 MarkFunctionReferenced(ELoc, DD);
2313 DiagnoseUseOfDecl(DD, ELoc);
2314 }
2315 }
2316
2317 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2318 // variable and it was checked already.
2319 if (StartLoc.isValid() && EndLoc.isValid()) {
2320 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2321 Type = Type.getNonReferenceType().getCanonicalType();
2322 bool IsConstant = Type.isConstant(Context);
2323 Type = Context.getBaseElementType(Type);
2324 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2325 // A list item that specifies a given variable may not appear in more
2326 // than one clause on the same directive, except that a variable may be
2327 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002328 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002329 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002330 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002331 << getOpenMPClauseName(DVar.CKind)
2332 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002333 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002334 continue;
2335 }
2336
2337 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2338 // in a Construct]
2339 // Variables with the predetermined data-sharing attributes may not be
2340 // listed in data-sharing attributes clauses, except for the cases
2341 // listed below. For these exceptions only, listing a predetermined
2342 // variable in a data-sharing attribute clause is allowed and overrides
2343 // the variable's predetermined data-sharing attributes.
2344 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2345 // in a Construct, C/C++, p.2]
2346 // Variables with const-qualified type having no mutable member may be
2347 // listed in a firstprivate clause, even if they are static data members.
2348 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2349 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2350 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002351 << getOpenMPClauseName(DVar.CKind)
2352 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002353 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002354 continue;
2355 }
2356
Alexey Bataevf29276e2014-06-18 04:14:57 +00002357 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002358 // OpenMP [2.9.3.4, Restrictions, p.2]
2359 // A list item that is private within a parallel region must not appear
2360 // in a firstprivate clause on a worksharing construct if any of the
2361 // worksharing regions arising from the worksharing construct ever bind
2362 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002363 if (isOpenMPWorksharingDirective(CurrDir) &&
2364 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002365 DVar = DSAStack->getImplicitDSA(VD);
2366 if (DVar.CKind != OMPC_shared) {
2367 Diag(ELoc, diag::err_omp_required_access)
2368 << getOpenMPClauseName(OMPC_firstprivate)
2369 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002370 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002371 continue;
2372 }
2373 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002374 // OpenMP [2.9.3.4, Restrictions, p.3]
2375 // A list item that appears in a reduction clause of a parallel construct
2376 // must not appear in a firstprivate clause on a worksharing or task
2377 // construct if any of the worksharing or task regions arising from the
2378 // worksharing or task construct ever bind to any of the parallel regions
2379 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002380 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002381 // OpenMP [2.9.3.4, Restrictions, p.4]
2382 // A list item that appears in a reduction clause in worksharing
2383 // construct must not appear in a firstprivate clause in a task construct
2384 // encountered during execution of any of the worksharing regions arising
2385 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002386 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002387 }
2388
2389 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2390 Vars.push_back(DE);
2391 }
2392
Alexey Bataeved09d242014-05-28 05:53:51 +00002393 if (Vars.empty())
2394 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002395
2396 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2397 Vars);
2398}
2399
Alexander Musman1bb328c2014-06-04 13:06:39 +00002400OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2401 SourceLocation StartLoc,
2402 SourceLocation LParenLoc,
2403 SourceLocation EndLoc) {
2404 SmallVector<Expr *, 8> Vars;
2405 for (auto &RefExpr : VarList) {
2406 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2407 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2408 // It will be analyzed later.
2409 Vars.push_back(RefExpr);
2410 continue;
2411 }
2412
2413 SourceLocation ELoc = RefExpr->getExprLoc();
2414 // OpenMP [2.1, C/C++]
2415 // A list item is a variable name.
2416 // OpenMP [2.14.3.5, Restrictions, p.1]
2417 // A variable that is part of another variable (as an array or structure
2418 // element) cannot appear in a lastprivate clause.
2419 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2420 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2421 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2422 continue;
2423 }
2424 Decl *D = DE->getDecl();
2425 VarDecl *VD = cast<VarDecl>(D);
2426
2427 QualType Type = VD->getType();
2428 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2429 // It will be analyzed later.
2430 Vars.push_back(DE);
2431 continue;
2432 }
2433
2434 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2435 // A variable that appears in a lastprivate clause must not have an
2436 // incomplete type or a reference type.
2437 if (RequireCompleteType(ELoc, Type,
2438 diag::err_omp_lastprivate_incomplete_type)) {
2439 continue;
2440 }
2441 if (Type->isReferenceType()) {
2442 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2443 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2444 bool IsDecl =
2445 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2446 Diag(VD->getLocation(),
2447 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2448 << VD;
2449 continue;
2450 }
2451
2452 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2453 // in a Construct]
2454 // Variables with the predetermined data-sharing attributes may not be
2455 // listed in data-sharing attributes clauses, except for the cases
2456 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002457 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2458 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2459 DVar.CKind != OMPC_firstprivate &&
2460 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2461 Diag(ELoc, diag::err_omp_wrong_dsa)
2462 << getOpenMPClauseName(DVar.CKind)
2463 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002464 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002465 continue;
2466 }
2467
Alexey Bataevf29276e2014-06-18 04:14:57 +00002468 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2469 // OpenMP [2.14.3.5, Restrictions, p.2]
2470 // A list item that is private within a parallel region, or that appears in
2471 // the reduction clause of a parallel construct, must not appear in a
2472 // lastprivate clause on a worksharing construct if any of the corresponding
2473 // worksharing regions ever binds to any of the corresponding parallel
2474 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002475 if (isOpenMPWorksharingDirective(CurrDir) &&
2476 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002477 DVar = DSAStack->getImplicitDSA(VD);
2478 if (DVar.CKind != OMPC_shared) {
2479 Diag(ELoc, diag::err_omp_required_access)
2480 << getOpenMPClauseName(OMPC_lastprivate)
2481 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002482 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002483 continue;
2484 }
2485 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002486 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002487 // A variable of class type (or array thereof) that appears in a
2488 // lastprivate clause requires an accessible, unambiguous default
2489 // constructor for the class type, unless the list item is also specified
2490 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002491 // A variable of class type (or array thereof) that appears in a
2492 // lastprivate clause requires an accessible, unambiguous copy assignment
2493 // operator for the class type.
2494 while (Type.getNonReferenceType()->isArrayType())
2495 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2496 ->getElementType();
2497 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2498 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2499 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002500 // FIXME This code must be replaced by actual copying and destructing of the
2501 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002502 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002503 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2504 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002505 if (MD) {
2506 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2507 MD->isDeleted()) {
2508 Diag(ELoc, diag::err_omp_required_method)
2509 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2510 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2511 VarDecl::DeclarationOnly;
2512 Diag(VD->getLocation(),
2513 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2514 << VD;
2515 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2516 continue;
2517 }
2518 MarkFunctionReferenced(ELoc, MD);
2519 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002520 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002521
2522 CXXDestructorDecl *DD = RD->getDestructor();
2523 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002524 PartialDiagnostic PD =
2525 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002526 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2527 DD->isDeleted()) {
2528 Diag(ELoc, diag::err_omp_required_method)
2529 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2530 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2531 VarDecl::DeclarationOnly;
2532 Diag(VD->getLocation(),
2533 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2534 << VD;
2535 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2536 continue;
2537 }
2538 MarkFunctionReferenced(ELoc, DD);
2539 DiagnoseUseOfDecl(DD, ELoc);
2540 }
2541 }
2542
Alexey Bataevf29276e2014-06-18 04:14:57 +00002543 if (DVar.CKind != OMPC_firstprivate)
2544 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002545 Vars.push_back(DE);
2546 }
2547
2548 if (Vars.empty())
2549 return nullptr;
2550
2551 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2552 Vars);
2553}
2554
Alexey Bataev758e55e2013-09-06 18:03:48 +00002555OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2556 SourceLocation StartLoc,
2557 SourceLocation LParenLoc,
2558 SourceLocation EndLoc) {
2559 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002560 for (auto &RefExpr : VarList) {
2561 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2562 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002563 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002564 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002565 continue;
2566 }
2567
Alexey Bataeved09d242014-05-28 05:53:51 +00002568 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002569 // OpenMP [2.1, C/C++]
2570 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002571 // OpenMP [2.14.3.2, Restrictions, p.1]
2572 // A variable that is part of another variable (as an array or structure
2573 // element) cannot appear in a shared unless it is a static data member
2574 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002575 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002576 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002577 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002578 continue;
2579 }
2580 Decl *D = DE->getDecl();
2581 VarDecl *VD = cast<VarDecl>(D);
2582
2583 QualType Type = VD->getType();
2584 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2585 // It will be analyzed later.
2586 Vars.push_back(DE);
2587 continue;
2588 }
2589
2590 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2591 // in a Construct]
2592 // Variables with the predetermined data-sharing attributes may not be
2593 // listed in data-sharing attributes clauses, except for the cases
2594 // listed below. For these exceptions only, listing a predetermined
2595 // variable in a data-sharing attribute clause is allowed and overrides
2596 // the variable's predetermined data-sharing attributes.
2597 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002598 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2599 DVar.RefExpr) {
2600 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2601 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002602 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002603 continue;
2604 }
2605
2606 DSAStack->addDSA(VD, DE, OMPC_shared);
2607 Vars.push_back(DE);
2608 }
2609
Alexey Bataeved09d242014-05-28 05:53:51 +00002610 if (Vars.empty())
2611 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002612
2613 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2614}
2615
Alexey Bataevc5e02582014-06-16 07:08:35 +00002616namespace {
2617class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2618 DSAStackTy *Stack;
2619
2620public:
2621 bool VisitDeclRefExpr(DeclRefExpr *E) {
2622 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2623 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2624 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2625 return false;
2626 if (DVar.CKind != OMPC_unknown)
2627 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002628 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002629 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002630 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002631 return true;
2632 return false;
2633 }
2634 return false;
2635 }
2636 bool VisitStmt(Stmt *S) {
2637 for (auto Child : S->children()) {
2638 if (Child && Visit(Child))
2639 return true;
2640 }
2641 return false;
2642 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002643 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002644};
Alexey Bataev23b69422014-06-18 07:08:49 +00002645} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002646
2647OMPClause *Sema::ActOnOpenMPReductionClause(
2648 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2649 SourceLocation ColonLoc, SourceLocation EndLoc,
2650 CXXScopeSpec &ReductionIdScopeSpec,
2651 const DeclarationNameInfo &ReductionId) {
2652 // TODO: Allow scope specification search when 'declare reduction' is
2653 // supported.
2654 assert(ReductionIdScopeSpec.isEmpty() &&
2655 "No support for scoped reduction identifiers yet.");
2656
2657 auto DN = ReductionId.getName();
2658 auto OOK = DN.getCXXOverloadedOperator();
2659 BinaryOperatorKind BOK = BO_Comma;
2660
2661 // OpenMP [2.14.3.6, reduction clause]
2662 // C
2663 // reduction-identifier is either an identifier or one of the following
2664 // operators: +, -, *, &, |, ^, && and ||
2665 // C++
2666 // reduction-identifier is either an id-expression or one of the following
2667 // operators: +, -, *, &, |, ^, && and ||
2668 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2669 switch (OOK) {
2670 case OO_Plus:
2671 case OO_Minus:
2672 BOK = BO_AddAssign;
2673 break;
2674 case OO_Star:
2675 BOK = BO_MulAssign;
2676 break;
2677 case OO_Amp:
2678 BOK = BO_AndAssign;
2679 break;
2680 case OO_Pipe:
2681 BOK = BO_OrAssign;
2682 break;
2683 case OO_Caret:
2684 BOK = BO_XorAssign;
2685 break;
2686 case OO_AmpAmp:
2687 BOK = BO_LAnd;
2688 break;
2689 case OO_PipePipe:
2690 BOK = BO_LOr;
2691 break;
2692 default:
2693 if (auto II = DN.getAsIdentifierInfo()) {
2694 if (II->isStr("max"))
2695 BOK = BO_GT;
2696 else if (II->isStr("min"))
2697 BOK = BO_LT;
2698 }
2699 break;
2700 }
2701 SourceRange ReductionIdRange;
2702 if (ReductionIdScopeSpec.isValid()) {
2703 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2704 }
2705 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2706 if (BOK == BO_Comma) {
2707 // Not allowed reduction identifier is found.
2708 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2709 << ReductionIdRange;
2710 return nullptr;
2711 }
2712
2713 SmallVector<Expr *, 8> Vars;
2714 for (auto RefExpr : VarList) {
2715 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2716 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2717 // It will be analyzed later.
2718 Vars.push_back(RefExpr);
2719 continue;
2720 }
2721
2722 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2723 RefExpr->isInstantiationDependent() ||
2724 RefExpr->containsUnexpandedParameterPack()) {
2725 // It will be analyzed later.
2726 Vars.push_back(RefExpr);
2727 continue;
2728 }
2729
2730 auto ELoc = RefExpr->getExprLoc();
2731 auto ERange = RefExpr->getSourceRange();
2732 // OpenMP [2.1, C/C++]
2733 // A list item is a variable or array section, subject to the restrictions
2734 // specified in Section 2.4 on page 42 and in each of the sections
2735 // describing clauses and directives for which a list appears.
2736 // OpenMP [2.14.3.3, Restrictions, p.1]
2737 // A variable that is part of another variable (as an array or
2738 // structure element) cannot appear in a private clause.
2739 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2740 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2741 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2742 continue;
2743 }
2744 auto D = DE->getDecl();
2745 auto VD = cast<VarDecl>(D);
2746 auto Type = VD->getType();
2747 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2748 // A variable that appears in a private clause must not have an incomplete
2749 // type or a reference type.
2750 if (RequireCompleteType(ELoc, Type,
2751 diag::err_omp_reduction_incomplete_type))
2752 continue;
2753 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2754 // Arrays may not appear in a reduction clause.
2755 if (Type.getNonReferenceType()->isArrayType()) {
2756 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2757 bool IsDecl =
2758 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2759 Diag(VD->getLocation(),
2760 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2761 << VD;
2762 continue;
2763 }
2764 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2765 // A list item that appears in a reduction clause must not be
2766 // const-qualified.
2767 if (Type.getNonReferenceType().isConstant(Context)) {
2768 Diag(ELoc, diag::err_omp_const_variable)
2769 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2770 bool IsDecl =
2771 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2772 Diag(VD->getLocation(),
2773 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2774 << VD;
2775 continue;
2776 }
2777 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2778 // If a list-item is a reference type then it must bind to the same object
2779 // for all threads of the team.
2780 VarDecl *VDDef = VD->getDefinition();
2781 if (Type->isReferenceType() && VDDef) {
2782 DSARefChecker Check(DSAStack);
2783 if (Check.Visit(VDDef->getInit())) {
2784 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2785 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2786 continue;
2787 }
2788 }
2789 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2790 // The type of a list item that appears in a reduction clause must be valid
2791 // for the reduction-identifier. For a max or min reduction in C, the type
2792 // of the list item must be an allowed arithmetic data type: char, int,
2793 // float, double, or _Bool, possibly modified with long, short, signed, or
2794 // unsigned. For a max or min reduction in C++, the type of the list item
2795 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2796 // double, or bool, possibly modified with long, short, signed, or unsigned.
2797 if ((BOK == BO_GT || BOK == BO_LT) &&
2798 !(Type->isScalarType() ||
2799 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2800 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2801 << getLangOpts().CPlusPlus;
2802 bool IsDecl =
2803 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2804 Diag(VD->getLocation(),
2805 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2806 << VD;
2807 continue;
2808 }
2809 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2810 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2811 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2812 bool IsDecl =
2813 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2814 Diag(VD->getLocation(),
2815 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2816 << VD;
2817 continue;
2818 }
2819 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2820 getDiagnostics().setSuppressAllDiagnostics(true);
2821 ExprResult ReductionOp =
2822 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2823 RefExpr, RefExpr);
2824 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2825 if (ReductionOp.isInvalid()) {
2826 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002827 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002828 bool IsDecl =
2829 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2830 Diag(VD->getLocation(),
2831 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2832 << VD;
2833 continue;
2834 }
2835
2836 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2837 // in a Construct]
2838 // Variables with the predetermined data-sharing attributes may not be
2839 // listed in data-sharing attributes clauses, except for the cases
2840 // listed below. For these exceptions only, listing a predetermined
2841 // variable in a data-sharing attribute clause is allowed and overrides
2842 // the variable's predetermined data-sharing attributes.
2843 // OpenMP [2.14.3.6, Restrictions, p.3]
2844 // Any number of reduction clauses can be specified on the directive,
2845 // but a list item can appear only once in the reduction clauses for that
2846 // directive.
2847 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2848 if (DVar.CKind == OMPC_reduction) {
2849 Diag(ELoc, diag::err_omp_once_referenced)
2850 << getOpenMPClauseName(OMPC_reduction);
2851 if (DVar.RefExpr) {
2852 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2853 }
2854 } else if (DVar.CKind != OMPC_unknown) {
2855 Diag(ELoc, diag::err_omp_wrong_dsa)
2856 << getOpenMPClauseName(DVar.CKind)
2857 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002858 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002859 continue;
2860 }
2861
2862 // OpenMP [2.14.3.6, Restrictions, p.1]
2863 // A list item that appears in a reduction clause of a worksharing
2864 // construct must be shared in the parallel regions to which any of the
2865 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002866 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00002867 if (isOpenMPWorksharingDirective(CurrDir) &&
2868 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002869 DVar = DSAStack->getImplicitDSA(VD);
2870 if (DVar.CKind != OMPC_shared) {
2871 Diag(ELoc, diag::err_omp_required_access)
2872 << getOpenMPClauseName(OMPC_reduction)
2873 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002874 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002875 continue;
2876 }
2877 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00002878
2879 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2880 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2881 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002882 // FIXME This code must be replaced by actual constructing/destructing of
2883 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00002884 if (RD) {
2885 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2886 PartialDiagnostic PD =
2887 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00002888 if (!CD ||
2889 CheckConstructorAccess(ELoc, CD,
2890 InitializedEntity::InitializeTemporary(Type),
2891 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00002892 CD->isDeleted()) {
2893 Diag(ELoc, diag::err_omp_required_method)
2894 << getOpenMPClauseName(OMPC_reduction) << 0;
2895 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2896 VarDecl::DeclarationOnly;
2897 Diag(VD->getLocation(),
2898 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2899 << VD;
2900 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2901 continue;
2902 }
2903 MarkFunctionReferenced(ELoc, CD);
2904 DiagnoseUseOfDecl(CD, ELoc);
2905
2906 CXXDestructorDecl *DD = RD->getDestructor();
2907 if (DD) {
2908 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2909 DD->isDeleted()) {
2910 Diag(ELoc, diag::err_omp_required_method)
2911 << getOpenMPClauseName(OMPC_reduction) << 4;
2912 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2913 VarDecl::DeclarationOnly;
2914 Diag(VD->getLocation(),
2915 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2916 << VD;
2917 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2918 continue;
2919 }
2920 MarkFunctionReferenced(ELoc, DD);
2921 DiagnoseUseOfDecl(DD, ELoc);
2922 }
2923 }
2924
2925 DSAStack->addDSA(VD, DE, OMPC_reduction);
2926 Vars.push_back(DE);
2927 }
2928
2929 if (Vars.empty())
2930 return nullptr;
2931
2932 return OMPReductionClause::Create(
2933 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
2934 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
2935}
2936
Alexander Musman8dba6642014-04-22 13:09:42 +00002937OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2938 SourceLocation StartLoc,
2939 SourceLocation LParenLoc,
2940 SourceLocation ColonLoc,
2941 SourceLocation EndLoc) {
2942 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002943 for (auto &RefExpr : VarList) {
2944 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2945 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002946 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002947 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002948 continue;
2949 }
2950
2951 // OpenMP [2.14.3.7, linear clause]
2952 // A list item that appears in a linear clause is subject to the private
2953 // clause semantics described in Section 2.14.3.3 on page 159 except as
2954 // noted. In addition, the value of the new list item on each iteration
2955 // of the associated loop(s) corresponds to the value of the original
2956 // list item before entering the construct plus the logical number of
2957 // the iteration times linear-step.
2958
Alexey Bataeved09d242014-05-28 05:53:51 +00002959 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00002960 // OpenMP [2.1, C/C++]
2961 // A list item is a variable name.
2962 // OpenMP [2.14.3.3, Restrictions, p.1]
2963 // A variable that is part of another variable (as an array or
2964 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002965 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002966 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002967 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00002968 continue;
2969 }
2970
2971 VarDecl *VD = cast<VarDecl>(DE->getDecl());
2972
2973 // OpenMP [2.14.3.7, linear clause]
2974 // A list-item cannot appear in more than one linear clause.
2975 // A list-item that appears in a linear clause cannot appear in any
2976 // other data-sharing attribute clause.
2977 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2978 if (DVar.RefExpr) {
2979 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2980 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002981 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00002982 continue;
2983 }
2984
2985 QualType QType = VD->getType();
2986 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2987 // It will be analyzed later.
2988 Vars.push_back(DE);
2989 continue;
2990 }
2991
2992 // A variable must not have an incomplete type or a reference type.
2993 if (RequireCompleteType(ELoc, QType,
2994 diag::err_omp_linear_incomplete_type)) {
2995 continue;
2996 }
2997 if (QType->isReferenceType()) {
2998 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2999 << getOpenMPClauseName(OMPC_linear) << QType;
3000 bool IsDecl =
3001 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3002 Diag(VD->getLocation(),
3003 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3004 << VD;
3005 continue;
3006 }
3007
3008 // A list item must not be const-qualified.
3009 if (QType.isConstant(Context)) {
3010 Diag(ELoc, diag::err_omp_const_variable)
3011 << getOpenMPClauseName(OMPC_linear);
3012 bool IsDecl =
3013 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3014 Diag(VD->getLocation(),
3015 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3016 << VD;
3017 continue;
3018 }
3019
3020 // A list item must be of integral or pointer type.
3021 QType = QType.getUnqualifiedType().getCanonicalType();
3022 const Type *Ty = QType.getTypePtrOrNull();
3023 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3024 !Ty->isPointerType())) {
3025 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3026 bool IsDecl =
3027 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3028 Diag(VD->getLocation(),
3029 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3030 << VD;
3031 continue;
3032 }
3033
3034 DSAStack->addDSA(VD, DE, OMPC_linear);
3035 Vars.push_back(DE);
3036 }
3037
3038 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003039 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003040
3041 Expr *StepExpr = Step;
3042 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3043 !Step->isInstantiationDependent() &&
3044 !Step->containsUnexpandedParameterPack()) {
3045 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003046 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003047 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003048 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003049 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003050
3051 // Warn about zero linear step (it would be probably better specified as
3052 // making corresponding variables 'const').
3053 llvm::APSInt Result;
3054 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3055 !Result.isNegative() && !Result.isStrictlyPositive())
3056 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3057 << (Vars.size() > 1);
3058 }
3059
3060 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3061 Vars, StepExpr);
3062}
3063
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003064OMPClause *Sema::ActOnOpenMPAlignedClause(
3065 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3066 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3067
3068 SmallVector<Expr *, 8> Vars;
3069 for (auto &RefExpr : VarList) {
3070 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3071 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3072 // It will be analyzed later.
3073 Vars.push_back(RefExpr);
3074 continue;
3075 }
3076
3077 SourceLocation ELoc = RefExpr->getExprLoc();
3078 // OpenMP [2.1, C/C++]
3079 // A list item is a variable name.
3080 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3081 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3082 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3083 continue;
3084 }
3085
3086 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3087
3088 // OpenMP [2.8.1, simd construct, Restrictions]
3089 // The type of list items appearing in the aligned clause must be
3090 // array, pointer, reference to array, or reference to pointer.
3091 QualType QType = DE->getType()
3092 .getNonReferenceType()
3093 .getUnqualifiedType()
3094 .getCanonicalType();
3095 const Type *Ty = QType.getTypePtrOrNull();
3096 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3097 !Ty->isPointerType())) {
3098 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3099 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3100 bool IsDecl =
3101 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3102 Diag(VD->getLocation(),
3103 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3104 << VD;
3105 continue;
3106 }
3107
3108 // OpenMP [2.8.1, simd construct, Restrictions]
3109 // A list-item cannot appear in more than one aligned clause.
3110 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3111 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3112 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3113 << getOpenMPClauseName(OMPC_aligned);
3114 continue;
3115 }
3116
3117 Vars.push_back(DE);
3118 }
3119
3120 // OpenMP [2.8.1, simd construct, Description]
3121 // The parameter of the aligned clause, alignment, must be a constant
3122 // positive integer expression.
3123 // If no optional parameter is specified, implementation-defined default
3124 // alignments for SIMD instructions on the target platforms are assumed.
3125 if (Alignment != nullptr) {
3126 ExprResult AlignResult =
3127 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3128 if (AlignResult.isInvalid())
3129 return nullptr;
3130 Alignment = AlignResult.get();
3131 }
3132 if (Vars.empty())
3133 return nullptr;
3134
3135 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3136 EndLoc, Vars, Alignment);
3137}
3138
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003139OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3140 SourceLocation StartLoc,
3141 SourceLocation LParenLoc,
3142 SourceLocation EndLoc) {
3143 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003144 for (auto &RefExpr : VarList) {
3145 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3146 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003147 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003148 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003149 continue;
3150 }
3151
Alexey Bataeved09d242014-05-28 05:53:51 +00003152 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003153 // OpenMP [2.1, C/C++]
3154 // A list item is a variable name.
3155 // OpenMP [2.14.4.1, Restrictions, p.1]
3156 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003157 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003158 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003159 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003160 continue;
3161 }
3162
3163 Decl *D = DE->getDecl();
3164 VarDecl *VD = cast<VarDecl>(D);
3165
3166 QualType Type = VD->getType();
3167 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3168 // It will be analyzed later.
3169 Vars.push_back(DE);
3170 continue;
3171 }
3172
3173 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3174 // A list item that appears in a copyin clause must be threadprivate.
3175 if (!DSAStack->isThreadPrivate(VD)) {
3176 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003177 << getOpenMPClauseName(OMPC_copyin)
3178 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003179 continue;
3180 }
3181
3182 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3183 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003184 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003185 // operator for the class type.
3186 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003187 CXXRecordDecl *RD =
3188 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003189 // FIXME This code must be replaced by actual assignment of the
3190 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003191 if (RD) {
3192 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3193 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003194 if (MD) {
3195 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3196 MD->isDeleted()) {
3197 Diag(ELoc, diag::err_omp_required_method)
3198 << getOpenMPClauseName(OMPC_copyin) << 2;
3199 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3200 VarDecl::DeclarationOnly;
3201 Diag(VD->getLocation(),
3202 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3203 << VD;
3204 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3205 continue;
3206 }
3207 MarkFunctionReferenced(ELoc, MD);
3208 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003209 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003210 }
3211
3212 DSAStack->addDSA(VD, DE, OMPC_copyin);
3213 Vars.push_back(DE);
3214 }
3215
Alexey Bataeved09d242014-05-28 05:53:51 +00003216 if (Vars.empty())
3217 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003218
3219 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3220}
3221
Alexey Bataev758e55e2013-09-06 18:03:48 +00003222#undef DSAStack