1 //===--- OpenMPClause.cpp - Classes for OpenMP clauses --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/OpenMPClause.h"
16 #include "clang/AST/ASTContext.h"
18 using namespace clang;
20 OMPClause::child_range OMPClause::children() {
21 switch (getClauseKind()) {
24 #define OPENMP_CLAUSE(Name, Class) \
26 return static_cast<Class *>(this)->children();
27 #include "clang/Basic/OpenMPKinds.def"
29 llvm_unreachable("unknown OMPClause");
32 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
33 assert(VL.size() == varlist_size() &&
34 "Number of private copies is not the same as the preallocated buffer");
35 std::copy(VL.begin(), VL.end(), varlist_end());
39 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
40 SourceLocation LParenLoc, SourceLocation EndLoc,
41 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
42 // Allocate space for private variables and initializer expressions.
43 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
44 OMPPrivateClause *Clause =
45 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
46 Clause->setVarRefs(VL);
47 Clause->setPrivateCopies(PrivateVL);
51 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
53 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
54 return new (Mem) OMPPrivateClause(N);
57 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
58 assert(VL.size() == varlist_size() &&
59 "Number of private copies is not the same as the preallocated buffer");
60 std::copy(VL.begin(), VL.end(), varlist_end());
63 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
64 assert(VL.size() == varlist_size() &&
65 "Number of inits is not the same as the preallocated buffer");
66 std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
69 OMPFirstprivateClause *
70 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
71 SourceLocation LParenLoc, SourceLocation EndLoc,
72 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
73 ArrayRef<Expr *> InitVL) {
74 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
75 OMPFirstprivateClause *Clause =
76 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
77 Clause->setVarRefs(VL);
78 Clause->setPrivateCopies(PrivateVL);
79 Clause->setInits(InitVL);
83 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
85 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
86 return new (Mem) OMPFirstprivateClause(N);
89 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
90 assert(PrivateCopies.size() == varlist_size() &&
91 "Number of private copies is not the same as the preallocated buffer");
92 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
95 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
96 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
97 "not the same as the "
98 "preallocated buffer");
99 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
102 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
103 assert(DstExprs.size() == varlist_size() && "Number of destination "
104 "expressions is not the same as "
105 "the preallocated buffer");
106 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
109 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
110 assert(AssignmentOps.size() == varlist_size() &&
111 "Number of assignment expressions is not the same as the preallocated "
113 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
114 getDestinationExprs().end());
117 OMPLastprivateClause *OMPLastprivateClause::Create(
118 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
119 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
120 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
121 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
122 OMPLastprivateClause *Clause =
123 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
124 Clause->setVarRefs(VL);
125 Clause->setSourceExprs(SrcExprs);
126 Clause->setDestinationExprs(DstExprs);
127 Clause->setAssignmentOps(AssignmentOps);
131 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
133 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
134 return new (Mem) OMPLastprivateClause(N);
137 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
138 SourceLocation StartLoc,
139 SourceLocation LParenLoc,
140 SourceLocation EndLoc,
141 ArrayRef<Expr *> VL) {
142 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
143 OMPSharedClause *Clause =
144 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
145 Clause->setVarRefs(VL);
149 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
150 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
151 return new (Mem) OMPSharedClause(N);
154 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
155 assert(PL.size() == varlist_size() &&
156 "Number of privates is not the same as the preallocated buffer");
157 std::copy(PL.begin(), PL.end(), varlist_end());
160 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
161 assert(IL.size() == varlist_size() &&
162 "Number of inits is not the same as the preallocated buffer");
163 std::copy(IL.begin(), IL.end(), getPrivates().end());
166 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
167 assert(UL.size() == varlist_size() &&
168 "Number of updates is not the same as the preallocated buffer");
169 std::copy(UL.begin(), UL.end(), getInits().end());
172 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
173 assert(FL.size() == varlist_size() &&
174 "Number of final updates is not the same as the preallocated buffer");
175 std::copy(FL.begin(), FL.end(), getUpdates().end());
178 OMPLinearClause *OMPLinearClause::Create(
179 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
180 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
181 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
182 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep) {
183 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
184 // (Step and CalcStep).
185 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
186 OMPLinearClause *Clause = new (Mem) OMPLinearClause(
187 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
188 Clause->setVarRefs(VL);
189 Clause->setPrivates(PL);
190 Clause->setInits(IL);
191 // Fill update and final expressions with zeroes, they are provided later,
192 // after the directive construction.
193 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
195 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
197 Clause->setStep(Step);
198 Clause->setCalcStep(CalcStep);
202 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
204 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
205 // (Step and CalcStep).
206 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
207 return new (Mem) OMPLinearClause(NumVars);
211 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
212 SourceLocation LParenLoc, SourceLocation ColonLoc,
213 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
214 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
215 OMPAlignedClause *Clause = new (Mem)
216 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
217 Clause->setVarRefs(VL);
218 Clause->setAlignment(A);
222 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
224 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
225 return new (Mem) OMPAlignedClause(NumVars);
228 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
229 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
230 "not the same as the "
231 "preallocated buffer");
232 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
235 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
236 assert(DstExprs.size() == varlist_size() && "Number of destination "
237 "expressions is not the same as "
238 "the preallocated buffer");
239 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
242 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
243 assert(AssignmentOps.size() == varlist_size() &&
244 "Number of assignment expressions is not the same as the preallocated "
246 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
247 getDestinationExprs().end());
250 OMPCopyinClause *OMPCopyinClause::Create(
251 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
252 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
253 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
254 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
255 OMPCopyinClause *Clause =
256 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
257 Clause->setVarRefs(VL);
258 Clause->setSourceExprs(SrcExprs);
259 Clause->setDestinationExprs(DstExprs);
260 Clause->setAssignmentOps(AssignmentOps);
264 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
265 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
266 return new (Mem) OMPCopyinClause(N);
269 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
270 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
271 "not the same as the "
272 "preallocated buffer");
273 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
276 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
277 assert(DstExprs.size() == varlist_size() && "Number of destination "
278 "expressions is not the same as "
279 "the preallocated buffer");
280 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
283 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
284 assert(AssignmentOps.size() == varlist_size() &&
285 "Number of assignment expressions is not the same as the preallocated "
287 std::copy(AssignmentOps.begin(), AssignmentOps.end(),
288 getDestinationExprs().end());
291 OMPCopyprivateClause *OMPCopyprivateClause::Create(
292 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
293 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
294 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
295 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
296 OMPCopyprivateClause *Clause =
297 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
298 Clause->setVarRefs(VL);
299 Clause->setSourceExprs(SrcExprs);
300 Clause->setDestinationExprs(DstExprs);
301 Clause->setAssignmentOps(AssignmentOps);
305 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
307 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
308 return new (Mem) OMPCopyprivateClause(N);
311 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
312 assert(Privates.size() == varlist_size() &&
313 "Number of private copies is not the same as the preallocated buffer");
314 std::copy(Privates.begin(), Privates.end(), varlist_end());
317 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
319 LHSExprs.size() == varlist_size() &&
320 "Number of LHS expressions is not the same as the preallocated buffer");
321 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
324 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
326 RHSExprs.size() == varlist_size() &&
327 "Number of RHS expressions is not the same as the preallocated buffer");
328 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
331 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
332 assert(ReductionOps.size() == varlist_size() && "Number of reduction "
333 "expressions is not the same "
334 "as the preallocated buffer");
335 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
338 OMPReductionClause *OMPReductionClause::Create(
339 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
340 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
341 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
342 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
343 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps) {
344 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
345 OMPReductionClause *Clause = new (Mem) OMPReductionClause(
346 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
347 Clause->setVarRefs(VL);
348 Clause->setPrivates(Privates);
349 Clause->setLHSExprs(LHSExprs);
350 Clause->setRHSExprs(RHSExprs);
351 Clause->setReductionOps(ReductionOps);
355 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
357 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
358 return new (Mem) OMPReductionClause(N);
361 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
362 SourceLocation StartLoc,
363 SourceLocation LParenLoc,
364 SourceLocation EndLoc,
365 ArrayRef<Expr *> VL) {
366 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
367 OMPFlushClause *Clause =
368 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
369 Clause->setVarRefs(VL);
373 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
374 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
375 return new (Mem) OMPFlushClause(N);
379 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
380 SourceLocation LParenLoc, SourceLocation EndLoc,
381 OpenMPDependClauseKind DepKind, SourceLocation DepLoc,
382 SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
383 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
384 OMPDependClause *Clause =
385 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
386 Clause->setVarRefs(VL);
387 Clause->setDependencyKind(DepKind);
388 Clause->setDependencyLoc(DepLoc);
389 Clause->setColonLoc(ColonLoc);
393 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
394 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
395 return new (Mem) OMPDependClause(N);
398 OMPMapClause *OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
399 SourceLocation LParenLoc,
400 SourceLocation EndLoc, ArrayRef<Expr *> VL,
401 OpenMPMapClauseKind TypeModifier,
402 OpenMPMapClauseKind Type,
403 SourceLocation TypeLoc) {
404 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
405 OMPMapClause *Clause = new (Mem) OMPMapClause(
406 TypeModifier, Type, TypeLoc, StartLoc, LParenLoc, EndLoc, VL.size());
407 Clause->setVarRefs(VL);
408 Clause->setMapTypeModifier(TypeModifier);
409 Clause->setMapType(Type);
410 Clause->setMapLoc(TypeLoc);
414 OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned N) {
415 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
416 return new (Mem) OMPMapClause(N);