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