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