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