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