blob: ba441a2b14e956ed3b17e7a092ad4e53fd73513c [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 Bataev1e0498a2014-06-26 08:21:58 +0000920 case OMPD_section: {
921 Sema::CapturedParamNameType Params[] = {
922 std::make_pair(StringRef(), QualType()) // __context with shared vars
923 };
924 ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
925 break;
926 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000927 case OMPD_threadprivate:
928 case OMPD_task:
929 llvm_unreachable("OpenMP Directive is not allowed");
930 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000931 llvm_unreachable("Unknown OpenMP directive");
932 }
933}
934
Alexey Bataev549210e2014-06-24 04:39:47 +0000935bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
936 OpenMPDirectiveKind CurrentRegion,
937 SourceLocation StartLoc) {
938 if (Stack->getCurScope()) {
939 auto ParentRegion = Stack->getParentDirective();
940 bool NestingProhibited = false;
941 bool CloseNesting = true;
942 bool ShouldBeInParallelRegion = false;
943 if (isOpenMPSimdDirective(ParentRegion)) {
944 // OpenMP [2.16, Nesting of Regions]
945 // OpenMP constructs may not be nested inside a simd region.
946 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
947 return true;
948 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000949 if (CurrentRegion == OMPD_section) {
950 // OpenMP [2.7.2, sections Construct, Restrictions]
951 // Orphaned section directives are prohibited. That is, the section
952 // directives must appear within the sections construct and must not be
953 // encountered elsewhere in the sections region.
954 if (ParentRegion != OMPD_sections) {
955 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
956 << (ParentRegion != OMPD_unknown)
957 << getOpenMPDirectiveName(ParentRegion);
958 return true;
959 }
960 return false;
961 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000962 if (isOpenMPWorksharingDirective(CurrentRegion) &&
963 !isOpenMPParallelDirective(CurrentRegion) &&
964 !isOpenMPSimdDirective(CurrentRegion)) {
965 // OpenMP [2.16, Nesting of Regions]
966 // A worksharing region may not be closely nested inside a worksharing,
967 // explicit task, critical, ordered, atomic, or master region.
968 // TODO
969 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) &&
970 !isOpenMPSimdDirective(ParentRegion);
971 ShouldBeInParallelRegion = true;
972 }
973 if (NestingProhibited) {
974 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
975 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << true
976 << getOpenMPDirectiveName(CurrentRegion) << ShouldBeInParallelRegion;
977 return true;
978 }
979 }
980 return false;
981}
982
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000983StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
984 ArrayRef<OMPClause *> Clauses,
985 Stmt *AStmt,
986 SourceLocation StartLoc,
987 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000988 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
989
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000990 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +0000991 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
992 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000993
994 // Check default data sharing attributes for referenced variables.
995 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
996 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
997 if (DSAChecker.isErrorFound())
998 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000999 // Generate list of implicitly defined firstprivate variables.
1000 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1001 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1002
1003 bool ErrorFound = false;
1004 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001005 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1006 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1007 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001008 ClausesWithImplicit.push_back(Implicit);
1009 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +00001010 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001011 } else
1012 ErrorFound = true;
1013 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001014
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001015 switch (Kind) {
1016 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001017 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1018 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001019 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001020 case OMPD_simd:
Alexey Bataeved09d242014-05-28 05:53:51 +00001021 Res =
1022 ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001023 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001024 case OMPD_for:
1025 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1026 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001027 case OMPD_sections:
1028 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1029 EndLoc);
1030 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001031 case OMPD_section:
1032 assert(ClausesWithImplicit.empty() &&
1033 "No clauses is allowed for 'omp section' directive");
1034 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1035 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001036 case OMPD_threadprivate:
1037 case OMPD_task:
1038 llvm_unreachable("OpenMP Directive is not allowed");
1039 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001040 llvm_unreachable("Unknown OpenMP directive");
1041 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001042
Alexey Bataeved09d242014-05-28 05:53:51 +00001043 if (ErrorFound)
1044 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001045 return Res;
1046}
1047
1048StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1049 Stmt *AStmt,
1050 SourceLocation StartLoc,
1051 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001052 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1053 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1054 // 1.2.2 OpenMP Language Terminology
1055 // Structured block - An executable statement with a single entry at the
1056 // top and a single exit at the bottom.
1057 // The point of exit cannot be a branch out of the structured block.
1058 // longjmp() and throw() must not violate the entry/exit criteria.
1059 CS->getCapturedDecl()->setNothrow();
1060
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001061 getCurFunction()->setHasBranchProtectedScope();
1062
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001063 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1064 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001065}
1066
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001067namespace {
1068/// \brief Helper class for checking canonical form of the OpenMP loops and
1069/// extracting iteration space of each loop in the loop nest, that will be used
1070/// for IR generation.
1071class OpenMPIterationSpaceChecker {
1072 /// \brief Reference to Sema.
1073 Sema &SemaRef;
1074 /// \brief A location for diagnostics (when there is no some better location).
1075 SourceLocation DefaultLoc;
1076 /// \brief A location for diagnostics (when increment is not compatible).
1077 SourceLocation ConditionLoc;
1078 /// \brief A source location for referring to condition later.
1079 SourceRange ConditionSrcRange;
1080 /// \brief Loop variable.
1081 VarDecl *Var;
1082 /// \brief Lower bound (initializer for the var).
1083 Expr *LB;
1084 /// \brief Upper bound.
1085 Expr *UB;
1086 /// \brief Loop step (increment).
1087 Expr *Step;
1088 /// \brief This flag is true when condition is one of:
1089 /// Var < UB
1090 /// Var <= UB
1091 /// UB > Var
1092 /// UB >= Var
1093 bool TestIsLessOp;
1094 /// \brief This flag is true when condition is strict ( < or > ).
1095 bool TestIsStrictOp;
1096 /// \brief This flag is true when step is subtracted on each iteration.
1097 bool SubtractStep;
1098
1099public:
1100 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1101 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1102 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1103 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1104 SubtractStep(false) {}
1105 /// \brief Check init-expr for canonical loop form and save loop counter
1106 /// variable - #Var and its initialization value - #LB.
1107 bool CheckInit(Stmt *S);
1108 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1109 /// for less/greater and for strict/non-strict comparison.
1110 bool CheckCond(Expr *S);
1111 /// \brief Check incr-expr for canonical loop form and return true if it
1112 /// does not conform, otherwise save loop step (#Step).
1113 bool CheckInc(Expr *S);
1114 /// \brief Return the loop counter variable.
1115 VarDecl *GetLoopVar() const { return Var; }
1116 /// \brief Return true if any expression is dependent.
1117 bool Dependent() const;
1118
1119private:
1120 /// \brief Check the right-hand side of an assignment in the increment
1121 /// expression.
1122 bool CheckIncRHS(Expr *RHS);
1123 /// \brief Helper to set loop counter variable and its initializer.
1124 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1125 /// \brief Helper to set upper bound.
1126 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1127 const SourceLocation &SL);
1128 /// \brief Helper to set loop increment.
1129 bool SetStep(Expr *NewStep, bool Subtract);
1130};
1131
1132bool OpenMPIterationSpaceChecker::Dependent() const {
1133 if (!Var) {
1134 assert(!LB && !UB && !Step);
1135 return false;
1136 }
1137 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1138 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1139}
1140
1141bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1142 // State consistency checking to ensure correct usage.
1143 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1144 !TestIsLessOp && !TestIsStrictOp);
1145 if (!NewVar || !NewLB)
1146 return true;
1147 Var = NewVar;
1148 LB = NewLB;
1149 return false;
1150}
1151
1152bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1153 const SourceRange &SR,
1154 const SourceLocation &SL) {
1155 // State consistency checking to ensure correct usage.
1156 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1157 !TestIsLessOp && !TestIsStrictOp);
1158 if (!NewUB)
1159 return true;
1160 UB = NewUB;
1161 TestIsLessOp = LessOp;
1162 TestIsStrictOp = StrictOp;
1163 ConditionSrcRange = SR;
1164 ConditionLoc = SL;
1165 return false;
1166}
1167
1168bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1169 // State consistency checking to ensure correct usage.
1170 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1171 if (!NewStep)
1172 return true;
1173 if (!NewStep->isValueDependent()) {
1174 // Check that the step is integer expression.
1175 SourceLocation StepLoc = NewStep->getLocStart();
1176 ExprResult Val =
1177 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1178 if (Val.isInvalid())
1179 return true;
1180 NewStep = Val.get();
1181
1182 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1183 // If test-expr is of form var relational-op b and relational-op is < or
1184 // <= then incr-expr must cause var to increase on each iteration of the
1185 // loop. If test-expr is of form var relational-op b and relational-op is
1186 // > or >= then incr-expr must cause var to decrease on each iteration of
1187 // the loop.
1188 // If test-expr is of form b relational-op var and relational-op is < or
1189 // <= then incr-expr must cause var to decrease on each iteration of the
1190 // loop. If test-expr is of form b relational-op var and relational-op is
1191 // > or >= then incr-expr must cause var to increase on each iteration of
1192 // the loop.
1193 llvm::APSInt Result;
1194 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1195 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1196 bool IsConstNeg =
1197 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1198 bool IsConstZero = IsConstant && !Result.getBoolValue();
1199 if (UB && (IsConstZero ||
1200 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1201 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1202 SemaRef.Diag(NewStep->getExprLoc(),
1203 diag::err_omp_loop_incr_not_compatible)
1204 << Var << TestIsLessOp << NewStep->getSourceRange();
1205 SemaRef.Diag(ConditionLoc,
1206 diag::note_omp_loop_cond_requres_compatible_incr)
1207 << TestIsLessOp << ConditionSrcRange;
1208 return true;
1209 }
1210 }
1211
1212 Step = NewStep;
1213 SubtractStep = Subtract;
1214 return false;
1215}
1216
1217bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1218 // Check init-expr for canonical loop form and save loop counter
1219 // variable - #Var and its initialization value - #LB.
1220 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1221 // var = lb
1222 // integer-type var = lb
1223 // random-access-iterator-type var = lb
1224 // pointer-type var = lb
1225 //
1226 if (!S) {
1227 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1228 return true;
1229 }
1230 if (Expr *E = dyn_cast<Expr>(S))
1231 S = E->IgnoreParens();
1232 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1233 if (BO->getOpcode() == BO_Assign)
1234 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1235 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1236 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1237 if (DS->isSingleDecl()) {
1238 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1239 if (Var->hasInit()) {
1240 // Accept non-canonical init form here but emit ext. warning.
1241 if (Var->getInitStyle() != VarDecl::CInit)
1242 SemaRef.Diag(S->getLocStart(),
1243 diag::ext_omp_loop_not_canonical_init)
1244 << S->getSourceRange();
1245 return SetVarAndLB(Var, Var->getInit());
1246 }
1247 }
1248 }
1249 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1250 if (CE->getOperator() == OO_Equal)
1251 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1252 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1253
1254 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1255 << S->getSourceRange();
1256 return true;
1257}
1258
Alexey Bataev23b69422014-06-18 07:08:49 +00001259/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001260/// variable (which may be the loop variable) if possible.
1261static const VarDecl *GetInitVarDecl(const Expr *E) {
1262 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001263 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001264 E = E->IgnoreParenImpCasts();
1265 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1266 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1267 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1268 CE->getArg(0) != nullptr)
1269 E = CE->getArg(0)->IgnoreParenImpCasts();
1270 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1271 if (!DRE)
1272 return nullptr;
1273 return dyn_cast<VarDecl>(DRE->getDecl());
1274}
1275
1276bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1277 // Check test-expr for canonical form, save upper-bound UB, flags for
1278 // less/greater and for strict/non-strict comparison.
1279 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1280 // var relational-op b
1281 // b relational-op var
1282 //
1283 if (!S) {
1284 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1285 return true;
1286 }
1287 S = S->IgnoreParenImpCasts();
1288 SourceLocation CondLoc = S->getLocStart();
1289 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1290 if (BO->isRelationalOp()) {
1291 if (GetInitVarDecl(BO->getLHS()) == Var)
1292 return SetUB(BO->getRHS(),
1293 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1294 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1295 BO->getSourceRange(), BO->getOperatorLoc());
1296 if (GetInitVarDecl(BO->getRHS()) == Var)
1297 return SetUB(BO->getLHS(),
1298 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1299 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1300 BO->getSourceRange(), BO->getOperatorLoc());
1301 }
1302 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1303 if (CE->getNumArgs() == 2) {
1304 auto Op = CE->getOperator();
1305 switch (Op) {
1306 case OO_Greater:
1307 case OO_GreaterEqual:
1308 case OO_Less:
1309 case OO_LessEqual:
1310 if (GetInitVarDecl(CE->getArg(0)) == Var)
1311 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1312 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1313 CE->getOperatorLoc());
1314 if (GetInitVarDecl(CE->getArg(1)) == Var)
1315 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1316 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1317 CE->getOperatorLoc());
1318 break;
1319 default:
1320 break;
1321 }
1322 }
1323 }
1324 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1325 << S->getSourceRange() << Var;
1326 return true;
1327}
1328
1329bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1330 // RHS of canonical loop form increment can be:
1331 // var + incr
1332 // incr + var
1333 // var - incr
1334 //
1335 RHS = RHS->IgnoreParenImpCasts();
1336 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1337 if (BO->isAdditiveOp()) {
1338 bool IsAdd = BO->getOpcode() == BO_Add;
1339 if (GetInitVarDecl(BO->getLHS()) == Var)
1340 return SetStep(BO->getRHS(), !IsAdd);
1341 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1342 return SetStep(BO->getLHS(), false);
1343 }
1344 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1345 bool IsAdd = CE->getOperator() == OO_Plus;
1346 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1347 if (GetInitVarDecl(CE->getArg(0)) == Var)
1348 return SetStep(CE->getArg(1), !IsAdd);
1349 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1350 return SetStep(CE->getArg(0), false);
1351 }
1352 }
1353 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1354 << RHS->getSourceRange() << Var;
1355 return true;
1356}
1357
1358bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1359 // Check incr-expr for canonical loop form and return true if it
1360 // does not conform.
1361 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1362 // ++var
1363 // var++
1364 // --var
1365 // var--
1366 // var += incr
1367 // var -= incr
1368 // var = var + incr
1369 // var = incr + var
1370 // var = var - incr
1371 //
1372 if (!S) {
1373 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1374 return true;
1375 }
1376 S = S->IgnoreParens();
1377 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1378 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1379 return SetStep(
1380 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1381 (UO->isDecrementOp() ? -1 : 1)).get(),
1382 false);
1383 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1384 switch (BO->getOpcode()) {
1385 case BO_AddAssign:
1386 case BO_SubAssign:
1387 if (GetInitVarDecl(BO->getLHS()) == Var)
1388 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1389 break;
1390 case BO_Assign:
1391 if (GetInitVarDecl(BO->getLHS()) == Var)
1392 return CheckIncRHS(BO->getRHS());
1393 break;
1394 default:
1395 break;
1396 }
1397 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1398 switch (CE->getOperator()) {
1399 case OO_PlusPlus:
1400 case OO_MinusMinus:
1401 if (GetInitVarDecl(CE->getArg(0)) == Var)
1402 return SetStep(
1403 SemaRef.ActOnIntegerConstant(
1404 CE->getLocStart(),
1405 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1406 false);
1407 break;
1408 case OO_PlusEqual:
1409 case OO_MinusEqual:
1410 if (GetInitVarDecl(CE->getArg(0)) == Var)
1411 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1412 break;
1413 case OO_Equal:
1414 if (GetInitVarDecl(CE->getArg(0)) == Var)
1415 return CheckIncRHS(CE->getArg(1));
1416 break;
1417 default:
1418 break;
1419 }
1420 }
1421 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1422 << S->getSourceRange() << Var;
1423 return true;
1424}
Alexey Bataev23b69422014-06-18 07:08:49 +00001425} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001426
1427/// \brief Called on a for stmt to check and extract its iteration space
1428/// for further processing (such as collapsing).
1429static bool CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind, Stmt *S,
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001430 Sema &SemaRef, DSAStackTy &DSA,
1431 unsigned CurrentNestedLoopCount,
1432 unsigned NestedLoopCount,
1433 Expr *NestedLoopCountExpr) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001434 // OpenMP [2.6, Canonical Loop Form]
1435 // for (init-expr; test-expr; incr-expr) structured-block
1436 auto For = dyn_cast_or_null<ForStmt>(S);
1437 if (!For) {
1438 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001439 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1440 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1441 << CurrentNestedLoopCount;
1442 if (NestedLoopCount > 1)
1443 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1444 diag::note_omp_collapse_expr)
1445 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001446 return true;
1447 }
1448 assert(For->getBody());
1449
1450 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1451
1452 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001453 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001454 if (ISC.CheckInit(Init)) {
1455 return true;
1456 }
1457
1458 bool HasErrors = false;
1459
1460 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001461 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001462
1463 // OpenMP [2.6, Canonical Loop Form]
1464 // Var is one of the following:
1465 // A variable of signed or unsigned integer type.
1466 // For C++, a variable of a random access iterator type.
1467 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001468 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001469 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1470 !VarType->isPointerType() &&
1471 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1472 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1473 << SemaRef.getLangOpts().CPlusPlus;
1474 HasErrors = true;
1475 }
1476
1477 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1478 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001479 // The loop iteration variable in the associated for-loop of a simd construct
1480 // with just one associated for-loop may be listed in a linear clause with a
1481 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001482 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1483 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001484 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001485 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
1486 DVar.CKind != OMPC_linear && DVar.CKind != OMPC_lastprivate) ||
1487 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1488 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001489 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001490 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
1491 << getOpenMPClauseName(DVar.CKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001492 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001493 HasErrors = true;
1494 } else {
1495 // Make the loop iteration variable private by default.
1496 DSA.addDSA(Var, nullptr, OMPC_private);
1497 }
1498
Alexey Bataev7ff55242014-06-19 09:13:45 +00001499 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001500
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001501 // Check test-expr.
1502 HasErrors |= ISC.CheckCond(For->getCond());
1503
1504 // Check incr-expr.
1505 HasErrors |= ISC.CheckInc(For->getInc());
1506
1507 if (ISC.Dependent())
1508 return HasErrors;
1509
1510 // FIXME: Build loop's iteration space representation.
1511 return HasErrors;
1512}
1513
1514/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1515/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1516/// to get the first for loop.
1517static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1518 if (IgnoreCaptured)
1519 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1520 S = CapS->getCapturedStmt();
1521 // OpenMP [2.8.1, simd construct, Restrictions]
1522 // All loops associated with the construct must be perfectly nested; that is,
1523 // there must be no intervening code nor any OpenMP directive between any two
1524 // loops.
1525 while (true) {
1526 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1527 S = AS->getSubStmt();
1528 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1529 if (CS->size() != 1)
1530 break;
1531 S = CS->body_back();
1532 } else
1533 break;
1534 }
1535 return S;
1536}
1537
1538/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001539/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1540/// number of collapsed loops otherwise.
1541static unsigned CheckOpenMPLoop(OpenMPDirectiveKind DKind,
1542 Expr *NestedLoopCountExpr, Stmt *AStmt,
1543 Sema &SemaRef, DSAStackTy &DSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001544 unsigned NestedLoopCount = 1;
1545 if (NestedLoopCountExpr) {
1546 // Found 'collapse' clause - calculate collapse number.
1547 llvm::APSInt Result;
1548 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1549 NestedLoopCount = Result.getLimitedValue();
1550 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001551 // This is helper routine for loop directives (e.g., 'for', 'simd',
1552 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001553 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1554 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001555 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
1556 NestedLoopCount, NestedLoopCountExpr))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001557 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001558 // Move on to the next nested for loop, or to the loop body.
1559 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1560 }
1561
1562 // FIXME: Build resulting iteration space for IR generation (collapsing
1563 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001564 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001565}
1566
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001567static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001568 auto CollapseFilter = [](const OMPClause *C) -> bool {
1569 return C->getClauseKind() == OMPC_collapse;
1570 };
1571 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1572 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001573 if (I)
1574 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1575 return nullptr;
1576}
1577
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001578StmtResult Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses,
Alexey Bataeved09d242014-05-28 05:53:51 +00001579 Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001580 SourceLocation EndLoc) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001581 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataevabfc0692014-06-25 06:52:00 +00001582 unsigned NestedLoopCount = CheckOpenMPLoop(
1583 OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack);
1584 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001585 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001586
1587 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001588 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1589 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001590}
1591
Alexey Bataevf29276e2014-06-18 04:14:57 +00001592StmtResult Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses,
1593 Stmt *AStmt, SourceLocation StartLoc,
1594 SourceLocation EndLoc) {
1595 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataevabfc0692014-06-25 06:52:00 +00001596 unsigned NestedLoopCount = CheckOpenMPLoop(
1597 OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this, *DSAStack);
1598 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001599 return StmtError();
1600
1601 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001602 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1603 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001604}
1605
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001606StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1607 Stmt *AStmt,
1608 SourceLocation StartLoc,
1609 SourceLocation EndLoc) {
1610 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1611 auto BaseStmt = AStmt;
1612 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1613 BaseStmt = CS->getCapturedStmt();
1614 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1615 auto S = C->children();
1616 if (!S)
1617 return StmtError();
1618 // All associated statements must be '#pragma omp section' except for
1619 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001620 for (++S; S; ++S) {
1621 auto SectionStmt = *S;
1622 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1623 if (SectionStmt)
1624 Diag(SectionStmt->getLocStart(),
1625 diag::err_omp_sections_substmt_not_section);
1626 return StmtError();
1627 }
1628 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001629 } else {
1630 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1631 return StmtError();
1632 }
1633
1634 getCurFunction()->setHasBranchProtectedScope();
1635
1636 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1637 AStmt);
1638}
1639
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001640StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1641 SourceLocation StartLoc,
1642 SourceLocation EndLoc) {
1643 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1644
1645 getCurFunction()->setHasBranchProtectedScope();
1646
1647 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1648}
1649
Alexey Bataeved09d242014-05-28 05:53:51 +00001650OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001651 SourceLocation StartLoc,
1652 SourceLocation LParenLoc,
1653 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001654 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001655 switch (Kind) {
1656 case OMPC_if:
1657 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1658 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001659 case OMPC_num_threads:
1660 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1661 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001662 case OMPC_safelen:
1663 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1664 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001665 case OMPC_collapse:
1666 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1667 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001668 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001669 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001670 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001671 case OMPC_private:
1672 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001673 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001674 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001675 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001676 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001677 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001678 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001679 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001680 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001681 case OMPC_threadprivate:
1682 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001683 llvm_unreachable("Clause is not allowed.");
1684 }
1685 return Res;
1686}
1687
Alexey Bataeved09d242014-05-28 05:53:51 +00001688OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001689 SourceLocation LParenLoc,
1690 SourceLocation EndLoc) {
1691 Expr *ValExpr = Condition;
1692 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1693 !Condition->isInstantiationDependent() &&
1694 !Condition->containsUnexpandedParameterPack()) {
1695 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001696 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001697 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001698 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001699
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001700 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001701 }
1702
1703 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1704}
1705
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001706ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1707 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001708 if (!Op)
1709 return ExprError();
1710
1711 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1712 public:
1713 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001714 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001715 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1716 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001717 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1718 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001719 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1720 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001721 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1722 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001723 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1724 QualType T,
1725 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001726 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1727 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001728 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1729 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001730 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001731 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001732 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001733 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1734 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001735 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1736 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001737 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1738 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001739 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001740 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001741 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001742 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1743 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001744 llvm_unreachable("conversion functions are permitted");
1745 }
1746 } ConvertDiagnoser;
1747 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1748}
1749
1750OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1751 SourceLocation StartLoc,
1752 SourceLocation LParenLoc,
1753 SourceLocation EndLoc) {
1754 Expr *ValExpr = NumThreads;
1755 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1756 !NumThreads->isInstantiationDependent() &&
1757 !NumThreads->containsUnexpandedParameterPack()) {
1758 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1759 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001760 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001761 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001762 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001763
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001764 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001765
1766 // OpenMP [2.5, Restrictions]
1767 // The num_threads expression must evaluate to a positive integer value.
1768 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001769 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1770 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001771 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1772 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001773 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001774 }
1775 }
1776
Alexey Bataeved09d242014-05-28 05:53:51 +00001777 return new (Context)
1778 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001779}
1780
Alexey Bataev62c87d22014-03-21 04:51:18 +00001781ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1782 OpenMPClauseKind CKind) {
1783 if (!E)
1784 return ExprError();
1785 if (E->isValueDependent() || E->isTypeDependent() ||
1786 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001787 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001788 llvm::APSInt Result;
1789 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1790 if (ICE.isInvalid())
1791 return ExprError();
1792 if (!Result.isStrictlyPositive()) {
1793 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1794 << getOpenMPClauseName(CKind) << E->getSourceRange();
1795 return ExprError();
1796 }
1797 return ICE;
1798}
1799
1800OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1801 SourceLocation LParenLoc,
1802 SourceLocation EndLoc) {
1803 // OpenMP [2.8.1, simd construct, Description]
1804 // The parameter of the safelen clause must be a constant
1805 // positive integer expression.
1806 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1807 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001808 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001809 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001810 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001811}
1812
Alexander Musman64d33f12014-06-04 07:53:32 +00001813OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
1814 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00001815 SourceLocation LParenLoc,
1816 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00001817 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001818 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00001819 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00001820 // The parameter of the collapse clause must be a constant
1821 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00001822 ExprResult NumForLoopsResult =
1823 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
1824 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00001825 return nullptr;
1826 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00001827 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00001828}
1829
Alexey Bataeved09d242014-05-28 05:53:51 +00001830OMPClause *Sema::ActOnOpenMPSimpleClause(
1831 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
1832 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001833 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001834 switch (Kind) {
1835 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001836 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00001837 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
1838 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001839 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001840 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00001841 Res = ActOnOpenMPProcBindClause(
1842 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
1843 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001844 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001845 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00001846 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00001847 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00001848 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001849 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001850 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001851 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001852 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00001853 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001854 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001855 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001856 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001857 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001858 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001859 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001860 case OMPC_threadprivate:
1861 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001862 llvm_unreachable("Clause is not allowed.");
1863 }
1864 return Res;
1865}
1866
1867OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
1868 SourceLocation KindKwLoc,
1869 SourceLocation StartLoc,
1870 SourceLocation LParenLoc,
1871 SourceLocation EndLoc) {
1872 if (Kind == OMPC_DEFAULT_unknown) {
1873 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001874 static_assert(OMPC_DEFAULT_unknown > 0,
1875 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00001876 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001877 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001878 Values += "'";
1879 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
1880 Values += "'";
1881 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001882 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001883 Values += " or ";
1884 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00001885 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001886 break;
1887 default:
1888 Values += Sep;
1889 break;
1890 }
1891 }
1892 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001893 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001894 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001895 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001896 switch (Kind) {
1897 case OMPC_DEFAULT_none:
1898 DSAStack->setDefaultDSANone();
1899 break;
1900 case OMPC_DEFAULT_shared:
1901 DSAStack->setDefaultDSAShared();
1902 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001903 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001904 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00001905 break;
1906 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001907 return new (Context)
1908 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001909}
1910
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001911OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
1912 SourceLocation KindKwLoc,
1913 SourceLocation StartLoc,
1914 SourceLocation LParenLoc,
1915 SourceLocation EndLoc) {
1916 if (Kind == OMPC_PROC_BIND_unknown) {
1917 std::string Values;
1918 std::string Sep(", ");
1919 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
1920 Values += "'";
1921 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
1922 Values += "'";
1923 switch (i) {
1924 case OMPC_PROC_BIND_unknown - 2:
1925 Values += " or ";
1926 break;
1927 case OMPC_PROC_BIND_unknown - 1:
1928 break;
1929 default:
1930 Values += Sep;
1931 break;
1932 }
1933 }
1934 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00001935 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001936 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001937 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001938 return new (Context)
1939 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001940}
1941
Alexey Bataev56dafe82014-06-20 07:16:17 +00001942OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
1943 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
1944 SourceLocation StartLoc, SourceLocation LParenLoc,
1945 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
1946 SourceLocation EndLoc) {
1947 OMPClause *Res = nullptr;
1948 switch (Kind) {
1949 case OMPC_schedule:
1950 Res = ActOnOpenMPScheduleClause(
1951 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
1952 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
1953 break;
1954 case OMPC_if:
1955 case OMPC_num_threads:
1956 case OMPC_safelen:
1957 case OMPC_collapse:
1958 case OMPC_default:
1959 case OMPC_proc_bind:
1960 case OMPC_private:
1961 case OMPC_firstprivate:
1962 case OMPC_lastprivate:
1963 case OMPC_shared:
1964 case OMPC_reduction:
1965 case OMPC_linear:
1966 case OMPC_aligned:
1967 case OMPC_copyin:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001968 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001969 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001970 case OMPC_threadprivate:
1971 case OMPC_unknown:
1972 llvm_unreachable("Clause is not allowed.");
1973 }
1974 return Res;
1975}
1976
1977OMPClause *Sema::ActOnOpenMPScheduleClause(
1978 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
1979 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
1980 SourceLocation EndLoc) {
1981 if (Kind == OMPC_SCHEDULE_unknown) {
1982 std::string Values;
1983 std::string Sep(", ");
1984 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
1985 Values += "'";
1986 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
1987 Values += "'";
1988 switch (i) {
1989 case OMPC_SCHEDULE_unknown - 2:
1990 Values += " or ";
1991 break;
1992 case OMPC_SCHEDULE_unknown - 1:
1993 break;
1994 default:
1995 Values += Sep;
1996 break;
1997 }
1998 }
1999 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2000 << Values << getOpenMPClauseName(OMPC_schedule);
2001 return nullptr;
2002 }
2003 Expr *ValExpr = ChunkSize;
2004 if (ChunkSize) {
2005 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2006 !ChunkSize->isInstantiationDependent() &&
2007 !ChunkSize->containsUnexpandedParameterPack()) {
2008 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2009 ExprResult Val =
2010 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2011 if (Val.isInvalid())
2012 return nullptr;
2013
2014 ValExpr = Val.get();
2015
2016 // OpenMP [2.7.1, Restrictions]
2017 // chunk_size must be a loop invariant integer expression with a positive
2018 // value.
2019 llvm::APSInt Result;
2020 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2021 Result.isSigned() && !Result.isStrictlyPositive()) {
2022 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2023 << "schedule" << ChunkSize->getSourceRange();
2024 return nullptr;
2025 }
2026 }
2027 }
2028
2029 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2030 EndLoc, Kind, ValExpr);
2031}
2032
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002033OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2034 SourceLocation StartLoc,
2035 SourceLocation EndLoc) {
2036 OMPClause *Res = nullptr;
2037 switch (Kind) {
2038 case OMPC_ordered:
2039 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2040 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002041 case OMPC_nowait:
2042 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2043 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002044 case OMPC_if:
2045 case OMPC_num_threads:
2046 case OMPC_safelen:
2047 case OMPC_collapse:
2048 case OMPC_schedule:
2049 case OMPC_private:
2050 case OMPC_firstprivate:
2051 case OMPC_lastprivate:
2052 case OMPC_shared:
2053 case OMPC_reduction:
2054 case OMPC_linear:
2055 case OMPC_aligned:
2056 case OMPC_copyin:
2057 case OMPC_default:
2058 case OMPC_proc_bind:
2059 case OMPC_threadprivate:
2060 case OMPC_unknown:
2061 llvm_unreachable("Clause is not allowed.");
2062 }
2063 return Res;
2064}
2065
2066OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2067 SourceLocation EndLoc) {
2068 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2069}
2070
Alexey Bataev236070f2014-06-20 11:19:47 +00002071OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2072 SourceLocation EndLoc) {
2073 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2074}
2075
Alexey Bataevc5e02582014-06-16 07:08:35 +00002076OMPClause *Sema::ActOnOpenMPVarListClause(
2077 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2078 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2079 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2080 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002081 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002082 switch (Kind) {
2083 case OMPC_private:
2084 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2085 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002086 case OMPC_firstprivate:
2087 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2088 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002089 case OMPC_lastprivate:
2090 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2091 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002092 case OMPC_shared:
2093 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2094 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002095 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002096 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2097 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002098 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002099 case OMPC_linear:
2100 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2101 ColonLoc, EndLoc);
2102 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002103 case OMPC_aligned:
2104 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2105 ColonLoc, EndLoc);
2106 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002107 case OMPC_copyin:
2108 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2109 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002110 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002111 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002112 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002113 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002114 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002115 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002116 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002117 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002118 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002119 case OMPC_threadprivate:
2120 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002121 llvm_unreachable("Clause is not allowed.");
2122 }
2123 return Res;
2124}
2125
2126OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2127 SourceLocation StartLoc,
2128 SourceLocation LParenLoc,
2129 SourceLocation EndLoc) {
2130 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002131 for (auto &RefExpr : VarList) {
2132 assert(RefExpr && "NULL expr in OpenMP private clause.");
2133 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002134 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002135 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002136 continue;
2137 }
2138
Alexey Bataeved09d242014-05-28 05:53:51 +00002139 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002140 // OpenMP [2.1, C/C++]
2141 // A list item is a variable name.
2142 // OpenMP [2.9.3.3, Restrictions, p.1]
2143 // A variable that is part of another variable (as an array or
2144 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002145 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002146 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002147 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002148 continue;
2149 }
2150 Decl *D = DE->getDecl();
2151 VarDecl *VD = cast<VarDecl>(D);
2152
2153 QualType Type = VD->getType();
2154 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2155 // It will be analyzed later.
2156 Vars.push_back(DE);
2157 continue;
2158 }
2159
2160 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2161 // A variable that appears in a private clause must not have an incomplete
2162 // type or a reference type.
2163 if (RequireCompleteType(ELoc, Type,
2164 diag::err_omp_private_incomplete_type)) {
2165 continue;
2166 }
2167 if (Type->isReferenceType()) {
2168 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002169 << getOpenMPClauseName(OMPC_private) << Type;
2170 bool IsDecl =
2171 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2172 Diag(VD->getLocation(),
2173 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2174 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002175 continue;
2176 }
2177
2178 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2179 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002180 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002181 // class type.
2182 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002183 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2184 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002185 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002186 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2187 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2188 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002189 // FIXME This code must be replaced by actual constructing/destructing of
2190 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002191 if (RD) {
2192 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2193 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002194 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002195 if (!CD ||
2196 CheckConstructorAccess(ELoc, CD,
2197 InitializedEntity::InitializeTemporary(Type),
2198 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002199 CD->isDeleted()) {
2200 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002201 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002202 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2203 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002204 Diag(VD->getLocation(),
2205 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2206 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002207 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2208 continue;
2209 }
2210 MarkFunctionReferenced(ELoc, CD);
2211 DiagnoseUseOfDecl(CD, ELoc);
2212
2213 CXXDestructorDecl *DD = RD->getDestructor();
2214 if (DD) {
2215 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2216 DD->isDeleted()) {
2217 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002218 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002219 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2220 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002221 Diag(VD->getLocation(),
2222 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2223 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002224 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2225 continue;
2226 }
2227 MarkFunctionReferenced(ELoc, DD);
2228 DiagnoseUseOfDecl(DD, ELoc);
2229 }
2230 }
2231
Alexey Bataev758e55e2013-09-06 18:03:48 +00002232 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2233 // in a Construct]
2234 // Variables with the predetermined data-sharing attributes may not be
2235 // listed in data-sharing attributes clauses, except for the cases
2236 // listed below. For these exceptions only, listing a predetermined
2237 // variable in a data-sharing attribute clause is allowed and overrides
2238 // the variable's predetermined data-sharing attributes.
2239 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2240 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002241 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2242 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002243 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002244 continue;
2245 }
2246
2247 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002248 Vars.push_back(DE);
2249 }
2250
Alexey Bataeved09d242014-05-28 05:53:51 +00002251 if (Vars.empty())
2252 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002253
2254 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2255}
2256
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002257OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2258 SourceLocation StartLoc,
2259 SourceLocation LParenLoc,
2260 SourceLocation EndLoc) {
2261 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002262 for (auto &RefExpr : VarList) {
2263 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2264 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002265 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002266 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002267 continue;
2268 }
2269
Alexey Bataeved09d242014-05-28 05:53:51 +00002270 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002271 // OpenMP [2.1, C/C++]
2272 // A list item is a variable name.
2273 // OpenMP [2.9.3.3, Restrictions, p.1]
2274 // A variable that is part of another variable (as an array or
2275 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002276 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002277 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002278 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002279 continue;
2280 }
2281 Decl *D = DE->getDecl();
2282 VarDecl *VD = cast<VarDecl>(D);
2283
2284 QualType Type = VD->getType();
2285 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2286 // It will be analyzed later.
2287 Vars.push_back(DE);
2288 continue;
2289 }
2290
2291 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2292 // A variable that appears in a private clause must not have an incomplete
2293 // type or a reference type.
2294 if (RequireCompleteType(ELoc, Type,
2295 diag::err_omp_firstprivate_incomplete_type)) {
2296 continue;
2297 }
2298 if (Type->isReferenceType()) {
2299 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002300 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2301 bool IsDecl =
2302 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2303 Diag(VD->getLocation(),
2304 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2305 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002306 continue;
2307 }
2308
2309 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2310 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002311 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002312 // class type.
2313 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002314 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2315 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2316 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002317 // FIXME This code must be replaced by actual constructing/destructing of
2318 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002319 if (RD) {
2320 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2321 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002322 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002323 if (!CD ||
2324 CheckConstructorAccess(ELoc, CD,
2325 InitializedEntity::InitializeTemporary(Type),
2326 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002327 CD->isDeleted()) {
2328 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002329 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002330 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2331 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002332 Diag(VD->getLocation(),
2333 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2334 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002335 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2336 continue;
2337 }
2338 MarkFunctionReferenced(ELoc, CD);
2339 DiagnoseUseOfDecl(CD, ELoc);
2340
2341 CXXDestructorDecl *DD = RD->getDestructor();
2342 if (DD) {
2343 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2344 DD->isDeleted()) {
2345 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002346 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002347 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2348 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002349 Diag(VD->getLocation(),
2350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2351 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002352 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2353 continue;
2354 }
2355 MarkFunctionReferenced(ELoc, DD);
2356 DiagnoseUseOfDecl(DD, ELoc);
2357 }
2358 }
2359
2360 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2361 // variable and it was checked already.
2362 if (StartLoc.isValid() && EndLoc.isValid()) {
2363 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2364 Type = Type.getNonReferenceType().getCanonicalType();
2365 bool IsConstant = Type.isConstant(Context);
2366 Type = Context.getBaseElementType(Type);
2367 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2368 // A list item that specifies a given variable may not appear in more
2369 // than one clause on the same directive, except that a variable may be
2370 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002371 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002372 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002373 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002374 << getOpenMPClauseName(DVar.CKind)
2375 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002376 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002377 continue;
2378 }
2379
2380 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2381 // in a Construct]
2382 // Variables with the predetermined data-sharing attributes may not be
2383 // listed in data-sharing attributes clauses, except for the cases
2384 // listed below. For these exceptions only, listing a predetermined
2385 // variable in a data-sharing attribute clause is allowed and overrides
2386 // the variable's predetermined data-sharing attributes.
2387 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2388 // in a Construct, C/C++, p.2]
2389 // Variables with const-qualified type having no mutable member may be
2390 // listed in a firstprivate clause, even if they are static data members.
2391 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2392 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2393 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002394 << getOpenMPClauseName(DVar.CKind)
2395 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002396 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002397 continue;
2398 }
2399
Alexey Bataevf29276e2014-06-18 04:14:57 +00002400 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002401 // OpenMP [2.9.3.4, Restrictions, p.2]
2402 // A list item that is private within a parallel region must not appear
2403 // in a firstprivate clause on a worksharing construct if any of the
2404 // worksharing regions arising from the worksharing construct ever bind
2405 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002406 if (isOpenMPWorksharingDirective(CurrDir) &&
2407 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002408 DVar = DSAStack->getImplicitDSA(VD);
2409 if (DVar.CKind != OMPC_shared) {
2410 Diag(ELoc, diag::err_omp_required_access)
2411 << getOpenMPClauseName(OMPC_firstprivate)
2412 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002413 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002414 continue;
2415 }
2416 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002417 // OpenMP [2.9.3.4, Restrictions, p.3]
2418 // A list item that appears in a reduction clause of a parallel construct
2419 // must not appear in a firstprivate clause on a worksharing or task
2420 // construct if any of the worksharing or task regions arising from the
2421 // worksharing or task construct ever bind to any of the parallel regions
2422 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002423 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002424 // OpenMP [2.9.3.4, Restrictions, p.4]
2425 // A list item that appears in a reduction clause in worksharing
2426 // construct must not appear in a firstprivate clause in a task construct
2427 // encountered during execution of any of the worksharing regions arising
2428 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002429 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002430 }
2431
2432 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2433 Vars.push_back(DE);
2434 }
2435
Alexey Bataeved09d242014-05-28 05:53:51 +00002436 if (Vars.empty())
2437 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002438
2439 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2440 Vars);
2441}
2442
Alexander Musman1bb328c2014-06-04 13:06:39 +00002443OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2444 SourceLocation StartLoc,
2445 SourceLocation LParenLoc,
2446 SourceLocation EndLoc) {
2447 SmallVector<Expr *, 8> Vars;
2448 for (auto &RefExpr : VarList) {
2449 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2450 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2451 // It will be analyzed later.
2452 Vars.push_back(RefExpr);
2453 continue;
2454 }
2455
2456 SourceLocation ELoc = RefExpr->getExprLoc();
2457 // OpenMP [2.1, C/C++]
2458 // A list item is a variable name.
2459 // OpenMP [2.14.3.5, Restrictions, p.1]
2460 // A variable that is part of another variable (as an array or structure
2461 // element) cannot appear in a lastprivate clause.
2462 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2463 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2464 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2465 continue;
2466 }
2467 Decl *D = DE->getDecl();
2468 VarDecl *VD = cast<VarDecl>(D);
2469
2470 QualType Type = VD->getType();
2471 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2472 // It will be analyzed later.
2473 Vars.push_back(DE);
2474 continue;
2475 }
2476
2477 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2478 // A variable that appears in a lastprivate clause must not have an
2479 // incomplete type or a reference type.
2480 if (RequireCompleteType(ELoc, Type,
2481 diag::err_omp_lastprivate_incomplete_type)) {
2482 continue;
2483 }
2484 if (Type->isReferenceType()) {
2485 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2486 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2487 bool IsDecl =
2488 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2489 Diag(VD->getLocation(),
2490 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2491 << VD;
2492 continue;
2493 }
2494
2495 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2496 // in a Construct]
2497 // Variables with the predetermined data-sharing attributes may not be
2498 // listed in data-sharing attributes clauses, except for the cases
2499 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002500 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2501 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2502 DVar.CKind != OMPC_firstprivate &&
2503 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2504 Diag(ELoc, diag::err_omp_wrong_dsa)
2505 << getOpenMPClauseName(DVar.CKind)
2506 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002507 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002508 continue;
2509 }
2510
Alexey Bataevf29276e2014-06-18 04:14:57 +00002511 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2512 // OpenMP [2.14.3.5, Restrictions, p.2]
2513 // A list item that is private within a parallel region, or that appears in
2514 // the reduction clause of a parallel construct, must not appear in a
2515 // lastprivate clause on a worksharing construct if any of the corresponding
2516 // worksharing regions ever binds to any of the corresponding parallel
2517 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002518 if (isOpenMPWorksharingDirective(CurrDir) &&
2519 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002520 DVar = DSAStack->getImplicitDSA(VD);
2521 if (DVar.CKind != OMPC_shared) {
2522 Diag(ELoc, diag::err_omp_required_access)
2523 << getOpenMPClauseName(OMPC_lastprivate)
2524 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002525 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002526 continue;
2527 }
2528 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002529 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002530 // A variable of class type (or array thereof) that appears in a
2531 // lastprivate clause requires an accessible, unambiguous default
2532 // constructor for the class type, unless the list item is also specified
2533 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002534 // A variable of class type (or array thereof) that appears in a
2535 // lastprivate clause requires an accessible, unambiguous copy assignment
2536 // operator for the class type.
2537 while (Type.getNonReferenceType()->isArrayType())
2538 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2539 ->getElementType();
2540 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2541 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2542 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002543 // FIXME This code must be replaced by actual copying and destructing of the
2544 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002545 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002546 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2547 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002548 if (MD) {
2549 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2550 MD->isDeleted()) {
2551 Diag(ELoc, diag::err_omp_required_method)
2552 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2553 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2554 VarDecl::DeclarationOnly;
2555 Diag(VD->getLocation(),
2556 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2557 << VD;
2558 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2559 continue;
2560 }
2561 MarkFunctionReferenced(ELoc, MD);
2562 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002563 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002564
2565 CXXDestructorDecl *DD = RD->getDestructor();
2566 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002567 PartialDiagnostic PD =
2568 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002569 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2570 DD->isDeleted()) {
2571 Diag(ELoc, diag::err_omp_required_method)
2572 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2573 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2574 VarDecl::DeclarationOnly;
2575 Diag(VD->getLocation(),
2576 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2577 << VD;
2578 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2579 continue;
2580 }
2581 MarkFunctionReferenced(ELoc, DD);
2582 DiagnoseUseOfDecl(DD, ELoc);
2583 }
2584 }
2585
Alexey Bataevf29276e2014-06-18 04:14:57 +00002586 if (DVar.CKind != OMPC_firstprivate)
2587 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002588 Vars.push_back(DE);
2589 }
2590
2591 if (Vars.empty())
2592 return nullptr;
2593
2594 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2595 Vars);
2596}
2597
Alexey Bataev758e55e2013-09-06 18:03:48 +00002598OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2599 SourceLocation StartLoc,
2600 SourceLocation LParenLoc,
2601 SourceLocation EndLoc) {
2602 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002603 for (auto &RefExpr : VarList) {
2604 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2605 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002606 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002607 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002608 continue;
2609 }
2610
Alexey Bataeved09d242014-05-28 05:53:51 +00002611 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002612 // OpenMP [2.1, C/C++]
2613 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002614 // OpenMP [2.14.3.2, Restrictions, p.1]
2615 // A variable that is part of another variable (as an array or structure
2616 // element) cannot appear in a shared unless it is a static data member
2617 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002618 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002619 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002620 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002621 continue;
2622 }
2623 Decl *D = DE->getDecl();
2624 VarDecl *VD = cast<VarDecl>(D);
2625
2626 QualType Type = VD->getType();
2627 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2628 // It will be analyzed later.
2629 Vars.push_back(DE);
2630 continue;
2631 }
2632
2633 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2634 // in a Construct]
2635 // Variables with the predetermined data-sharing attributes may not be
2636 // listed in data-sharing attributes clauses, except for the cases
2637 // listed below. For these exceptions only, listing a predetermined
2638 // variable in a data-sharing attribute clause is allowed and overrides
2639 // the variable's predetermined data-sharing attributes.
2640 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002641 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2642 DVar.RefExpr) {
2643 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2644 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002645 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002646 continue;
2647 }
2648
2649 DSAStack->addDSA(VD, DE, OMPC_shared);
2650 Vars.push_back(DE);
2651 }
2652
Alexey Bataeved09d242014-05-28 05:53:51 +00002653 if (Vars.empty())
2654 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002655
2656 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2657}
2658
Alexey Bataevc5e02582014-06-16 07:08:35 +00002659namespace {
2660class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2661 DSAStackTy *Stack;
2662
2663public:
2664 bool VisitDeclRefExpr(DeclRefExpr *E) {
2665 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2666 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2667 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2668 return false;
2669 if (DVar.CKind != OMPC_unknown)
2670 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002671 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002672 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002673 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002674 return true;
2675 return false;
2676 }
2677 return false;
2678 }
2679 bool VisitStmt(Stmt *S) {
2680 for (auto Child : S->children()) {
2681 if (Child && Visit(Child))
2682 return true;
2683 }
2684 return false;
2685 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002686 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002687};
Alexey Bataev23b69422014-06-18 07:08:49 +00002688} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002689
2690OMPClause *Sema::ActOnOpenMPReductionClause(
2691 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2692 SourceLocation ColonLoc, SourceLocation EndLoc,
2693 CXXScopeSpec &ReductionIdScopeSpec,
2694 const DeclarationNameInfo &ReductionId) {
2695 // TODO: Allow scope specification search when 'declare reduction' is
2696 // supported.
2697 assert(ReductionIdScopeSpec.isEmpty() &&
2698 "No support for scoped reduction identifiers yet.");
2699
2700 auto DN = ReductionId.getName();
2701 auto OOK = DN.getCXXOverloadedOperator();
2702 BinaryOperatorKind BOK = BO_Comma;
2703
2704 // OpenMP [2.14.3.6, reduction clause]
2705 // C
2706 // reduction-identifier is either an identifier or one of the following
2707 // operators: +, -, *, &, |, ^, && and ||
2708 // C++
2709 // reduction-identifier is either an id-expression or one of the following
2710 // operators: +, -, *, &, |, ^, && and ||
2711 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2712 switch (OOK) {
2713 case OO_Plus:
2714 case OO_Minus:
2715 BOK = BO_AddAssign;
2716 break;
2717 case OO_Star:
2718 BOK = BO_MulAssign;
2719 break;
2720 case OO_Amp:
2721 BOK = BO_AndAssign;
2722 break;
2723 case OO_Pipe:
2724 BOK = BO_OrAssign;
2725 break;
2726 case OO_Caret:
2727 BOK = BO_XorAssign;
2728 break;
2729 case OO_AmpAmp:
2730 BOK = BO_LAnd;
2731 break;
2732 case OO_PipePipe:
2733 BOK = BO_LOr;
2734 break;
2735 default:
2736 if (auto II = DN.getAsIdentifierInfo()) {
2737 if (II->isStr("max"))
2738 BOK = BO_GT;
2739 else if (II->isStr("min"))
2740 BOK = BO_LT;
2741 }
2742 break;
2743 }
2744 SourceRange ReductionIdRange;
2745 if (ReductionIdScopeSpec.isValid()) {
2746 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2747 }
2748 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2749 if (BOK == BO_Comma) {
2750 // Not allowed reduction identifier is found.
2751 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2752 << ReductionIdRange;
2753 return nullptr;
2754 }
2755
2756 SmallVector<Expr *, 8> Vars;
2757 for (auto RefExpr : VarList) {
2758 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2759 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2760 // It will be analyzed later.
2761 Vars.push_back(RefExpr);
2762 continue;
2763 }
2764
2765 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2766 RefExpr->isInstantiationDependent() ||
2767 RefExpr->containsUnexpandedParameterPack()) {
2768 // It will be analyzed later.
2769 Vars.push_back(RefExpr);
2770 continue;
2771 }
2772
2773 auto ELoc = RefExpr->getExprLoc();
2774 auto ERange = RefExpr->getSourceRange();
2775 // OpenMP [2.1, C/C++]
2776 // A list item is a variable or array section, subject to the restrictions
2777 // specified in Section 2.4 on page 42 and in each of the sections
2778 // describing clauses and directives for which a list appears.
2779 // OpenMP [2.14.3.3, Restrictions, p.1]
2780 // A variable that is part of another variable (as an array or
2781 // structure element) cannot appear in a private clause.
2782 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2783 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2784 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2785 continue;
2786 }
2787 auto D = DE->getDecl();
2788 auto VD = cast<VarDecl>(D);
2789 auto Type = VD->getType();
2790 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2791 // A variable that appears in a private clause must not have an incomplete
2792 // type or a reference type.
2793 if (RequireCompleteType(ELoc, Type,
2794 diag::err_omp_reduction_incomplete_type))
2795 continue;
2796 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2797 // Arrays may not appear in a reduction clause.
2798 if (Type.getNonReferenceType()->isArrayType()) {
2799 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2800 bool IsDecl =
2801 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2802 Diag(VD->getLocation(),
2803 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2804 << VD;
2805 continue;
2806 }
2807 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2808 // A list item that appears in a reduction clause must not be
2809 // const-qualified.
2810 if (Type.getNonReferenceType().isConstant(Context)) {
2811 Diag(ELoc, diag::err_omp_const_variable)
2812 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
2813 bool IsDecl =
2814 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2815 Diag(VD->getLocation(),
2816 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2817 << VD;
2818 continue;
2819 }
2820 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
2821 // If a list-item is a reference type then it must bind to the same object
2822 // for all threads of the team.
2823 VarDecl *VDDef = VD->getDefinition();
2824 if (Type->isReferenceType() && VDDef) {
2825 DSARefChecker Check(DSAStack);
2826 if (Check.Visit(VDDef->getInit())) {
2827 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
2828 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
2829 continue;
2830 }
2831 }
2832 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2833 // The type of a list item that appears in a reduction clause must be valid
2834 // for the reduction-identifier. For a max or min reduction in C, the type
2835 // of the list item must be an allowed arithmetic data type: char, int,
2836 // float, double, or _Bool, possibly modified with long, short, signed, or
2837 // unsigned. For a max or min reduction in C++, the type of the list item
2838 // must be an allowed arithmetic data type: char, wchar_t, int, float,
2839 // double, or bool, possibly modified with long, short, signed, or unsigned.
2840 if ((BOK == BO_GT || BOK == BO_LT) &&
2841 !(Type->isScalarType() ||
2842 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
2843 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
2844 << getLangOpts().CPlusPlus;
2845 bool IsDecl =
2846 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2847 Diag(VD->getLocation(),
2848 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2849 << VD;
2850 continue;
2851 }
2852 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
2853 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
2854 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
2855 bool IsDecl =
2856 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2857 Diag(VD->getLocation(),
2858 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2859 << VD;
2860 continue;
2861 }
2862 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
2863 getDiagnostics().setSuppressAllDiagnostics(true);
2864 ExprResult ReductionOp =
2865 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
2866 RefExpr, RefExpr);
2867 getDiagnostics().setSuppressAllDiagnostics(Suppress);
2868 if (ReductionOp.isInvalid()) {
2869 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00002870 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002871 bool IsDecl =
2872 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2873 Diag(VD->getLocation(),
2874 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2875 << VD;
2876 continue;
2877 }
2878
2879 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2880 // in a Construct]
2881 // Variables with the predetermined data-sharing attributes may not be
2882 // listed in data-sharing attributes clauses, except for the cases
2883 // listed below. For these exceptions only, listing a predetermined
2884 // variable in a data-sharing attribute clause is allowed and overrides
2885 // the variable's predetermined data-sharing attributes.
2886 // OpenMP [2.14.3.6, Restrictions, p.3]
2887 // Any number of reduction clauses can be specified on the directive,
2888 // but a list item can appear only once in the reduction clauses for that
2889 // directive.
2890 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2891 if (DVar.CKind == OMPC_reduction) {
2892 Diag(ELoc, diag::err_omp_once_referenced)
2893 << getOpenMPClauseName(OMPC_reduction);
2894 if (DVar.RefExpr) {
2895 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
2896 }
2897 } else if (DVar.CKind != OMPC_unknown) {
2898 Diag(ELoc, diag::err_omp_wrong_dsa)
2899 << getOpenMPClauseName(DVar.CKind)
2900 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002901 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002902 continue;
2903 }
2904
2905 // OpenMP [2.14.3.6, Restrictions, p.1]
2906 // A list item that appears in a reduction clause of a worksharing
2907 // construct must be shared in the parallel regions to which any of the
2908 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002909 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00002910 if (isOpenMPWorksharingDirective(CurrDir) &&
2911 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002912 DVar = DSAStack->getImplicitDSA(VD);
2913 if (DVar.CKind != OMPC_shared) {
2914 Diag(ELoc, diag::err_omp_required_access)
2915 << getOpenMPClauseName(OMPC_reduction)
2916 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002917 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002918 continue;
2919 }
2920 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00002921
2922 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2923 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2924 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002925 // FIXME This code must be replaced by actual constructing/destructing of
2926 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00002927 if (RD) {
2928 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2929 PartialDiagnostic PD =
2930 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00002931 if (!CD ||
2932 CheckConstructorAccess(ELoc, CD,
2933 InitializedEntity::InitializeTemporary(Type),
2934 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00002935 CD->isDeleted()) {
2936 Diag(ELoc, diag::err_omp_required_method)
2937 << getOpenMPClauseName(OMPC_reduction) << 0;
2938 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2939 VarDecl::DeclarationOnly;
2940 Diag(VD->getLocation(),
2941 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2942 << VD;
2943 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2944 continue;
2945 }
2946 MarkFunctionReferenced(ELoc, CD);
2947 DiagnoseUseOfDecl(CD, ELoc);
2948
2949 CXXDestructorDecl *DD = RD->getDestructor();
2950 if (DD) {
2951 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2952 DD->isDeleted()) {
2953 Diag(ELoc, diag::err_omp_required_method)
2954 << getOpenMPClauseName(OMPC_reduction) << 4;
2955 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2956 VarDecl::DeclarationOnly;
2957 Diag(VD->getLocation(),
2958 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2959 << VD;
2960 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2961 continue;
2962 }
2963 MarkFunctionReferenced(ELoc, DD);
2964 DiagnoseUseOfDecl(DD, ELoc);
2965 }
2966 }
2967
2968 DSAStack->addDSA(VD, DE, OMPC_reduction);
2969 Vars.push_back(DE);
2970 }
2971
2972 if (Vars.empty())
2973 return nullptr;
2974
2975 return OMPReductionClause::Create(
2976 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
2977 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
2978}
2979
Alexander Musman8dba6642014-04-22 13:09:42 +00002980OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
2981 SourceLocation StartLoc,
2982 SourceLocation LParenLoc,
2983 SourceLocation ColonLoc,
2984 SourceLocation EndLoc) {
2985 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002986 for (auto &RefExpr : VarList) {
2987 assert(RefExpr && "NULL expr in OpenMP linear clause.");
2988 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00002989 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002990 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00002991 continue;
2992 }
2993
2994 // OpenMP [2.14.3.7, linear clause]
2995 // A list item that appears in a linear clause is subject to the private
2996 // clause semantics described in Section 2.14.3.3 on page 159 except as
2997 // noted. In addition, the value of the new list item on each iteration
2998 // of the associated loop(s) corresponds to the value of the original
2999 // list item before entering the construct plus the logical number of
3000 // the iteration times linear-step.
3001
Alexey Bataeved09d242014-05-28 05:53:51 +00003002 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003003 // OpenMP [2.1, C/C++]
3004 // A list item is a variable name.
3005 // OpenMP [2.14.3.3, Restrictions, p.1]
3006 // A variable that is part of another variable (as an array or
3007 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003008 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003009 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003010 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003011 continue;
3012 }
3013
3014 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3015
3016 // OpenMP [2.14.3.7, linear clause]
3017 // A list-item cannot appear in more than one linear clause.
3018 // A list-item that appears in a linear clause cannot appear in any
3019 // other data-sharing attribute clause.
3020 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
3021 if (DVar.RefExpr) {
3022 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3023 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003024 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003025 continue;
3026 }
3027
3028 QualType QType = VD->getType();
3029 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3030 // It will be analyzed later.
3031 Vars.push_back(DE);
3032 continue;
3033 }
3034
3035 // A variable must not have an incomplete type or a reference type.
3036 if (RequireCompleteType(ELoc, QType,
3037 diag::err_omp_linear_incomplete_type)) {
3038 continue;
3039 }
3040 if (QType->isReferenceType()) {
3041 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3042 << getOpenMPClauseName(OMPC_linear) << QType;
3043 bool IsDecl =
3044 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3045 Diag(VD->getLocation(),
3046 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3047 << VD;
3048 continue;
3049 }
3050
3051 // A list item must not be const-qualified.
3052 if (QType.isConstant(Context)) {
3053 Diag(ELoc, diag::err_omp_const_variable)
3054 << getOpenMPClauseName(OMPC_linear);
3055 bool IsDecl =
3056 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3057 Diag(VD->getLocation(),
3058 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3059 << VD;
3060 continue;
3061 }
3062
3063 // A list item must be of integral or pointer type.
3064 QType = QType.getUnqualifiedType().getCanonicalType();
3065 const Type *Ty = QType.getTypePtrOrNull();
3066 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3067 !Ty->isPointerType())) {
3068 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3069 bool IsDecl =
3070 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3071 Diag(VD->getLocation(),
3072 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3073 << VD;
3074 continue;
3075 }
3076
3077 DSAStack->addDSA(VD, DE, OMPC_linear);
3078 Vars.push_back(DE);
3079 }
3080
3081 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003082 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003083
3084 Expr *StepExpr = Step;
3085 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3086 !Step->isInstantiationDependent() &&
3087 !Step->containsUnexpandedParameterPack()) {
3088 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003089 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003090 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003091 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003092 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003093
3094 // Warn about zero linear step (it would be probably better specified as
3095 // making corresponding variables 'const').
3096 llvm::APSInt Result;
3097 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3098 !Result.isNegative() && !Result.isStrictlyPositive())
3099 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3100 << (Vars.size() > 1);
3101 }
3102
3103 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3104 Vars, StepExpr);
3105}
3106
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003107OMPClause *Sema::ActOnOpenMPAlignedClause(
3108 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3109 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3110
3111 SmallVector<Expr *, 8> Vars;
3112 for (auto &RefExpr : VarList) {
3113 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3114 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3115 // It will be analyzed later.
3116 Vars.push_back(RefExpr);
3117 continue;
3118 }
3119
3120 SourceLocation ELoc = RefExpr->getExprLoc();
3121 // OpenMP [2.1, C/C++]
3122 // A list item is a variable name.
3123 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3124 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3125 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3126 continue;
3127 }
3128
3129 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3130
3131 // OpenMP [2.8.1, simd construct, Restrictions]
3132 // The type of list items appearing in the aligned clause must be
3133 // array, pointer, reference to array, or reference to pointer.
3134 QualType QType = DE->getType()
3135 .getNonReferenceType()
3136 .getUnqualifiedType()
3137 .getCanonicalType();
3138 const Type *Ty = QType.getTypePtrOrNull();
3139 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3140 !Ty->isPointerType())) {
3141 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3142 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3143 bool IsDecl =
3144 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3145 Diag(VD->getLocation(),
3146 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3147 << VD;
3148 continue;
3149 }
3150
3151 // OpenMP [2.8.1, simd construct, Restrictions]
3152 // A list-item cannot appear in more than one aligned clause.
3153 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3154 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3155 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3156 << getOpenMPClauseName(OMPC_aligned);
3157 continue;
3158 }
3159
3160 Vars.push_back(DE);
3161 }
3162
3163 // OpenMP [2.8.1, simd construct, Description]
3164 // The parameter of the aligned clause, alignment, must be a constant
3165 // positive integer expression.
3166 // If no optional parameter is specified, implementation-defined default
3167 // alignments for SIMD instructions on the target platforms are assumed.
3168 if (Alignment != nullptr) {
3169 ExprResult AlignResult =
3170 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3171 if (AlignResult.isInvalid())
3172 return nullptr;
3173 Alignment = AlignResult.get();
3174 }
3175 if (Vars.empty())
3176 return nullptr;
3177
3178 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3179 EndLoc, Vars, Alignment);
3180}
3181
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003182OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3183 SourceLocation StartLoc,
3184 SourceLocation LParenLoc,
3185 SourceLocation EndLoc) {
3186 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003187 for (auto &RefExpr : VarList) {
3188 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3189 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003190 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003191 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003192 continue;
3193 }
3194
Alexey Bataeved09d242014-05-28 05:53:51 +00003195 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003196 // OpenMP [2.1, C/C++]
3197 // A list item is a variable name.
3198 // OpenMP [2.14.4.1, Restrictions, p.1]
3199 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003200 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003201 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003202 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003203 continue;
3204 }
3205
3206 Decl *D = DE->getDecl();
3207 VarDecl *VD = cast<VarDecl>(D);
3208
3209 QualType Type = VD->getType();
3210 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3211 // It will be analyzed later.
3212 Vars.push_back(DE);
3213 continue;
3214 }
3215
3216 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3217 // A list item that appears in a copyin clause must be threadprivate.
3218 if (!DSAStack->isThreadPrivate(VD)) {
3219 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003220 << getOpenMPClauseName(OMPC_copyin)
3221 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003222 continue;
3223 }
3224
3225 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3226 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003227 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003228 // operator for the class type.
3229 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003230 CXXRecordDecl *RD =
3231 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003232 // FIXME This code must be replaced by actual assignment of the
3233 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003234 if (RD) {
3235 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3236 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003237 if (MD) {
3238 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3239 MD->isDeleted()) {
3240 Diag(ELoc, diag::err_omp_required_method)
3241 << getOpenMPClauseName(OMPC_copyin) << 2;
3242 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3243 VarDecl::DeclarationOnly;
3244 Diag(VD->getLocation(),
3245 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3246 << VD;
3247 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3248 continue;
3249 }
3250 MarkFunctionReferenced(ELoc, MD);
3251 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003252 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003253 }
3254
3255 DSAStack->addDSA(VD, DE, OMPC_copyin);
3256 Vars.push_back(DE);
3257 }
3258
Alexey Bataeved09d242014-05-28 05:53:51 +00003259 if (Vars.empty())
3260 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003261
3262 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3263}
3264
Alexey Bataev758e55e2013-09-06 18:03:48 +00003265#undef DSAStack