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