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