1 //===- OpenMPClause.cpp - Classes for OpenMP clauses ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the subclesses of Stmt class declared in OpenMPClause.h
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/OpenMPClause.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclOpenMP.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_allocator:
77   case OMPC_collapse:
78   case OMPC_private:
79   case OMPC_shared:
80   case OMPC_aligned:
81   case OMPC_copyin:
82   case OMPC_copyprivate:
83   case OMPC_ordered:
84   case OMPC_nowait:
85   case OMPC_untied:
86   case OMPC_mergeable:
87   case OMPC_threadprivate:
88   case OMPC_allocate:
89   case OMPC_flush:
90   case OMPC_read:
91   case OMPC_write:
92   case OMPC_update:
93   case OMPC_capture:
94   case OMPC_seq_cst:
95   case OMPC_depend:
96   case OMPC_threads:
97   case OMPC_simd:
98   case OMPC_map:
99   case OMPC_priority:
100   case OMPC_grainsize:
101   case OMPC_nogroup:
102   case OMPC_num_tasks:
103   case OMPC_hint:
104   case OMPC_defaultmap:
105   case OMPC_unknown:
106   case OMPC_uniform:
107   case OMPC_to:
108   case OMPC_from:
109   case OMPC_use_device_ptr:
110   case OMPC_is_device_ptr:
111   case OMPC_unified_address:
112   case OMPC_unified_shared_memory:
113   case OMPC_reverse_offload:
114   case OMPC_dynamic_allocators:
115   case OMPC_atomic_default_mem_order:
116     break;
117   }
118 
119   return nullptr;
120 }
121 
122 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) {
123   auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C));
124   return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr;
125 }
126 
127 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) {
128   switch (C->getClauseKind()) {
129   case OMPC_lastprivate:
130     return static_cast<const OMPLastprivateClause *>(C);
131   case OMPC_reduction:
132     return static_cast<const OMPReductionClause *>(C);
133   case OMPC_task_reduction:
134     return static_cast<const OMPTaskReductionClause *>(C);
135   case OMPC_in_reduction:
136     return static_cast<const OMPInReductionClause *>(C);
137   case OMPC_linear:
138     return static_cast<const OMPLinearClause *>(C);
139   case OMPC_schedule:
140   case OMPC_dist_schedule:
141   case OMPC_firstprivate:
142   case OMPC_default:
143   case OMPC_proc_bind:
144   case OMPC_if:
145   case OMPC_final:
146   case OMPC_num_threads:
147   case OMPC_safelen:
148   case OMPC_simdlen:
149   case OMPC_allocator:
150   case OMPC_collapse:
151   case OMPC_private:
152   case OMPC_shared:
153   case OMPC_aligned:
154   case OMPC_copyin:
155   case OMPC_copyprivate:
156   case OMPC_ordered:
157   case OMPC_nowait:
158   case OMPC_untied:
159   case OMPC_mergeable:
160   case OMPC_threadprivate:
161   case OMPC_allocate:
162   case OMPC_flush:
163   case OMPC_read:
164   case OMPC_write:
165   case OMPC_update:
166   case OMPC_capture:
167   case OMPC_seq_cst:
168   case OMPC_depend:
169   case OMPC_device:
170   case OMPC_threads:
171   case OMPC_simd:
172   case OMPC_map:
173   case OMPC_num_teams:
174   case OMPC_thread_limit:
175   case OMPC_priority:
176   case OMPC_grainsize:
177   case OMPC_nogroup:
178   case OMPC_num_tasks:
179   case OMPC_hint:
180   case OMPC_defaultmap:
181   case OMPC_unknown:
182   case OMPC_uniform:
183   case OMPC_to:
184   case OMPC_from:
185   case OMPC_use_device_ptr:
186   case OMPC_is_device_ptr:
187   case OMPC_unified_address:
188   case OMPC_unified_shared_memory:
189   case OMPC_reverse_offload:
190   case OMPC_dynamic_allocators:
191   case OMPC_atomic_default_mem_order:
192     break;
193   }
194 
195   return nullptr;
196 }
197 
198 OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
199                                            unsigned NumLoops,
200                                            SourceLocation StartLoc,
201                                            SourceLocation LParenLoc,
202                                            SourceLocation EndLoc) {
203   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
204   auto *Clause =
205       new (Mem) OMPOrderedClause(Num, NumLoops, StartLoc, LParenLoc, EndLoc);
206   for (unsigned I = 0; I < NumLoops; ++I) {
207     Clause->setLoopNumIterations(I, nullptr);
208     Clause->setLoopCounter(I, nullptr);
209   }
210   return Clause;
211 }
212 
213 OMPOrderedClause *OMPOrderedClause::CreateEmpty(const ASTContext &C,
214                                                 unsigned NumLoops) {
215   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * NumLoops));
216   auto *Clause = new (Mem) OMPOrderedClause(NumLoops);
217   for (unsigned I = 0; I < NumLoops; ++I) {
218     Clause->setLoopNumIterations(I, nullptr);
219     Clause->setLoopCounter(I, nullptr);
220   }
221   return Clause;
222 }
223 
224 void OMPOrderedClause::setLoopNumIterations(unsigned NumLoop,
225                                             Expr *NumIterations) {
226   assert(NumLoop < NumberOfLoops && "out of loops number.");
227   getTrailingObjects<Expr *>()[NumLoop] = NumIterations;
228 }
229 
230 ArrayRef<Expr *> OMPOrderedClause::getLoopNumIterations() const {
231   return llvm::makeArrayRef(getTrailingObjects<Expr *>(), NumberOfLoops);
232 }
233 
234 void OMPOrderedClause::setLoopCounter(unsigned NumLoop, Expr *Counter) {
235   assert(NumLoop < NumberOfLoops && "out of loops number.");
236   getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop] = Counter;
237 }
238 
239 Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) {
240   assert(NumLoop < NumberOfLoops && "out of loops number.");
241   return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
242 }
243 
244 const Expr *OMPOrderedClause::getLoopCounter(unsigned NumLoop) const {
245   assert(NumLoop < NumberOfLoops && "out of loops number.");
246   return getTrailingObjects<Expr *>()[NumberOfLoops + NumLoop];
247 }
248 
249 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
250   assert(VL.size() == varlist_size() &&
251          "Number of private copies is not the same as the preallocated buffer");
252   std::copy(VL.begin(), VL.end(), varlist_end());
253 }
254 
255 OMPPrivateClause *
256 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
257                          SourceLocation LParenLoc, SourceLocation EndLoc,
258                          ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) {
259   // Allocate space for private variables and initializer expressions.
260   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size()));
261   OMPPrivateClause *Clause =
262       new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
263   Clause->setVarRefs(VL);
264   Clause->setPrivateCopies(PrivateVL);
265   return Clause;
266 }
267 
268 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
269                                                 unsigned N) {
270   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N));
271   return new (Mem) OMPPrivateClause(N);
272 }
273 
274 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) {
275   assert(VL.size() == varlist_size() &&
276          "Number of private copies is not the same as the preallocated buffer");
277   std::copy(VL.begin(), VL.end(), varlist_end());
278 }
279 
280 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) {
281   assert(VL.size() == varlist_size() &&
282          "Number of inits is not the same as the preallocated buffer");
283   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
284 }
285 
286 OMPFirstprivateClause *
287 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc,
288                               SourceLocation LParenLoc, SourceLocation EndLoc,
289                               ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL,
290                               ArrayRef<Expr *> InitVL, Stmt *PreInit) {
291   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size()));
292   OMPFirstprivateClause *Clause =
293       new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
294   Clause->setVarRefs(VL);
295   Clause->setPrivateCopies(PrivateVL);
296   Clause->setInits(InitVL);
297   Clause->setPreInitStmt(PreInit);
298   return Clause;
299 }
300 
301 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
302                                                           unsigned N) {
303   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N));
304   return new (Mem) OMPFirstprivateClause(N);
305 }
306 
307 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) {
308   assert(PrivateCopies.size() == varlist_size() &&
309          "Number of private copies is not the same as the preallocated buffer");
310   std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end());
311 }
312 
313 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
314   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
315                                               "not the same as the "
316                                               "preallocated buffer");
317   std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end());
318 }
319 
320 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
321   assert(DstExprs.size() == varlist_size() && "Number of destination "
322                                               "expressions is not the same as "
323                                               "the preallocated buffer");
324   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
325 }
326 
327 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
328   assert(AssignmentOps.size() == varlist_size() &&
329          "Number of assignment expressions is not the same as the preallocated "
330          "buffer");
331   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
332             getDestinationExprs().end());
333 }
334 
335 OMPLastprivateClause *OMPLastprivateClause::Create(
336     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
337     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
338     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit,
339     Expr *PostUpdate) {
340   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
341   OMPLastprivateClause *Clause =
342       new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
343   Clause->setVarRefs(VL);
344   Clause->setSourceExprs(SrcExprs);
345   Clause->setDestinationExprs(DstExprs);
346   Clause->setAssignmentOps(AssignmentOps);
347   Clause->setPreInitStmt(PreInit);
348   Clause->setPostUpdateExpr(PostUpdate);
349   return Clause;
350 }
351 
352 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C,
353                                                         unsigned N) {
354   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
355   return new (Mem) OMPLastprivateClause(N);
356 }
357 
358 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C,
359                                          SourceLocation StartLoc,
360                                          SourceLocation LParenLoc,
361                                          SourceLocation EndLoc,
362                                          ArrayRef<Expr *> VL) {
363   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size()));
364   OMPSharedClause *Clause =
365       new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size());
366   Clause->setVarRefs(VL);
367   return Clause;
368 }
369 
370 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) {
371   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
372   return new (Mem) OMPSharedClause(N);
373 }
374 
375 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) {
376   assert(PL.size() == varlist_size() &&
377          "Number of privates is not the same as the preallocated buffer");
378   std::copy(PL.begin(), PL.end(), varlist_end());
379 }
380 
381 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) {
382   assert(IL.size() == varlist_size() &&
383          "Number of inits is not the same as the preallocated buffer");
384   std::copy(IL.begin(), IL.end(), getPrivates().end());
385 }
386 
387 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) {
388   assert(UL.size() == varlist_size() &&
389          "Number of updates is not the same as the preallocated buffer");
390   std::copy(UL.begin(), UL.end(), getInits().end());
391 }
392 
393 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) {
394   assert(FL.size() == varlist_size() &&
395          "Number of final updates is not the same as the preallocated buffer");
396   std::copy(FL.begin(), FL.end(), getUpdates().end());
397 }
398 
399 OMPLinearClause *OMPLinearClause::Create(
400     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
401     OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc,
402     SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL,
403     ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep,
404     Stmt *PreInit, Expr *PostUpdate) {
405   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
406   // (Step and CalcStep).
407   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2));
408   OMPLinearClause *Clause = new (Mem) OMPLinearClause(
409       StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size());
410   Clause->setVarRefs(VL);
411   Clause->setPrivates(PL);
412   Clause->setInits(IL);
413   // Fill update and final expressions with zeroes, they are provided later,
414   // after the directive construction.
415   std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(),
416             nullptr);
417   std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(),
418             nullptr);
419   Clause->setStep(Step);
420   Clause->setCalcStep(CalcStep);
421   Clause->setPreInitStmt(PreInit);
422   Clause->setPostUpdateExpr(PostUpdate);
423   return Clause;
424 }
425 
426 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C,
427                                               unsigned NumVars) {
428   // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions
429   // (Step and CalcStep).
430   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2));
431   return new (Mem) OMPLinearClause(NumVars);
432 }
433 
434 OMPAlignedClause *
435 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc,
436                          SourceLocation LParenLoc, SourceLocation ColonLoc,
437                          SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) {
438   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
439   OMPAlignedClause *Clause = new (Mem)
440       OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size());
441   Clause->setVarRefs(VL);
442   Clause->setAlignment(A);
443   return Clause;
444 }
445 
446 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C,
447                                                 unsigned NumVars) {
448   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1));
449   return new (Mem) OMPAlignedClause(NumVars);
450 }
451 
452 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
453   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
454                                               "not the same as the "
455                                               "preallocated buffer");
456   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
457 }
458 
459 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
460   assert(DstExprs.size() == varlist_size() && "Number of destination "
461                                               "expressions is not the same as "
462                                               "the preallocated buffer");
463   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
464 }
465 
466 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
467   assert(AssignmentOps.size() == varlist_size() &&
468          "Number of assignment expressions is not the same as the preallocated "
469          "buffer");
470   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
471             getDestinationExprs().end());
472 }
473 
474 OMPCopyinClause *OMPCopyinClause::Create(
475     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
476     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
477     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
478   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
479   OMPCopyinClause *Clause =
480       new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size());
481   Clause->setVarRefs(VL);
482   Clause->setSourceExprs(SrcExprs);
483   Clause->setDestinationExprs(DstExprs);
484   Clause->setAssignmentOps(AssignmentOps);
485   return Clause;
486 }
487 
488 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) {
489   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
490   return new (Mem) OMPCopyinClause(N);
491 }
492 
493 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) {
494   assert(SrcExprs.size() == varlist_size() && "Number of source expressions is "
495                                               "not the same as the "
496                                               "preallocated buffer");
497   std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end());
498 }
499 
500 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) {
501   assert(DstExprs.size() == varlist_size() && "Number of destination "
502                                               "expressions is not the same as "
503                                               "the preallocated buffer");
504   std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end());
505 }
506 
507 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) {
508   assert(AssignmentOps.size() == varlist_size() &&
509          "Number of assignment expressions is not the same as the preallocated "
510          "buffer");
511   std::copy(AssignmentOps.begin(), AssignmentOps.end(),
512             getDestinationExprs().end());
513 }
514 
515 OMPCopyprivateClause *OMPCopyprivateClause::Create(
516     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
517     SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs,
518     ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) {
519   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size()));
520   OMPCopyprivateClause *Clause =
521       new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size());
522   Clause->setVarRefs(VL);
523   Clause->setSourceExprs(SrcExprs);
524   Clause->setDestinationExprs(DstExprs);
525   Clause->setAssignmentOps(AssignmentOps);
526   return Clause;
527 }
528 
529 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C,
530                                                         unsigned N) {
531   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N));
532   return new (Mem) OMPCopyprivateClause(N);
533 }
534 
535 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
536   assert(Privates.size() == varlist_size() &&
537          "Number of private copies is not the same as the preallocated buffer");
538   std::copy(Privates.begin(), Privates.end(), varlist_end());
539 }
540 
541 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
542   assert(
543       LHSExprs.size() == varlist_size() &&
544       "Number of LHS expressions is not the same as the preallocated buffer");
545   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
546 }
547 
548 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
549   assert(
550       RHSExprs.size() == varlist_size() &&
551       "Number of RHS expressions is not the same as the preallocated buffer");
552   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
553 }
554 
555 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
556   assert(ReductionOps.size() == varlist_size() && "Number of reduction "
557                                                   "expressions is not the same "
558                                                   "as the preallocated buffer");
559   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
560 }
561 
562 OMPReductionClause *OMPReductionClause::Create(
563     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
564     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
565     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
566     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
567     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
568     Expr *PostUpdate) {
569   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
570   OMPReductionClause *Clause = new (Mem) OMPReductionClause(
571       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
572   Clause->setVarRefs(VL);
573   Clause->setPrivates(Privates);
574   Clause->setLHSExprs(LHSExprs);
575   Clause->setRHSExprs(RHSExprs);
576   Clause->setReductionOps(ReductionOps);
577   Clause->setPreInitStmt(PreInit);
578   Clause->setPostUpdateExpr(PostUpdate);
579   return Clause;
580 }
581 
582 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C,
583                                                     unsigned N) {
584   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
585   return new (Mem) OMPReductionClause(N);
586 }
587 
588 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
589   assert(Privates.size() == varlist_size() &&
590          "Number of private copies is not the same as the preallocated buffer");
591   std::copy(Privates.begin(), Privates.end(), varlist_end());
592 }
593 
594 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
595   assert(
596       LHSExprs.size() == varlist_size() &&
597       "Number of LHS expressions is not the same as the preallocated buffer");
598   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
599 }
600 
601 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
602   assert(
603       RHSExprs.size() == varlist_size() &&
604       "Number of RHS expressions is not the same as the preallocated buffer");
605   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
606 }
607 
608 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
609   assert(ReductionOps.size() == varlist_size() && "Number of task reduction "
610                                                   "expressions is not the same "
611                                                   "as the preallocated buffer");
612   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
613 }
614 
615 OMPTaskReductionClause *OMPTaskReductionClause::Create(
616     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
617     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
618     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
619     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
620     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit,
621     Expr *PostUpdate) {
622   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size()));
623   OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause(
624       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
625   Clause->setVarRefs(VL);
626   Clause->setPrivates(Privates);
627   Clause->setLHSExprs(LHSExprs);
628   Clause->setRHSExprs(RHSExprs);
629   Clause->setReductionOps(ReductionOps);
630   Clause->setPreInitStmt(PreInit);
631   Clause->setPostUpdateExpr(PostUpdate);
632   return Clause;
633 }
634 
635 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C,
636                                                             unsigned N) {
637   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N));
638   return new (Mem) OMPTaskReductionClause(N);
639 }
640 
641 void OMPInReductionClause::setPrivates(ArrayRef<Expr *> Privates) {
642   assert(Privates.size() == varlist_size() &&
643          "Number of private copies is not the same as the preallocated buffer");
644   std::copy(Privates.begin(), Privates.end(), varlist_end());
645 }
646 
647 void OMPInReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) {
648   assert(
649       LHSExprs.size() == varlist_size() &&
650       "Number of LHS expressions is not the same as the preallocated buffer");
651   std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end());
652 }
653 
654 void OMPInReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) {
655   assert(
656       RHSExprs.size() == varlist_size() &&
657       "Number of RHS expressions is not the same as the preallocated buffer");
658   std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end());
659 }
660 
661 void OMPInReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) {
662   assert(ReductionOps.size() == varlist_size() && "Number of in reduction "
663                                                   "expressions is not the same "
664                                                   "as the preallocated buffer");
665   std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end());
666 }
667 
668 void OMPInReductionClause::setTaskgroupDescriptors(
669     ArrayRef<Expr *> TaskgroupDescriptors) {
670   assert(TaskgroupDescriptors.size() == varlist_size() &&
671          "Number of in reduction descriptors is not the same as the "
672          "preallocated buffer");
673   std::copy(TaskgroupDescriptors.begin(), TaskgroupDescriptors.end(),
674             getReductionOps().end());
675 }
676 
677 OMPInReductionClause *OMPInReductionClause::Create(
678     const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc,
679     SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL,
680     NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
681     ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs,
682     ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps,
683     ArrayRef<Expr *> TaskgroupDescriptors, Stmt *PreInit, Expr *PostUpdate) {
684   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * VL.size()));
685   OMPInReductionClause *Clause = new (Mem) OMPInReductionClause(
686       StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo);
687   Clause->setVarRefs(VL);
688   Clause->setPrivates(Privates);
689   Clause->setLHSExprs(LHSExprs);
690   Clause->setRHSExprs(RHSExprs);
691   Clause->setReductionOps(ReductionOps);
692   Clause->setTaskgroupDescriptors(TaskgroupDescriptors);
693   Clause->setPreInitStmt(PreInit);
694   Clause->setPostUpdateExpr(PostUpdate);
695   return Clause;
696 }
697 
698 OMPInReductionClause *OMPInReductionClause::CreateEmpty(const ASTContext &C,
699                                                         unsigned N) {
700   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(6 * N));
701   return new (Mem) OMPInReductionClause(N);
702 }
703 
704 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C,
705                                        SourceLocation StartLoc,
706                                        SourceLocation LParenLoc,
707                                        SourceLocation EndLoc,
708                                        ArrayRef<Expr *> VL) {
709   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1));
710   OMPFlushClause *Clause =
711       new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size());
712   Clause->setVarRefs(VL);
713   return Clause;
714 }
715 
716 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) {
717   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N));
718   return new (Mem) OMPFlushClause(N);
719 }
720 
721 OMPDependClause *
722 OMPDependClause::Create(const ASTContext &C, SourceLocation StartLoc,
723                         SourceLocation LParenLoc, SourceLocation EndLoc,
724                         OpenMPDependClauseKind DepKind, SourceLocation DepLoc,
725                         SourceLocation ColonLoc, ArrayRef<Expr *> VL,
726                         unsigned NumLoops) {
727   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + NumLoops));
728   OMPDependClause *Clause = new (Mem)
729       OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size(), NumLoops);
730   Clause->setVarRefs(VL);
731   Clause->setDependencyKind(DepKind);
732   Clause->setDependencyLoc(DepLoc);
733   Clause->setColonLoc(ColonLoc);
734   for (unsigned I = 0 ; I < NumLoops; ++I)
735     Clause->setLoopData(I, nullptr);
736   return Clause;
737 }
738 
739 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N,
740                                               unsigned NumLoops) {
741   void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + NumLoops));
742   return new (Mem) OMPDependClause(N, NumLoops);
743 }
744 
745 void OMPDependClause::setLoopData(unsigned NumLoop, Expr *Cnt) {
746   assert((getDependencyKind() == OMPC_DEPEND_sink ||
747           getDependencyKind() == OMPC_DEPEND_source) &&
748          NumLoop < NumLoops &&
749          "Expected sink or source depend + loop index must be less number of "
750          "loops.");
751   auto It = std::next(getVarRefs().end(), NumLoop);
752   *It = Cnt;
753 }
754 
755 Expr *OMPDependClause::getLoopData(unsigned NumLoop) {
756   assert((getDependencyKind() == OMPC_DEPEND_sink ||
757           getDependencyKind() == OMPC_DEPEND_source) &&
758          NumLoop < NumLoops &&
759          "Expected sink or source depend + loop index must be less number of "
760          "loops.");
761   auto It = std::next(getVarRefs().end(), NumLoop);
762   return *It;
763 }
764 
765 const Expr *OMPDependClause::getLoopData(unsigned NumLoop) const {
766   assert((getDependencyKind() == OMPC_DEPEND_sink ||
767           getDependencyKind() == OMPC_DEPEND_source) &&
768          NumLoop < NumLoops &&
769          "Expected sink or source depend + loop index must be less number of "
770          "loops.");
771   auto It = std::next(getVarRefs().end(), NumLoop);
772   return *It;
773 }
774 
775 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber(
776     MappableExprComponentListsRef ComponentLists) {
777   unsigned TotalNum = 0u;
778   for (auto &C : ComponentLists)
779     TotalNum += C.size();
780   return TotalNum;
781 }
782 
783 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber(
784     ArrayRef<const ValueDecl *> Declarations) {
785   unsigned TotalNum = 0u;
786   llvm::SmallPtrSet<const ValueDecl *, 8> Cache;
787   for (const ValueDecl *D : Declarations) {
788     const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
789     if (Cache.count(VD))
790       continue;
791     ++TotalNum;
792     Cache.insert(VD);
793   }
794   return TotalNum;
795 }
796 
797 OMPMapClause *OMPMapClause::Create(
798     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
799     ArrayRef<ValueDecl *> Declarations,
800     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
801     ArrayRef<OpenMPMapModifierKind> MapModifiers,
802     ArrayRef<SourceLocation> MapModifiersLoc,
803     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId,
804     OpenMPMapClauseKind Type, bool TypeIsImplicit, SourceLocation TypeLoc) {
805   OMPMappableExprListSizeTy Sizes;
806   Sizes.NumVars = Vars.size();
807   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
808   Sizes.NumComponentLists = ComponentLists.size();
809   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
810 
811   // We need to allocate:
812   // 2 x NumVars x Expr* - we have an original list expression and an associated
813   // user-defined mapper for each clause list entry.
814   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
815   // with each component list.
816   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
817   // number of lists for each unique declaration and the size of each component
818   // list.
819   // NumComponents x MappableComponent - the total of all the components in all
820   // the lists.
821   void *Mem = C.Allocate(
822       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
823                        OMPClauseMappableExprCommon::MappableComponent>(
824           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
825           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
826           Sizes.NumComponents));
827   OMPMapClause *Clause = new (Mem)
828       OMPMapClause(MapModifiers, MapModifiersLoc, UDMQualifierLoc, MapperId,
829                    Type, TypeIsImplicit, TypeLoc, Locs, Sizes);
830 
831   Clause->setVarRefs(Vars);
832   Clause->setUDMapperRefs(UDMapperRefs);
833   Clause->setClauseInfo(Declarations, ComponentLists);
834   Clause->setMapType(Type);
835   Clause->setMapLoc(TypeLoc);
836   return Clause;
837 }
838 
839 OMPMapClause *
840 OMPMapClause::CreateEmpty(const ASTContext &C,
841                           const OMPMappableExprListSizeTy &Sizes) {
842   void *Mem = C.Allocate(
843       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
844                        OMPClauseMappableExprCommon::MappableComponent>(
845           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
846           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
847           Sizes.NumComponents));
848   return new (Mem) OMPMapClause(Sizes);
849 }
850 
851 OMPToClause *OMPToClause::Create(
852     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
853     ArrayRef<ValueDecl *> Declarations,
854     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
855     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
856   OMPMappableExprListSizeTy Sizes;
857   Sizes.NumVars = Vars.size();
858   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
859   Sizes.NumComponentLists = ComponentLists.size();
860   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
861 
862   // We need to allocate:
863   // 2 x NumVars x Expr* - we have an original list expression and an associated
864   // user-defined mapper for each clause list entry.
865   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
866   // with each component list.
867   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
868   // number of lists for each unique declaration and the size of each component
869   // list.
870   // NumComponents x MappableComponent - the total of all the components in all
871   // the lists.
872   void *Mem = C.Allocate(
873       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
874                        OMPClauseMappableExprCommon::MappableComponent>(
875           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
876           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
877           Sizes.NumComponents));
878 
879   auto *Clause = new (Mem) OMPToClause(UDMQualifierLoc, MapperId, Locs, Sizes);
880 
881   Clause->setVarRefs(Vars);
882   Clause->setUDMapperRefs(UDMapperRefs);
883   Clause->setClauseInfo(Declarations, ComponentLists);
884   return Clause;
885 }
886 
887 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C,
888                                       const OMPMappableExprListSizeTy &Sizes) {
889   void *Mem = C.Allocate(
890       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
891                        OMPClauseMappableExprCommon::MappableComponent>(
892           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
893           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
894           Sizes.NumComponents));
895   return new (Mem) OMPToClause(Sizes);
896 }
897 
898 OMPFromClause *OMPFromClause::Create(
899     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
900     ArrayRef<ValueDecl *> Declarations,
901     MappableExprComponentListsRef ComponentLists, ArrayRef<Expr *> UDMapperRefs,
902     NestedNameSpecifierLoc UDMQualifierLoc, DeclarationNameInfo MapperId) {
903   OMPMappableExprListSizeTy Sizes;
904   Sizes.NumVars = Vars.size();
905   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
906   Sizes.NumComponentLists = ComponentLists.size();
907   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
908 
909   // We need to allocate:
910   // 2 x NumVars x Expr* - we have an original list expression and an associated
911   // user-defined mapper for each clause list entry.
912   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
913   // with each component list.
914   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
915   // number of lists for each unique declaration and the size of each component
916   // list.
917   // NumComponents x MappableComponent - the total of all the components in all
918   // the lists.
919   void *Mem = C.Allocate(
920       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
921                        OMPClauseMappableExprCommon::MappableComponent>(
922           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
923           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
924           Sizes.NumComponents));
925 
926   auto *Clause =
927       new (Mem) OMPFromClause(UDMQualifierLoc, MapperId, Locs, Sizes);
928 
929   Clause->setVarRefs(Vars);
930   Clause->setUDMapperRefs(UDMapperRefs);
931   Clause->setClauseInfo(Declarations, ComponentLists);
932   return Clause;
933 }
934 
935 OMPFromClause *
936 OMPFromClause::CreateEmpty(const ASTContext &C,
937                            const OMPMappableExprListSizeTy &Sizes) {
938   void *Mem = C.Allocate(
939       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
940                        OMPClauseMappableExprCommon::MappableComponent>(
941           2 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
942           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
943           Sizes.NumComponents));
944   return new (Mem) OMPFromClause(Sizes);
945 }
946 
947 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) {
948   assert(VL.size() == varlist_size() &&
949          "Number of private copies is not the same as the preallocated buffer");
950   std::copy(VL.begin(), VL.end(), varlist_end());
951 }
952 
953 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) {
954   assert(VL.size() == varlist_size() &&
955          "Number of inits is not the same as the preallocated buffer");
956   std::copy(VL.begin(), VL.end(), getPrivateCopies().end());
957 }
958 
959 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create(
960     const ASTContext &C, const OMPVarListLocTy &Locs, ArrayRef<Expr *> Vars,
961     ArrayRef<Expr *> PrivateVars, ArrayRef<Expr *> Inits,
962     ArrayRef<ValueDecl *> Declarations,
963     MappableExprComponentListsRef ComponentLists) {
964   OMPMappableExprListSizeTy Sizes;
965   Sizes.NumVars = Vars.size();
966   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
967   Sizes.NumComponentLists = ComponentLists.size();
968   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
969 
970   // We need to allocate:
971   // 3 x NumVars x Expr* - we have an original list expression for each clause
972   // list entry and an equal number of private copies and inits.
973   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
974   // with each component list.
975   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
976   // number of lists for each unique declaration and the size of each component
977   // list.
978   // NumComponents x MappableComponent - the total of all the components in all
979   // the lists.
980   void *Mem = C.Allocate(
981       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
982                        OMPClauseMappableExprCommon::MappableComponent>(
983           3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
984           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
985           Sizes.NumComponents));
986 
987   OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause(Locs, Sizes);
988 
989   Clause->setVarRefs(Vars);
990   Clause->setPrivateCopies(PrivateVars);
991   Clause->setInits(Inits);
992   Clause->setClauseInfo(Declarations, ComponentLists);
993   return Clause;
994 }
995 
996 OMPUseDevicePtrClause *
997 OMPUseDevicePtrClause::CreateEmpty(const ASTContext &C,
998                                    const OMPMappableExprListSizeTy &Sizes) {
999   void *Mem = C.Allocate(
1000       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1001                        OMPClauseMappableExprCommon::MappableComponent>(
1002           3 * Sizes.NumVars, Sizes.NumUniqueDeclarations,
1003           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1004           Sizes.NumComponents));
1005   return new (Mem) OMPUseDevicePtrClause(Sizes);
1006 }
1007 
1008 OMPIsDevicePtrClause *
1009 OMPIsDevicePtrClause::Create(const ASTContext &C, const OMPVarListLocTy &Locs,
1010                              ArrayRef<Expr *> Vars,
1011                              ArrayRef<ValueDecl *> Declarations,
1012                              MappableExprComponentListsRef ComponentLists) {
1013   OMPMappableExprListSizeTy Sizes;
1014   Sizes.NumVars = Vars.size();
1015   Sizes.NumUniqueDeclarations = getUniqueDeclarationsTotalNumber(Declarations);
1016   Sizes.NumComponentLists = ComponentLists.size();
1017   Sizes.NumComponents = getComponentsTotalNumber(ComponentLists);
1018 
1019   // We need to allocate:
1020   // NumVars x Expr* - we have an original list expression for each clause list
1021   // entry.
1022   // NumUniqueDeclarations x ValueDecl* - unique base declarations associated
1023   // with each component list.
1024   // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the
1025   // number of lists for each unique declaration and the size of each component
1026   // list.
1027   // NumComponents x MappableComponent - the total of all the components in all
1028   // the lists.
1029   void *Mem = C.Allocate(
1030       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1031                        OMPClauseMappableExprCommon::MappableComponent>(
1032           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1033           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1034           Sizes.NumComponents));
1035 
1036   OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause(Locs, Sizes);
1037 
1038   Clause->setVarRefs(Vars);
1039   Clause->setClauseInfo(Declarations, ComponentLists);
1040   return Clause;
1041 }
1042 
1043 OMPIsDevicePtrClause *
1044 OMPIsDevicePtrClause::CreateEmpty(const ASTContext &C,
1045                                   const OMPMappableExprListSizeTy &Sizes) {
1046   void *Mem = C.Allocate(
1047       totalSizeToAlloc<Expr *, ValueDecl *, unsigned,
1048                        OMPClauseMappableExprCommon::MappableComponent>(
1049           Sizes.NumVars, Sizes.NumUniqueDeclarations,
1050           Sizes.NumUniqueDeclarations + Sizes.NumComponentLists,
1051           Sizes.NumComponents));
1052   return new (Mem) OMPIsDevicePtrClause(Sizes);
1053 }
1054 
1055 //===----------------------------------------------------------------------===//
1056 //  OpenMP clauses printing methods
1057 //===----------------------------------------------------------------------===//
1058 
1059 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
1060   OS << "if(";
1061   if (Node->getNameModifier() != OMPD_unknown)
1062     OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
1063   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1064   OS << ")";
1065 }
1066 
1067 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
1068   OS << "final(";
1069   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1070   OS << ")";
1071 }
1072 
1073 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
1074   OS << "num_threads(";
1075   Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
1076   OS << ")";
1077 }
1078 
1079 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
1080   OS << "safelen(";
1081   Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
1082   OS << ")";
1083 }
1084 
1085 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
1086   OS << "simdlen(";
1087   Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
1088   OS << ")";
1089 }
1090 
1091 void OMPClausePrinter::VisitOMPAllocatorClause(OMPAllocatorClause *Node) {
1092   OS << "allocator(";
1093   Node->getAllocator()->printPretty(OS, nullptr, Policy, 0);
1094   OS << ")";
1095 }
1096 
1097 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
1098   OS << "collapse(";
1099   Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
1100   OS << ")";
1101 }
1102 
1103 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
1104   OS << "default("
1105      << getOpenMPSimpleClauseTypeName(OMPC_default, Node->getDefaultKind())
1106      << ")";
1107 }
1108 
1109 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
1110   OS << "proc_bind("
1111      << getOpenMPSimpleClauseTypeName(OMPC_proc_bind, Node->getProcBindKind())
1112      << ")";
1113 }
1114 
1115 void OMPClausePrinter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {
1116   OS << "unified_address";
1117 }
1118 
1119 void OMPClausePrinter::VisitOMPUnifiedSharedMemoryClause(
1120     OMPUnifiedSharedMemoryClause *) {
1121   OS << "unified_shared_memory";
1122 }
1123 
1124 void OMPClausePrinter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {
1125   OS << "reverse_offload";
1126 }
1127 
1128 void OMPClausePrinter::VisitOMPDynamicAllocatorsClause(
1129     OMPDynamicAllocatorsClause *) {
1130   OS << "dynamic_allocators";
1131 }
1132 
1133 void OMPClausePrinter::VisitOMPAtomicDefaultMemOrderClause(
1134     OMPAtomicDefaultMemOrderClause *Node) {
1135   OS << "atomic_default_mem_order("
1136      << getOpenMPSimpleClauseTypeName(OMPC_atomic_default_mem_order,
1137                                       Node->getAtomicDefaultMemOrderKind())
1138      << ")";
1139 }
1140 
1141 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
1142   OS << "schedule(";
1143   if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1144     OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1145                                         Node->getFirstScheduleModifier());
1146     if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1147       OS << ", ";
1148       OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1149                                           Node->getSecondScheduleModifier());
1150     }
1151     OS << ": ";
1152   }
1153   OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
1154   if (auto *E = Node->getChunkSize()) {
1155     OS << ", ";
1156     E->printPretty(OS, nullptr, Policy);
1157   }
1158   OS << ")";
1159 }
1160 
1161 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
1162   OS << "ordered";
1163   if (auto *Num = Node->getNumForLoops()) {
1164     OS << "(";
1165     Num->printPretty(OS, nullptr, Policy, 0);
1166     OS << ")";
1167   }
1168 }
1169 
1170 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
1171   OS << "nowait";
1172 }
1173 
1174 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
1175   OS << "untied";
1176 }
1177 
1178 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
1179   OS << "nogroup";
1180 }
1181 
1182 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
1183   OS << "mergeable";
1184 }
1185 
1186 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
1187 
1188 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
1189 
1190 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *) {
1191   OS << "update";
1192 }
1193 
1194 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
1195   OS << "capture";
1196 }
1197 
1198 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
1199   OS << "seq_cst";
1200 }
1201 
1202 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
1203   OS << "threads";
1204 }
1205 
1206 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
1207 
1208 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
1209   OS << "device(";
1210   Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
1211   OS << ")";
1212 }
1213 
1214 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
1215   OS << "num_teams(";
1216   Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
1217   OS << ")";
1218 }
1219 
1220 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
1221   OS << "thread_limit(";
1222   Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
1223   OS << ")";
1224 }
1225 
1226 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
1227   OS << "priority(";
1228   Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
1229   OS << ")";
1230 }
1231 
1232 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
1233   OS << "grainsize(";
1234   Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
1235   OS << ")";
1236 }
1237 
1238 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
1239   OS << "num_tasks(";
1240   Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
1241   OS << ")";
1242 }
1243 
1244 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
1245   OS << "hint(";
1246   Node->getHint()->printPretty(OS, nullptr, Policy, 0);
1247   OS << ")";
1248 }
1249 
1250 template<typename T>
1251 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
1252   for (typename T::varlist_iterator I = Node->varlist_begin(),
1253                                     E = Node->varlist_end();
1254        I != E; ++I) {
1255     assert(*I && "Expected non-null Stmt");
1256     OS << (I == Node->varlist_begin() ? StartSym : ',');
1257     if (auto *DRE = dyn_cast<DeclRefExpr>(*I)) {
1258       if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
1259         DRE->printPretty(OS, nullptr, Policy, 0);
1260       else
1261         DRE->getDecl()->printQualifiedName(OS);
1262     } else
1263       (*I)->printPretty(OS, nullptr, Policy, 0);
1264   }
1265 }
1266 
1267 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
1268   if (!Node->varlist_empty()) {
1269     OS << "private";
1270     VisitOMPClauseList(Node, '(');
1271     OS << ")";
1272   }
1273 }
1274 
1275 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
1276   if (!Node->varlist_empty()) {
1277     OS << "firstprivate";
1278     VisitOMPClauseList(Node, '(');
1279     OS << ")";
1280   }
1281 }
1282 
1283 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
1284   if (!Node->varlist_empty()) {
1285     OS << "lastprivate";
1286     VisitOMPClauseList(Node, '(');
1287     OS << ")";
1288   }
1289 }
1290 
1291 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
1292   if (!Node->varlist_empty()) {
1293     OS << "shared";
1294     VisitOMPClauseList(Node, '(');
1295     OS << ")";
1296   }
1297 }
1298 
1299 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
1300   if (!Node->varlist_empty()) {
1301     OS << "reduction(";
1302     NestedNameSpecifier *QualifierLoc =
1303         Node->getQualifierLoc().getNestedNameSpecifier();
1304     OverloadedOperatorKind OOK =
1305         Node->getNameInfo().getName().getCXXOverloadedOperator();
1306     if (QualifierLoc == nullptr && OOK != OO_None) {
1307       // Print reduction identifier in C format
1308       OS << getOperatorSpelling(OOK);
1309     } else {
1310       // Use C++ format
1311       if (QualifierLoc != nullptr)
1312         QualifierLoc->print(OS, Policy);
1313       OS << Node->getNameInfo();
1314     }
1315     OS << ":";
1316     VisitOMPClauseList(Node, ' ');
1317     OS << ")";
1318   }
1319 }
1320 
1321 void OMPClausePrinter::VisitOMPTaskReductionClause(
1322     OMPTaskReductionClause *Node) {
1323   if (!Node->varlist_empty()) {
1324     OS << "task_reduction(";
1325     NestedNameSpecifier *QualifierLoc =
1326         Node->getQualifierLoc().getNestedNameSpecifier();
1327     OverloadedOperatorKind OOK =
1328         Node->getNameInfo().getName().getCXXOverloadedOperator();
1329     if (QualifierLoc == nullptr && OOK != OO_None) {
1330       // Print reduction identifier in C format
1331       OS << getOperatorSpelling(OOK);
1332     } else {
1333       // Use C++ format
1334       if (QualifierLoc != nullptr)
1335         QualifierLoc->print(OS, Policy);
1336       OS << Node->getNameInfo();
1337     }
1338     OS << ":";
1339     VisitOMPClauseList(Node, ' ');
1340     OS << ")";
1341   }
1342 }
1343 
1344 void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
1345   if (!Node->varlist_empty()) {
1346     OS << "in_reduction(";
1347     NestedNameSpecifier *QualifierLoc =
1348         Node->getQualifierLoc().getNestedNameSpecifier();
1349     OverloadedOperatorKind OOK =
1350         Node->getNameInfo().getName().getCXXOverloadedOperator();
1351     if (QualifierLoc == nullptr && OOK != OO_None) {
1352       // Print reduction identifier in C format
1353       OS << getOperatorSpelling(OOK);
1354     } else {
1355       // Use C++ format
1356       if (QualifierLoc != nullptr)
1357         QualifierLoc->print(OS, Policy);
1358       OS << Node->getNameInfo();
1359     }
1360     OS << ":";
1361     VisitOMPClauseList(Node, ' ');
1362     OS << ")";
1363   }
1364 }
1365 
1366 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
1367   if (!Node->varlist_empty()) {
1368     OS << "linear";
1369     if (Node->getModifierLoc().isValid()) {
1370       OS << '('
1371          << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
1372     }
1373     VisitOMPClauseList(Node, '(');
1374     if (Node->getModifierLoc().isValid())
1375       OS << ')';
1376     if (Node->getStep() != nullptr) {
1377       OS << ": ";
1378       Node->getStep()->printPretty(OS, nullptr, Policy, 0);
1379     }
1380     OS << ")";
1381   }
1382 }
1383 
1384 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
1385   if (!Node->varlist_empty()) {
1386     OS << "aligned";
1387     VisitOMPClauseList(Node, '(');
1388     if (Node->getAlignment() != nullptr) {
1389       OS << ": ";
1390       Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
1391     }
1392     OS << ")";
1393   }
1394 }
1395 
1396 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
1397   if (!Node->varlist_empty()) {
1398     OS << "copyin";
1399     VisitOMPClauseList(Node, '(');
1400     OS << ")";
1401   }
1402 }
1403 
1404 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
1405   if (!Node->varlist_empty()) {
1406     OS << "copyprivate";
1407     VisitOMPClauseList(Node, '(');
1408     OS << ")";
1409   }
1410 }
1411 
1412 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
1413   if (!Node->varlist_empty()) {
1414     VisitOMPClauseList(Node, '(');
1415     OS << ")";
1416   }
1417 }
1418 
1419 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
1420   OS << "depend(";
1421   OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
1422                                       Node->getDependencyKind());
1423   if (!Node->varlist_empty()) {
1424     OS << " :";
1425     VisitOMPClauseList(Node, ' ');
1426   }
1427   OS << ")";
1428 }
1429 
1430 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
1431   if (!Node->varlist_empty()) {
1432     OS << "map(";
1433     if (Node->getMapType() != OMPC_MAP_unknown) {
1434       for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) {
1435         if (Node->getMapTypeModifier(I) != OMPC_MAP_MODIFIER_unknown) {
1436           OS << getOpenMPSimpleClauseTypeName(OMPC_map,
1437                                               Node->getMapTypeModifier(I));
1438           if (Node->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_mapper) {
1439             OS << '(';
1440             NestedNameSpecifier *MapperNNS =
1441                 Node->getMapperQualifierLoc().getNestedNameSpecifier();
1442             if (MapperNNS)
1443               MapperNNS->print(OS, Policy);
1444             OS << Node->getMapperIdInfo() << ')';
1445           }
1446           OS << ',';
1447         }
1448       }
1449       OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
1450       OS << ':';
1451     }
1452     VisitOMPClauseList(Node, ' ');
1453     OS << ")";
1454   }
1455 }
1456 
1457 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
1458   if (!Node->varlist_empty()) {
1459     OS << "to";
1460     DeclarationNameInfo MapperId = Node->getMapperIdInfo();
1461     if (MapperId.getName() && !MapperId.getName().isEmpty()) {
1462       OS << '(';
1463       OS << "mapper(";
1464       NestedNameSpecifier *MapperNNS =
1465           Node->getMapperQualifierLoc().getNestedNameSpecifier();
1466       if (MapperNNS)
1467         MapperNNS->print(OS, Policy);
1468       OS << MapperId << "):";
1469       VisitOMPClauseList(Node, ' ');
1470     } else {
1471       VisitOMPClauseList(Node, '(');
1472     }
1473     OS << ")";
1474   }
1475 }
1476 
1477 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
1478   if (!Node->varlist_empty()) {
1479     OS << "from";
1480     DeclarationNameInfo MapperId = Node->getMapperIdInfo();
1481     if (MapperId.getName() && !MapperId.getName().isEmpty()) {
1482       OS << '(';
1483       OS << "mapper(";
1484       NestedNameSpecifier *MapperNNS =
1485           Node->getMapperQualifierLoc().getNestedNameSpecifier();
1486       if (MapperNNS)
1487         MapperNNS->print(OS, Policy);
1488       OS << MapperId << "):";
1489       VisitOMPClauseList(Node, ' ');
1490     } else {
1491       VisitOMPClauseList(Node, '(');
1492     }
1493     OS << ")";
1494   }
1495 }
1496 
1497 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
1498   OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
1499                            OMPC_dist_schedule, Node->getDistScheduleKind());
1500   if (auto *E = Node->getChunkSize()) {
1501     OS << ", ";
1502     E->printPretty(OS, nullptr, Policy);
1503   }
1504   OS << ")";
1505 }
1506 
1507 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
1508   OS << "defaultmap(";
1509   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
1510                                       Node->getDefaultmapModifier());
1511   OS << ": ";
1512   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
1513     Node->getDefaultmapKind());
1514   OS << ")";
1515 }
1516 
1517 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
1518   if (!Node->varlist_empty()) {
1519     OS << "use_device_ptr";
1520     VisitOMPClauseList(Node, '(');
1521     OS << ")";
1522   }
1523 }
1524 
1525 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
1526   if (!Node->varlist_empty()) {
1527     OS << "is_device_ptr";
1528     VisitOMPClauseList(Node, '(');
1529     OS << ")";
1530   }
1531 }
1532 
1533