]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/AST/OpenMPClause.cpp
Vendor import of clang trunk r338150:
[FreeBSD/FreeBSD.git] / lib / AST / OpenMPClause.cpp
1 //===- OpenMPClause.cpp - Classes for OpenMP clauses ----------------------===//
2 //
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 //
10 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/AST/OpenMPClause.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include <algorithm>
22 #include <cassert>
23
24 using namespace clang;
25
26 OMPClause::child_range OMPClause::children() {
27   switch (getClauseKind()) {
28   default:
29     break;
30 #define OPENMP_CLAUSE(Name, Class)                                             \
31   case OMPC_##Name:                                                            \
32     return static_cast<Class *>(this)->children();
33 #include "clang/Basic/OpenMPKinds.def"
34   }
35   llvm_unreachable("unknown OMPClause");
36 }
37
38 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) {
39   auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C));
40   return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr;
41 }
42
43 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
44   switch (C->getClauseKind()) {
45   case OMPC_schedule:
46     return static_cast<const OMPScheduleClause *>(C);
47   case OMPC_dist_schedule:
48     return static_cast<const OMPDistScheduleClause *>(C);
49   case OMPC_firstprivate:
50     return static_cast<const OMPFirstprivateClause *>(C);
51   case OMPC_lastprivate:
52     return static_cast<const OMPLastprivateClause *>(C);
53   case OMPC_reduction:
54     return static_cast<const OMPReductionClause *>(C);
55   case OMPC_task_reduction:
56     return static_cast<const OMPTaskReductionClause *>(C);
57   case OMPC_in_reduction:
58     return static_cast<const OMPInReductionClause *>(C);
59   case OMPC_linear:
60     return static_cast<const OMPLinearClause *>(C);
61   case OMPC_if:
62     return static_cast<const OMPIfClause *>(C);
63   case OMPC_num_threads:
64     return static_cast<const OMPNumThreadsClause *>(C);
65   case OMPC_num_teams:
66     return static_cast<const OMPNumTeamsClause *>(C);
67   case OMPC_thread_limit:
68     return static_cast<const OMPThreadLimitClause *>(C);
69   case OMPC_device:
70     return static_cast<const OMPDeviceClause *>(C);
71   case OMPC_default:
72   case OMPC_proc_bind:
73   case OMPC_final:
74   case OMPC_safelen:
75   case OMPC_simdlen:
76   case OMPC_collapse:
77   case OMPC_private:
78   case OMPC_shared:
79   case OMPC_aligned:
80   case OMPC_copyin:
81   case OMPC_copyprivate:
82   case OMPC_ordered:
83   case OMPC_nowait:
84   case OMPC_untied:
85   case OMPC_mergeable:
86   case OMPC_threadprivate:
87   case OMPC_flush:
88   case OMPC_read:
89   case OMPC_write:
90   case OMPC_update:
91   case OMPC_capture:
92   case OMPC_seq_cst:
93   case OMPC_depend:
94   case OMPC_threads:
95   case OMPC_simd:
96   case OMPC_map:
97   case OMPC_priority:
98   case OMPC_grainsize:
99   case OMPC_nogroup:
100   case OMPC_num_tasks:
101   case OMPC_hint:
102   case OMPC_defaultmap:
103   case OMPC_unknown:
104   case OMPC_uniform:
105   case OMPC_to:
106   case OMPC_from:
107   case OMPC_use_device_ptr:
108   case OMPC_is_device_ptr:
109     break;
110   }
111
112   return nullptr;
113 }
114
115 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
116   auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
117   return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
118 }
119
120 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
121   switch (C->getClauseKind()) {
122   case OMPC_lastprivate:
123     return static_cast<const OMPLastprivateClause *>(C);
124   case OMPC_reduction:
125     return static_cast<const OMPReductionClause *>(C);
126   case OMPC_task_reduction:
127     return static_cast<const OMPTaskReductionClause *>(C);
128   case OMPC_in_reduction:
129     return static_cast<const OMPInReductionClause *>(C);
130   case OMPC_linear:
131     return static_cast<const OMPLinearClause *>(C);
132   case OMPC_schedule:
133   case OMPC_dist_schedule:
134   case OMPC_firstprivate:
135   case OMPC_default:
136   case OMPC_proc_bind:
137   case OMPC_if:
138   case OMPC_final:
139   case OMPC_num_threads:
140   case OMPC_safelen:
141   case OMPC_simdlen:
142   case OMPC_collapse:
143   case OMPC_private:
144   case OMPC_shared:
145   case OMPC_aligned:
146   case OMPC_copyin:
147   case OMPC_copyprivate:
148   case OMPC_ordered:
149   case OMPC_nowait:
150   case OMPC_untied:
151   case OMPC_mergeable:
152   case OMPC_threadprivate:
153   case OMPC_flush:
154   case OMPC_read:
155   case OMPC_write:
156   case OMPC_update:
157   case OMPC_capture:
158   case OMPC_seq_cst:
159   case OMPC_depend:
160   case OMPC_device:
161   case OMPC_threads:
162   case OMPC_simd:
163   case OMPC_map:
164   case OMPC_num_teams:
165   case OMPC_thread_limit:
166   case OMPC_priority:
167   case OMPC_grainsize:
168   case OMPC_nogroup:
169   case OMPC_num_tasks:
170   case OMPC_hint:
171   case OMPC_defaultmap:
172   case OMPC_unknown:
173   case OMPC_uniform:
174   case OMPC_to:
175   case OMPC_from:
176   case OMPC_use_device_ptr:
177   case OMPC_is_device_ptr:
178     break;
179   }
180
181   return nullptr;
182 }
183
184 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
185   assert(VL.size() == varlist_size() &&
186          "Number of private copies is not the same as the preallocated buffer");
187   std::copy(VL.begin(), VL.end(), varlist_end());
188 }
189
190 OMPPrivateClause *
191 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
192                          SourceLocation LParenLoc, SourceLocation EndLoc,
193                          ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
194   // Allocate space for private variables and initializer expressions.
195   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
196   OMPPrivateClause *Clause =
197       new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
198   Clause->setVarRefs(VL);
199   Clause->setPrivateCopies(PrivateVL);
200   return Clause;
201 }
202
203 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
204                                                 unsigned N) {
205   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
206   return new (Mem) OMPPrivateClause(N);
207 }
208
209 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
210   assert(VL.size() == varlist_size() &&
211          "Number of private copies is not the same as the preallocated buffer");
212   std::copy(VL.begin(), VL.end(), varlist_end());
213 }
214
215 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
216   assert(VL.size() == varlist_size() &&
217          "Number of inits is not the same as the preallocated buffer");
218   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
219 }
220
221 OMPFirstprivateClause *
222 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
223                               SourceLocation LParenLoc, SourceLocation EndLoc,
224                               ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
225                               ArrayRef<Expr *> InitVL, Stmt *PreInit) {
226   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
227   OMPFirstprivateClause *Clause =
228       new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
229   Clause->setVarRefs(VL);
230   Clause->setPrivateCopies(PrivateVL);
231   Clause->setInits(InitVL);
232   Clause->setPreInitStmt(PreInit);
233   return Clause;
234 }
235
236 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
237                                                           unsigned N) {
238   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
239   return new (Mem) OMPFirstprivateClause(N);
240 }
241
242 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
243   assert(PrivateCopies.size() == varlist_size() &&
244          "Number of private copies is not the same as the preallocated buffer");
245   std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
246 }
247
248 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
249   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
250                                               "not the same as the "
251                                               "preallocated buffer");
252   std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
253 }
254
255 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
256   assert(DstExprs.size() == varlist_size() && "Number of destination "
257                                               "expressions is not the same as "
258                                               "the preallocated buffer");
259   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
260 }
261
262 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
263   assert(AssignmentOps.size() == varlist_size() &&
264          "Number of assignment expressions is not the same as the preallocated "
265          "buffer");
266   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
267             getDestinationExprs().end());
268 }
269
270 OMPLastprivateClause *OMPLastprivateClause::Create(
271     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
272     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
273     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
274     Expr *PostUpdate) {
275   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
276   OMPLastprivateClause *Clause =
277       new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
278   Clause->setVarRefs(VL);
279   Clause->setSourceExprs(SrcExprs);
280   Clause->setDestinationExprs(DstExprs);
281   Clause->setAssignmentOps(AssignmentOps);
282   Clause->setPreInitStmt(PreInit);
283   Clause->setPostUpdateExpr(PostUpdate);
284   return Clause;
285 }
286
287 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
288                                                         unsigned N) {
289   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
290   return new (Mem) OMPLastprivateClause(N);
291 }
292
293 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
294                                          SourceLocation StartLoc,
295                                          SourceLocation LParenLoc,
296                                          SourceLocation EndLoc,
297                                          ArrayRef<Expr *> VL) {
298   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
299   OMPSharedClause *Clause =
300       new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
301   Clause->setVarRefs(VL);
302   return Clause;
303 }
304
305 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
306   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
307   return new (Mem) OMPSharedClause(N);
308 }
309
310 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
311   assert(PL.size() == varlist_size() &&
312          "Number of privates is not the same as the preallocated buffer");
313   std::copy(PL.begin(), PL.end(), varlist_end());
314 }
315
316 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
317   assert(IL.size() == varlist_size() &&
318          "Number of inits is not the same as the preallocated buffer");
319   std::copy(IL.begin(), IL.end(), getPrivates().end());
320 }
321
322 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
323   assert(UL.size() == varlist_size() &&
324          "Number of updates is not the same as the preallocated buffer");
325   std::copy(UL.begin(), UL.end(), getInits().end());
326 }
327
328 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
329   assert(FL.size() == varlist_size() &&
330          "Number of final updates is not the same as the preallocated buffer");
331   std::copy(FL.begin(), FL.end(), getUpdates().end());
332 }
333
334 OMPLinearClause *OMPLinearClause::Create(
335     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
336     OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
337     SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
338     ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
339     Stmt *PreInit, Expr *PostUpdate) {
340   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
341   // (Step and CalcStep).
342   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
343   OMPLinearClause *Clause = new (Mem) OMPLinearClause(
344       StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
345   Clause->setVarRefs(VL);
346   Clause->setPrivates(PL);
347   Clause->setInits(IL);
348   // Fill update and final expressions with zeroes, they are provided later,
349   // after the directive construction.
350   std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
351             nullptr);
352   std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
353             nullptr);
354   Clause->setStep(Step);
355   Clause->setCalcStep(CalcStep);
356   Clause->setPreInitStmt(PreInit);
357   Clause->setPostUpdateExpr(PostUpdate);
358   return Clause;
359 }
360
361 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
362                                               unsigned NumVars) {
363   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
364   // (Step and CalcStep).
365   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
366   return new (Mem) OMPLinearClause(NumVars);
367 }
368
369 OMPAlignedClause *
370 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
371                          SourceLocation LParenLoc, SourceLocation ColonLoc,
372                          SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
373   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
374   OMPAlignedClause *Clause = new (Mem)
375       OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
376   Clause->setVarRefs(VL);
377   Clause->setAlignment(A);
378   return Clause;
379 }
380
381 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
382                                                 unsigned NumVars) {
383   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
384   return new (Mem) OMPAlignedClause(NumVars);
385 }
386
387 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
388   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
389                                               "not the same as the "
390                                               "preallocated buffer");
391   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
392 }
393
394 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
395   assert(DstExprs.size() == varlist_size() && "Number of destination "
396                                               "expressions is not the same as "
397                                               "the preallocated buffer");
398   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
399 }
400
401 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
402   assert(AssignmentOps.size() == varlist_size() &&
403          "Number of assignment expressions is not the same as the preallocated "
404          "buffer");
405   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
406             getDestinationExprs().end());
407 }
408
409 OMPCopyinClause *OMPCopyinClause::Create(
410     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
411     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
412     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
413   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
414   OMPCopyinClause *Clause =
415       new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
416   Clause->setVarRefs(VL);
417   Clause->setSourceExprs(SrcExprs);
418   Clause->setDestinationExprs(DstExprs);
419   Clause->setAssignmentOps(AssignmentOps);
420   return Clause;
421 }
422
423 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
424   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
425   return new (Mem) OMPCopyinClause(N);
426 }
427
428 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
429   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
430                                               "not the same as the "
431                                               "preallocated buffer");
432   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
433 }
434
435 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
436   assert(DstExprs.size() == varlist_size() && "Number of destination "
437                                               "expressions is not the same as "
438                                               "the preallocated buffer");
439   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
440 }
441
442 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
443   assert(AssignmentOps.size() == varlist_size() &&
444          "Number of assignment expressions is not the same as the preallocated "
445          "buffer");
446   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
447             getDestinationExprs().end());
448 }
449
450 OMPCopyprivateClause *OMPCopyprivateClause::Create(
451     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
452     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
453     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
454   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
455   OMPCopyprivateClause *Clause =
456       new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
457   Clause->setVarRefs(VL);
458   Clause->setSourceExprs(SrcExprs);
459   Clause->setDestinationExprs(DstExprs);
460   Clause->setAssignmentOps(AssignmentOps);
461   return Clause;
462 }
463
464 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
465                                                         unsigned N) {
466   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
467   return new (Mem) OMPCopyprivateClause(N);
468 }
469
470 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
471   assert(Privates.size() == varlist_size() &&
472          "Number of private copies is not the same as the preallocated buffer");
473   std::copy(Privates.begin(), Privates.end(), varlist_end());
474 }
475
476 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
477   assert(
478       LHSExprs.size() == varlist_size() &&
479       "Number of LHS expressions is not the same as the preallocated buffer");
480   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
481 }
482
483 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
484   assert(
485       RHSExprs.size() == varlist_size() &&
486       "Number of RHS expressions is not the same as the preallocated buffer");
487   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
488 }
489
490 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
491   assert(ReductionOps.size() == varlist_size() && "Number of reduction "
492                                                   "expressions is not the same "
493                                                   "as the preallocated buffer");
494   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
495 }
496
497 OMPReductionClause *OMPReductionClause::Create(
498     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
499     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
500     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
501     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
502     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
503     Expr *PostUpdate) {
504   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
505   OMPReductionClause *Clause = new (Mem) OMPReductionClause(
506       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
507   Clause->setVarRefs(VL);
508   Clause->setPrivates(Privates);
509   Clause->setLHSExprs(LHSExprs);
510   Clause->setRHSExprs(RHSExprs);
511   Clause->setReductionOps(ReductionOps);
512   Clause->setPreInitStmt(PreInit);
513   Clause->setPostUpdateExpr(PostUpdate);
514   return Clause;
515 }
516
517 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
518                                                     unsigned N) {
519   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
520   return new (Mem) OMPReductionClause(N);
521 }
522
523 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
524   assert(Privates.size() == varlist_size() &&
525          "Number of private copies is not the same as the preallocated buffer");
526   std::copy(Privates.begin(), Privates.end(), varlist_end());
527 }
528
529 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
530   assert(
531       LHSExprs.size() == varlist_size() &&
532       "Number of LHS expressions is not the same as the preallocated buffer");
533   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
534 }
535
536 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
537   assert(
538       RHSExprs.size() == varlist_size() &&
539       "Number of RHS expressions is not the same as the preallocated buffer");
540   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
541 }
542
543 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
544   assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
545                                                   "expressions is not the same "
546                                                   "as the preallocated buffer");
547   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
548 }
549
550 OMPTaskReductionClause *OMPTaskReductionClause::Create(
551     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
552     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
553     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
554     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
555     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
556     Expr *PostUpdate) {
557   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
558   OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
559       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
560   Clause->setVarRefs(VL);
561   Clause->setPrivates(Privates);
562   Clause->setLHSExprs(LHSExprs);
563   Clause->setRHSExprs(RHSExprs);
564   Clause->setReductionOps(ReductionOps);
565   Clause->setPreInitStmt(PreInit);
566   Clause->setPostUpdateExpr(PostUpdate);
567   return Clause;
568 }
569
570 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
571                                                             unsigned N) {
572   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
573   return new (Mem) OMPTaskReductionClause(N);
574 }
575
576 void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
577   assert(Privates.size() == varlist_size() &&
578          "Number of private copies is not the same as the preallocated buffer");
579   std::copy(Privates.begin(), Privates.end(), varlist_end());
580 }
581
582 void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
583   assert(
584       LHSExprs.size() == varlist_size() &&
585       "Number of LHS expressions is not the same as the preallocated buffer");
586   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
587 }
588
589 void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
590   assert(
591       RHSExprs.size() == varlist_size() &&
592       "Number of RHS expressions is not the same as the preallocated buffer");
593   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
594 }
595
596 void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
597   assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
598                                                   "expressions is not the same "
599                                                   "as the preallocated buffer");
600   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
601 }
602
603 void OMPInReductionClause::setTaskgroupDescriptors(
604     ArrayRef<Expr *> TaskgroupDescriptors) {
605   assert(TaskgroupDescriptors.size() == varlist_size() &&
606          "Number of in reduction descriptors is not the same as the "
607          "preallocated buffer");
608   std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
609             getReductionOps().end());
610 }
611
612 OMPInReductionClause *OMPInReductionClause::Create(
613     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
614     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
615     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
616     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
617     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
618     ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
619   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
620   OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
621       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
622   Clause->setVarRefs(VL);
623   Clause->setPrivates(Privates);
624   Clause->setLHSExprs(LHSExprs);
625   Clause->setRHSExprs(RHSExprs);
626   Clause->setReductionOps(ReductionOps);
627   Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
628   Clause->setPreInitStmt(PreInit);
629   Clause->setPostUpdateExpr(PostUpdate);
630   return Clause;
631 }
632
633 OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
634                                                         unsigned N) {
635   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
636   return new (Mem) OMPInReductionClause(N);
637 }
638
639 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
640                                        SourceLocation StartLoc,
641                                        SourceLocation LParenLoc,
642                                        SourceLocation EndLoc,
643                                        ArrayRef<Expr *> VL) {
644   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
645   OMPFlushClause *Clause =
646       new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
647   Clause->setVarRefs(VL);
648   return Clause;
649 }
650
651 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
652   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
653   return new (Mem) OMPFlushClause(N);
654 }
655
656 OMPDependClause *OMPDependClause::Create(
657     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
658     SourceLocation EndLoc, OpenMPDependClauseKind DepKind,
659     SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) {
660   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
661   OMPDependClause *Clause =
662       new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size());
663   Clause->setVarRefs(VL);
664   Clause->setDependencyKind(DepKind);
665   Clause->setDependencyLoc(DepLoc);
666   Clause->setColonLoc(ColonLoc);
667   Clause->setCounterValue(nullptr);
668   return Clause;
669 }
670
671 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) {
672   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1));
673   return new (Mem) OMPDependClause(N);
674 }
675
676 void OMPDependClause::setCounterValue(Expr *V) {
677   assert(getDependencyKind() == OMPC_DEPEND_sink ||
678          getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
679   *getVarRefs().end() = V;
680 }
681
682 const Expr *OMPDependClause::getCounterValue() const {
683   auto *V = *getVarRefs().end();
684   assert(getDependencyKind() == OMPC_DEPEND_sink ||
685          getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
686   return V;
687 }
688
689 Expr *OMPDependClause::getCounterValue() {
690   auto *V = *getVarRefs().end();
691   assert(getDependencyKind() == OMPC_DEPEND_sink ||
692          getDependencyKind() == OMPC_DEPEND_source || V == nullptr);
693   return V;
694 }
695
696 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
697     MappableExprComponentListsRef ComponentLists) {
698   unsigned TotalNum = 0u;
699   for (auto &C : ComponentLists)
700     TotalNum += C.size();
701   return TotalNum;
702 }
703
704 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
705     ArrayRef<const ValueDecl *> Declarations) {
706   unsigned TotalNum = 0u;
707   llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
708   for (const ValueDecl *D : Declarations) {
709     const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
710     if (Cache.count(VD))
711       continue;
712     ++TotalNum;
713     Cache.insert(VD);
714   }
715   return TotalNum;
716 }
717
718 OMPMapClause *
719 OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc,
720                      SourceLocation LParenLoc, SourceLocation EndLoc,
721                      ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
722                      MappableExprComponentListsRef ComponentLists,
723                      OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type,
724                      bool TypeIsImplicit, SourceLocation TypeLoc) {
725   unsigned NumVars = Vars.size();
726   unsigned NumUniqueDeclarations =
727       getUniqueDeclarationsTotalNumber(Declarations);
728   unsigned NumComponentLists = ComponentLists.size();
729   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
730
731   // We need to allocate:
732   // NumVars x Expr* - we have an original list expression for each clause list
733   // entry.
734   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
735   // with each component list.
736   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
737   // number of lists for each unique declaration and the size of each component
738   // list.
739   // NumComponents x MappableComponent - the total of all the components in all
740   // the lists.
741   void *Mem = C.Allocate(
742       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
743                        OMPClauseMappableExprCommon::MappableComponent>(
744           NumVars, NumUniqueDeclarations,
745           NumUniqueDeclarations + NumComponentLists, NumComponents));
746   OMPMapClause *Clause = new (Mem) OMPMapClause(
747       TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc,
748       NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents);
749
750   Clause->setVarRefs(Vars);
751   Clause->setClauseInfo(Declarations, ComponentLists);
752   Clause->setMapTypeModifier(TypeModifier);
753   Clause->setMapType(Type);
754   Clause->setMapLoc(TypeLoc);
755   return Clause;
756 }
757
758 OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
759                                         unsigned NumUniqueDeclarations,
760                                         unsigned NumComponentLists,
761                                         unsigned NumComponents) {
762   void *Mem = C.Allocate(
763       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
764                        OMPClauseMappableExprCommon::MappableComponent>(
765           NumVars, NumUniqueDeclarations,
766           NumUniqueDeclarations + NumComponentLists, NumComponents));
767   return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations,
768                                 NumComponentLists, NumComponents);
769 }
770
771 OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc,
772                                  SourceLocation LParenLoc,
773                                  SourceLocation EndLoc, ArrayRef<Expr *> Vars,
774                                  ArrayRef<ValueDecl *> Declarations,
775                                  MappableExprComponentListsRef ComponentLists) {
776   unsigned NumVars = Vars.size();
777   unsigned NumUniqueDeclarations =
778       getUniqueDeclarationsTotalNumber(Declarations);
779   unsigned NumComponentLists = ComponentLists.size();
780   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
781
782   // We need to allocate:
783   // NumVars x Expr* - we have an original list expression for each clause list
784   // entry.
785   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
786   // with each component list.
787   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
788   // number of lists for each unique declaration and the size of each component
789   // list.
790   // NumComponents x MappableComponent - the total of all the components in all
791   // the lists.
792   void *Mem = C.Allocate(
793       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
794                        OMPClauseMappableExprCommon::MappableComponent>(
795           NumVars, NumUniqueDeclarations,
796           NumUniqueDeclarations + NumComponentLists, NumComponents));
797
798   OMPToClause *Clause = new (Mem)
799       OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
800                   NumComponentLists, NumComponents);
801
802   Clause->setVarRefs(Vars);
803   Clause->setClauseInfo(Declarations, ComponentLists);
804   return Clause;
805 }
806
807 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
808                                       unsigned NumUniqueDeclarations,
809                                       unsigned NumComponentLists,
810                                       unsigned NumComponents) {
811   void *Mem = C.Allocate(
812       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
813                        OMPClauseMappableExprCommon::MappableComponent>(
814           NumVars, NumUniqueDeclarations,
815           NumUniqueDeclarations + NumComponentLists, NumComponents));
816   return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations,
817                                NumComponentLists, NumComponents);
818 }
819
820 OMPFromClause *
821 OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc,
822                       SourceLocation LParenLoc, SourceLocation EndLoc,
823                       ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations,
824                       MappableExprComponentListsRef ComponentLists) {
825   unsigned NumVars = Vars.size();
826   unsigned NumUniqueDeclarations =
827       getUniqueDeclarationsTotalNumber(Declarations);
828   unsigned NumComponentLists = ComponentLists.size();
829   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
830
831   // We need to allocate:
832   // NumVars x Expr* - we have an original list expression for each clause list
833   // entry.
834   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
835   // with each component list.
836   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
837   // number of lists for each unique declaration and the size of each component
838   // list.
839   // NumComponents x MappableComponent - the total of all the components in all
840   // the lists.
841   void *Mem = C.Allocate(
842       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
843                        OMPClauseMappableExprCommon::MappableComponent>(
844           NumVars, NumUniqueDeclarations,
845           NumUniqueDeclarations + NumComponentLists, NumComponents));
846
847   OMPFromClause *Clause = new (Mem)
848       OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
849                     NumComponentLists, NumComponents);
850
851   Clause->setVarRefs(Vars);
852   Clause->setClauseInfo(Declarations, ComponentLists);
853   return Clause;
854 }
855
856 OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars,
857                                           unsigned NumUniqueDeclarations,
858                                           unsigned NumComponentLists,
859                                           unsigned NumComponents) {
860   void *Mem = C.Allocate(
861       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
862                        OMPClauseMappableExprCommon::MappableComponent>(
863           NumVars, NumUniqueDeclarations,
864           NumUniqueDeclarations + NumComponentLists, NumComponents));
865   return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations,
866                                  NumComponentLists, NumComponents);
867 }
868
869 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
870   assert(VL.size() == varlist_size() &&
871          "Number of private copies is not the same as the preallocated buffer");
872   std::copy(VL.begin(), VL.end(), varlist_end());
873 }
874
875 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
876   assert(VL.size() == varlist_size() &&
877          "Number of inits is not the same as the preallocated buffer");
878   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
879 }
880
881 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
882     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
883     SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars,
884     ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations,
885     MappableExprComponentListsRef ComponentLists) {
886   unsigned NumVars = Vars.size();
887   unsigned NumUniqueDeclarations =
888       getUniqueDeclarationsTotalNumber(Declarations);
889   unsigned NumComponentLists = ComponentLists.size();
890   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
891
892   // We need to allocate:
893   // 3 x NumVars x Expr* - we have an original list expression for each clause
894   // list entry and an equal number of private copies and inits.
895   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
896   // with each component list.
897   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
898   // number of lists for each unique declaration and the size of each component
899   // list.
900   // NumComponents x MappableComponent - the total of all the components in all
901   // the lists.
902   void *Mem = C.Allocate(
903       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
904                        OMPClauseMappableExprCommon::MappableComponent>(
905           3 * NumVars, NumUniqueDeclarations,
906           NumUniqueDeclarations + NumComponentLists, NumComponents));
907
908   OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(
909       StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
910       NumComponentLists, NumComponents);
911
912   Clause->setVarRefs(Vars);
913   Clause->setPrivateCopies(PrivateVars);
914   Clause->setInits(Inits);
915   Clause->setClauseInfo(Declarations, ComponentLists);
916   return Clause;
917 }
918
919 OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty(
920     const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
921     unsigned NumComponentLists, unsigned NumComponents) {
922   void *Mem = C.Allocate(
923       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
924                        OMPClauseMappableExprCommon::MappableComponent>(
925           3 * NumVars, NumUniqueDeclarations,
926           NumUniqueDeclarations + NumComponentLists, NumComponents));
927   return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations,
928                                          NumComponentLists, NumComponents);
929 }
930
931 OMPIsDevicePtrClause *
932 OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc,
933                              SourceLocation LParenLoc, SourceLocation EndLoc,
934                              ArrayRef<Expr *> Vars,
935                              ArrayRef<ValueDecl *> Declarations,
936                              MappableExprComponentListsRef ComponentLists) {
937   unsigned NumVars = Vars.size();
938   unsigned NumUniqueDeclarations =
939       getUniqueDeclarationsTotalNumber(Declarations);
940   unsigned NumComponentLists = ComponentLists.size();
941   unsigned NumComponents = getComponentsTotalNumber(ComponentLists);
942
943   // We need to allocate:
944   // NumVars x Expr* - we have an original list expression for each clause list
945   // entry.
946   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
947   // with each component list.
948   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
949   // number of lists for each unique declaration and the size of each component
950   // list.
951   // NumComponents x MappableComponent - the total of all the components in all
952   // the lists.
953   void *Mem = C.Allocate(
954       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
955                        OMPClauseMappableExprCommon::MappableComponent>(
956           NumVars, NumUniqueDeclarations,
957           NumUniqueDeclarations + NumComponentLists, NumComponents));
958
959   OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(
960       StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations,
961       NumComponentLists, NumComponents);
962
963   Clause->setVarRefs(Vars);
964   Clause->setClauseInfo(Declarations, ComponentLists);
965   return Clause;
966 }
967
968 OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty(
969     const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations,
970     unsigned NumComponentLists, unsigned NumComponents) {
971   void *Mem = C.Allocate(
972       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
973                        OMPClauseMappableExprCommon::MappableComponent>(
974           NumVars, NumUniqueDeclarations,
975           NumUniqueDeclarations + NumComponentLists, NumComponents));
976   return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations,
977                                         NumComponentLists, NumComponents);
978 }