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 //===----------------------------------------------------------------------===//
1517 //  OpenMP clauses printing methods
1518 //===----------------------------------------------------------------------===//
1519 
1520 void OMPClausePrinter::VisitOMPIfClause(OMPIfClause *Node) {
1521   OS << "if(";
1522   if (Node->getNameModifier() != OMPD_unknown)
1523     OS << getOpenMPDirectiveName(Node->getNameModifier()) << ": ";
1524   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1525   OS << ")";
1526 }
1527 
1528 void OMPClausePrinter::VisitOMPFinalClause(OMPFinalClause *Node) {
1529   OS << "final(";
1530   Node->getCondition()->printPretty(OS, nullptr, Policy, 0);
1531   OS << ")";
1532 }
1533 
1534 void OMPClausePrinter::VisitOMPNumThreadsClause(OMPNumThreadsClause *Node) {
1535   OS << "num_threads(";
1536   Node->getNumThreads()->printPretty(OS, nullptr, Policy, 0);
1537   OS << ")";
1538 }
1539 
1540 void OMPClausePrinter::VisitOMPSafelenClause(OMPSafelenClause *Node) {
1541   OS << "safelen(";
1542   Node->getSafelen()->printPretty(OS, nullptr, Policy, 0);
1543   OS << ")";
1544 }
1545 
1546 void OMPClausePrinter::VisitOMPSimdlenClause(OMPSimdlenClause *Node) {
1547   OS << "simdlen(";
1548   Node->getSimdlen()->printPretty(OS, nullptr, Policy, 0);
1549   OS << ")";
1550 }
1551 
1552 void OMPClausePrinter::VisitOMPSizesClause(OMPSizesClause *Node) {
1553   OS << "sizes(";
1554   bool First = true;
1555   for (auto Size : Node->getSizesRefs()) {
1556     if (!First)
1557       OS << ", ";
1558     Size->printPretty(OS, nullptr, Policy, 0);
1559     First = false;
1560   }
1561   OS << ")";
1562 }
1563 
1564 void OMPClausePrinter::VisitOMPAllocatorClause(OMPAllocatorClause *Node) {
1565   OS << "allocator(";
1566   Node->getAllocator()->printPretty(OS, nullptr, Policy, 0);
1567   OS << ")";
1568 }
1569 
1570 void OMPClausePrinter::VisitOMPCollapseClause(OMPCollapseClause *Node) {
1571   OS << "collapse(";
1572   Node->getNumForLoops()->printPretty(OS, nullptr, Policy, 0);
1573   OS << ")";
1574 }
1575 
1576 void OMPClausePrinter::VisitOMPDetachClause(OMPDetachClause *Node) {
1577   OS << "detach(";
1578   Node->getEventHandler()->printPretty(OS, nullptr, Policy, 0);
1579   OS << ")";
1580 }
1581 
1582 void OMPClausePrinter::VisitOMPDefaultClause(OMPDefaultClause *Node) {
1583   OS << "default("
1584      << getOpenMPSimpleClauseTypeName(OMPC_default,
1585                                       unsigned(Node->getDefaultKind()))
1586      << ")";
1587 }
1588 
1589 void OMPClausePrinter::VisitOMPProcBindClause(OMPProcBindClause *Node) {
1590   OS << "proc_bind("
1591      << getOpenMPSimpleClauseTypeName(OMPC_proc_bind,
1592                                       unsigned(Node->getProcBindKind()))
1593      << ")";
1594 }
1595 
1596 void OMPClausePrinter::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {
1597   OS << "unified_address";
1598 }
1599 
1600 void OMPClausePrinter::VisitOMPUnifiedSharedMemoryClause(
1601     OMPUnifiedSharedMemoryClause *) {
1602   OS << "unified_shared_memory";
1603 }
1604 
1605 void OMPClausePrinter::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {
1606   OS << "reverse_offload";
1607 }
1608 
1609 void OMPClausePrinter::VisitOMPDynamicAllocatorsClause(
1610     OMPDynamicAllocatorsClause *) {
1611   OS << "dynamic_allocators";
1612 }
1613 
1614 void OMPClausePrinter::VisitOMPAtomicDefaultMemOrderClause(
1615     OMPAtomicDefaultMemOrderClause *Node) {
1616   OS << "atomic_default_mem_order("
1617      << getOpenMPSimpleClauseTypeName(OMPC_atomic_default_mem_order,
1618                                       Node->getAtomicDefaultMemOrderKind())
1619      << ")";
1620 }
1621 
1622 void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {
1623   OS << "schedule(";
1624   if (Node->getFirstScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1625     OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1626                                         Node->getFirstScheduleModifier());
1627     if (Node->getSecondScheduleModifier() != OMPC_SCHEDULE_MODIFIER_unknown) {
1628       OS << ", ";
1629       OS << getOpenMPSimpleClauseTypeName(OMPC_schedule,
1630                                           Node->getSecondScheduleModifier());
1631     }
1632     OS << ": ";
1633   }
1634   OS << getOpenMPSimpleClauseTypeName(OMPC_schedule, Node->getScheduleKind());
1635   if (auto *E = Node->getChunkSize()) {
1636     OS << ", ";
1637     E->printPretty(OS, nullptr, Policy);
1638   }
1639   OS << ")";
1640 }
1641 
1642 void OMPClausePrinter::VisitOMPOrderedClause(OMPOrderedClause *Node) {
1643   OS << "ordered";
1644   if (auto *Num = Node->getNumForLoops()) {
1645     OS << "(";
1646     Num->printPretty(OS, nullptr, Policy, 0);
1647     OS << ")";
1648   }
1649 }
1650 
1651 void OMPClausePrinter::VisitOMPNowaitClause(OMPNowaitClause *) {
1652   OS << "nowait";
1653 }
1654 
1655 void OMPClausePrinter::VisitOMPUntiedClause(OMPUntiedClause *) {
1656   OS << "untied";
1657 }
1658 
1659 void OMPClausePrinter::VisitOMPNogroupClause(OMPNogroupClause *) {
1660   OS << "nogroup";
1661 }
1662 
1663 void OMPClausePrinter::VisitOMPMergeableClause(OMPMergeableClause *) {
1664   OS << "mergeable";
1665 }
1666 
1667 void OMPClausePrinter::VisitOMPReadClause(OMPReadClause *) { OS << "read"; }
1668 
1669 void OMPClausePrinter::VisitOMPWriteClause(OMPWriteClause *) { OS << "write"; }
1670 
1671 void OMPClausePrinter::VisitOMPUpdateClause(OMPUpdateClause *Node) {
1672   OS << "update";
1673   if (Node->isExtended()) {
1674     OS << "(";
1675     OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
1676                                         Node->getDependencyKind());
1677     OS << ")";
1678   }
1679 }
1680 
1681 void OMPClausePrinter::VisitOMPCaptureClause(OMPCaptureClause *) {
1682   OS << "capture";
1683 }
1684 
1685 void OMPClausePrinter::VisitOMPSeqCstClause(OMPSeqCstClause *) {
1686   OS << "seq_cst";
1687 }
1688 
1689 void OMPClausePrinter::VisitOMPAcqRelClause(OMPAcqRelClause *) {
1690   OS << "acq_rel";
1691 }
1692 
1693 void OMPClausePrinter::VisitOMPAcquireClause(OMPAcquireClause *) {
1694   OS << "acquire";
1695 }
1696 
1697 void OMPClausePrinter::VisitOMPReleaseClause(OMPReleaseClause *) {
1698   OS << "release";
1699 }
1700 
1701 void OMPClausePrinter::VisitOMPRelaxedClause(OMPRelaxedClause *) {
1702   OS << "relaxed";
1703 }
1704 
1705 void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
1706   OS << "threads";
1707 }
1708 
1709 void OMPClausePrinter::VisitOMPSIMDClause(OMPSIMDClause *) { OS << "simd"; }
1710 
1711 void OMPClausePrinter::VisitOMPDeviceClause(OMPDeviceClause *Node) {
1712   OS << "device(";
1713   OpenMPDeviceClauseModifier Modifier = Node->getModifier();
1714   if (Modifier != OMPC_DEVICE_unknown) {
1715     OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(), Modifier)
1716        << ": ";
1717   }
1718   Node->getDevice()->printPretty(OS, nullptr, Policy, 0);
1719   OS << ")";
1720 }
1721 
1722 void OMPClausePrinter::VisitOMPNumTeamsClause(OMPNumTeamsClause *Node) {
1723   OS << "num_teams(";
1724   Node->getNumTeams()->printPretty(OS, nullptr, Policy, 0);
1725   OS << ")";
1726 }
1727 
1728 void OMPClausePrinter::VisitOMPThreadLimitClause(OMPThreadLimitClause *Node) {
1729   OS << "thread_limit(";
1730   Node->getThreadLimit()->printPretty(OS, nullptr, Policy, 0);
1731   OS << ")";
1732 }
1733 
1734 void OMPClausePrinter::VisitOMPPriorityClause(OMPPriorityClause *Node) {
1735   OS << "priority(";
1736   Node->getPriority()->printPretty(OS, nullptr, Policy, 0);
1737   OS << ")";
1738 }
1739 
1740 void OMPClausePrinter::VisitOMPGrainsizeClause(OMPGrainsizeClause *Node) {
1741   OS << "grainsize(";
1742   Node->getGrainsize()->printPretty(OS, nullptr, Policy, 0);
1743   OS << ")";
1744 }
1745 
1746 void OMPClausePrinter::VisitOMPNumTasksClause(OMPNumTasksClause *Node) {
1747   OS << "num_tasks(";
1748   Node->getNumTasks()->printPretty(OS, nullptr, Policy, 0);
1749   OS << ")";
1750 }
1751 
1752 void OMPClausePrinter::VisitOMPHintClause(OMPHintClause *Node) {
1753   OS << "hint(";
1754   Node->getHint()->printPretty(OS, nullptr, Policy, 0);
1755   OS << ")";
1756 }
1757 
1758 void OMPClausePrinter::VisitOMPDestroyClause(OMPDestroyClause *) {
1759   OS << "destroy";
1760 }
1761 
1762 template<typename T>
1763 void OMPClausePrinter::VisitOMPClauseList(T *Node, char StartSym) {
1764   for (typename T::varlist_iterator I = Node->varlist_begin(),
1765                                     E = Node->varlist_end();
1766        I != E; ++I) {
1767     assert(*I && "Expected non-null Stmt");
1768     OS << (I == Node->varlist_begin() ? StartSym : ',');
1769     if (auto *DRE = dyn_cast<DeclRefExpr>(*I)) {
1770       if (isa<OMPCapturedExprDecl>(DRE->getDecl()))
1771         DRE->printPretty(OS, nullptr, Policy, 0);
1772       else
1773         DRE->getDecl()->printQualifiedName(OS);
1774     } else
1775       (*I)->printPretty(OS, nullptr, Policy, 0);
1776   }
1777 }
1778 
1779 void OMPClausePrinter::VisitOMPAllocateClause(OMPAllocateClause *Node) {
1780   if (Node->varlist_empty())
1781     return;
1782   OS << "allocate";
1783   if (Expr *Allocator = Node->getAllocator()) {
1784     OS << "(";
1785     Allocator->printPretty(OS, nullptr, Policy, 0);
1786     OS << ":";
1787     VisitOMPClauseList(Node, ' ');
1788   } else {
1789     VisitOMPClauseList(Node, '(');
1790   }
1791   OS << ")";
1792 }
1793 
1794 void OMPClausePrinter::VisitOMPPrivateClause(OMPPrivateClause *Node) {
1795   if (!Node->varlist_empty()) {
1796     OS << "private";
1797     VisitOMPClauseList(Node, '(');
1798     OS << ")";
1799   }
1800 }
1801 
1802 void OMPClausePrinter::VisitOMPFirstprivateClause(OMPFirstprivateClause *Node) {
1803   if (!Node->varlist_empty()) {
1804     OS << "firstprivate";
1805     VisitOMPClauseList(Node, '(');
1806     OS << ")";
1807   }
1808 }
1809 
1810 void OMPClausePrinter::VisitOMPLastprivateClause(OMPLastprivateClause *Node) {
1811   if (!Node->varlist_empty()) {
1812     OS << "lastprivate";
1813     OpenMPLastprivateModifier LPKind = Node->getKind();
1814     if (LPKind != OMPC_LASTPRIVATE_unknown) {
1815       OS << "("
1816          << getOpenMPSimpleClauseTypeName(OMPC_lastprivate, Node->getKind())
1817          << ":";
1818     }
1819     VisitOMPClauseList(Node, LPKind == OMPC_LASTPRIVATE_unknown ? '(' : ' ');
1820     OS << ")";
1821   }
1822 }
1823 
1824 void OMPClausePrinter::VisitOMPSharedClause(OMPSharedClause *Node) {
1825   if (!Node->varlist_empty()) {
1826     OS << "shared";
1827     VisitOMPClauseList(Node, '(');
1828     OS << ")";
1829   }
1830 }
1831 
1832 void OMPClausePrinter::VisitOMPReductionClause(OMPReductionClause *Node) {
1833   if (!Node->varlist_empty()) {
1834     OS << "reduction(";
1835     if (Node->getModifierLoc().isValid())
1836       OS << getOpenMPSimpleClauseTypeName(OMPC_reduction, Node->getModifier())
1837          << ", ";
1838     NestedNameSpecifier *QualifierLoc =
1839         Node->getQualifierLoc().getNestedNameSpecifier();
1840     OverloadedOperatorKind OOK =
1841         Node->getNameInfo().getName().getCXXOverloadedOperator();
1842     if (QualifierLoc == nullptr && OOK != OO_None) {
1843       // Print reduction identifier in C format
1844       OS << getOperatorSpelling(OOK);
1845     } else {
1846       // Use C++ format
1847       if (QualifierLoc != nullptr)
1848         QualifierLoc->print(OS, Policy);
1849       OS << Node->getNameInfo();
1850     }
1851     OS << ":";
1852     VisitOMPClauseList(Node, ' ');
1853     OS << ")";
1854   }
1855 }
1856 
1857 void OMPClausePrinter::VisitOMPTaskReductionClause(
1858     OMPTaskReductionClause *Node) {
1859   if (!Node->varlist_empty()) {
1860     OS << "task_reduction(";
1861     NestedNameSpecifier *QualifierLoc =
1862         Node->getQualifierLoc().getNestedNameSpecifier();
1863     OverloadedOperatorKind OOK =
1864         Node->getNameInfo().getName().getCXXOverloadedOperator();
1865     if (QualifierLoc == nullptr && OOK != OO_None) {
1866       // Print reduction identifier in C format
1867       OS << getOperatorSpelling(OOK);
1868     } else {
1869       // Use C++ format
1870       if (QualifierLoc != nullptr)
1871         QualifierLoc->print(OS, Policy);
1872       OS << Node->getNameInfo();
1873     }
1874     OS << ":";
1875     VisitOMPClauseList(Node, ' ');
1876     OS << ")";
1877   }
1878 }
1879 
1880 void OMPClausePrinter::VisitOMPInReductionClause(OMPInReductionClause *Node) {
1881   if (!Node->varlist_empty()) {
1882     OS << "in_reduction(";
1883     NestedNameSpecifier *QualifierLoc =
1884         Node->getQualifierLoc().getNestedNameSpecifier();
1885     OverloadedOperatorKind OOK =
1886         Node->getNameInfo().getName().getCXXOverloadedOperator();
1887     if (QualifierLoc == nullptr && OOK != OO_None) {
1888       // Print reduction identifier in C format
1889       OS << getOperatorSpelling(OOK);
1890     } else {
1891       // Use C++ format
1892       if (QualifierLoc != nullptr)
1893         QualifierLoc->print(OS, Policy);
1894       OS << Node->getNameInfo();
1895     }
1896     OS << ":";
1897     VisitOMPClauseList(Node, ' ');
1898     OS << ")";
1899   }
1900 }
1901 
1902 void OMPClausePrinter::VisitOMPLinearClause(OMPLinearClause *Node) {
1903   if (!Node->varlist_empty()) {
1904     OS << "linear";
1905     if (Node->getModifierLoc().isValid()) {
1906       OS << '('
1907          << getOpenMPSimpleClauseTypeName(OMPC_linear, Node->getModifier());
1908     }
1909     VisitOMPClauseList(Node, '(');
1910     if (Node->getModifierLoc().isValid())
1911       OS << ')';
1912     if (Node->getStep() != nullptr) {
1913       OS << ": ";
1914       Node->getStep()->printPretty(OS, nullptr, Policy, 0);
1915     }
1916     OS << ")";
1917   }
1918 }
1919 
1920 void OMPClausePrinter::VisitOMPAlignedClause(OMPAlignedClause *Node) {
1921   if (!Node->varlist_empty()) {
1922     OS << "aligned";
1923     VisitOMPClauseList(Node, '(');
1924     if (Node->getAlignment() != nullptr) {
1925       OS << ": ";
1926       Node->getAlignment()->printPretty(OS, nullptr, Policy, 0);
1927     }
1928     OS << ")";
1929   }
1930 }
1931 
1932 void OMPClausePrinter::VisitOMPCopyinClause(OMPCopyinClause *Node) {
1933   if (!Node->varlist_empty()) {
1934     OS << "copyin";
1935     VisitOMPClauseList(Node, '(');
1936     OS << ")";
1937   }
1938 }
1939 
1940 void OMPClausePrinter::VisitOMPCopyprivateClause(OMPCopyprivateClause *Node) {
1941   if (!Node->varlist_empty()) {
1942     OS << "copyprivate";
1943     VisitOMPClauseList(Node, '(');
1944     OS << ")";
1945   }
1946 }
1947 
1948 void OMPClausePrinter::VisitOMPFlushClause(OMPFlushClause *Node) {
1949   if (!Node->varlist_empty()) {
1950     VisitOMPClauseList(Node, '(');
1951     OS << ")";
1952   }
1953 }
1954 
1955 void OMPClausePrinter::VisitOMPDepobjClause(OMPDepobjClause *Node) {
1956   OS << "(";
1957   Node->getDepobj()->printPretty(OS, nullptr, Policy, 0);
1958   OS << ")";
1959 }
1960 
1961 void OMPClausePrinter::VisitOMPDependClause(OMPDependClause *Node) {
1962   OS << "depend(";
1963   if (Expr *DepModifier = Node->getModifier()) {
1964     DepModifier->printPretty(OS, nullptr, Policy);
1965     OS << ", ";
1966   }
1967   OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
1968                                       Node->getDependencyKind());
1969   if (!Node->varlist_empty()) {
1970     OS << " :";
1971     VisitOMPClauseList(Node, ' ');
1972   }
1973   OS << ")";
1974 }
1975 
1976 template <typename T>
1977 static void PrintMapper(raw_ostream &OS, T *Node,
1978                         const PrintingPolicy &Policy) {
1979   OS << '(';
1980   NestedNameSpecifier *MapperNNS =
1981       Node->getMapperQualifierLoc().getNestedNameSpecifier();
1982   if (MapperNNS)
1983     MapperNNS->print(OS, Policy);
1984   OS << Node->getMapperIdInfo() << ')';
1985 }
1986 
1987 void OMPClausePrinter::VisitOMPMapClause(OMPMapClause *Node) {
1988   if (!Node->varlist_empty()) {
1989     OS << "map(";
1990     if (Node->getMapType() != OMPC_MAP_unknown) {
1991       for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
1992         if (Node->getMapTypeModifier(I) != OMPC_MAP_MODIFIER_unknown) {
1993           OS << getOpenMPSimpleClauseTypeName(OMPC_map,
1994                                               Node->getMapTypeModifier(I));
1995           if (Node->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_mapper)
1996             PrintMapper(OS, Node, Policy);
1997           OS << ',';
1998         }
1999       }
2000       OS << getOpenMPSimpleClauseTypeName(OMPC_map, Node->getMapType());
2001       OS << ':';
2002     }
2003     VisitOMPClauseList(Node, ' ');
2004     OS << ")";
2005   }
2006 }
2007 
2008 template <typename T> void OMPClausePrinter::VisitOMPMotionClause(T *Node) {
2009   if (Node->varlist_empty())
2010     return;
2011   OS << getOpenMPClauseName(Node->getClauseKind());
2012   unsigned ModifierCount = 0;
2013   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
2014     if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown)
2015       ++ModifierCount;
2016   }
2017   if (ModifierCount) {
2018     OS << '(';
2019     for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
2020       if (Node->getMotionModifier(I) != OMPC_MOTION_MODIFIER_unknown) {
2021         OS << getOpenMPSimpleClauseTypeName(Node->getClauseKind(),
2022                                             Node->getMotionModifier(I));
2023         if (Node->getMotionModifier(I) == OMPC_MOTION_MODIFIER_mapper)
2024           PrintMapper(OS, Node, Policy);
2025         if (I < ModifierCount - 1)
2026           OS << ", ";
2027       }
2028     }
2029     OS << ':';
2030     VisitOMPClauseList(Node, ' ');
2031   } else {
2032     VisitOMPClauseList(Node, '(');
2033   }
2034   OS << ")";
2035 }
2036 
2037 void OMPClausePrinter::VisitOMPToClause(OMPToClause *Node) {
2038   VisitOMPMotionClause(Node);
2039 }
2040 
2041 void OMPClausePrinter::VisitOMPFromClause(OMPFromClause *Node) {
2042   VisitOMPMotionClause(Node);
2043 }
2044 
2045 void OMPClausePrinter::VisitOMPDistScheduleClause(OMPDistScheduleClause *Node) {
2046   OS << "dist_schedule(" << getOpenMPSimpleClauseTypeName(
2047                            OMPC_dist_schedule, Node->getDistScheduleKind());
2048   if (auto *E = Node->getChunkSize()) {
2049     OS << ", ";
2050     E->printPretty(OS, nullptr, Policy);
2051   }
2052   OS << ")";
2053 }
2054 
2055 void OMPClausePrinter::VisitOMPDefaultmapClause(OMPDefaultmapClause *Node) {
2056   OS << "defaultmap(";
2057   OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
2058                                       Node->getDefaultmapModifier());
2059   if (Node->getDefaultmapKind() != OMPC_DEFAULTMAP_unknown) {
2060     OS << ": ";
2061     OS << getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
2062                                         Node->getDefaultmapKind());
2063   }
2064   OS << ")";
2065 }
2066 
2067 void OMPClausePrinter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *Node) {
2068   if (!Node->varlist_empty()) {
2069     OS << "use_device_ptr";
2070     VisitOMPClauseList(Node, '(');
2071     OS << ")";
2072   }
2073 }
2074 
2075 void OMPClausePrinter::VisitOMPUseDeviceAddrClause(
2076     OMPUseDeviceAddrClause *Node) {
2077   if (!Node->varlist_empty()) {
2078     OS << "use_device_addr";
2079     VisitOMPClauseList(Node, '(');
2080     OS << ")";
2081   }
2082 }
2083 
2084 void OMPClausePrinter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *Node) {
2085   if (!Node->varlist_empty()) {
2086     OS << "is_device_ptr";
2087     VisitOMPClauseList(Node, '(');
2088     OS << ")";
2089   }
2090 }
2091 
2092 void OMPClausePrinter::VisitOMPNontemporalClause(OMPNontemporalClause *Node) {
2093   if (!Node->varlist_empty()) {
2094     OS << "nontemporal";
2095     VisitOMPClauseList(Node, '(');
2096     OS << ")";
2097   }
2098 }
2099 
2100 void OMPClausePrinter::VisitOMPOrderClause(OMPOrderClause *Node) {
2101   OS << "order(" << getOpenMPSimpleClauseTypeName(OMPC_order, Node->getKind())
2102      << ")";
2103 }
2104 
2105 void OMPClausePrinter::VisitOMPInclusiveClause(OMPInclusiveClause *Node) {
2106   if (!Node->varlist_empty()) {
2107     OS << "inclusive";
2108     VisitOMPClauseList(Node, '(');
2109     OS << ")";
2110   }
2111 }
2112 
2113 void OMPClausePrinter::VisitOMPExclusiveClause(OMPExclusiveClause *Node) {
2114   if (!Node->varlist_empty()) {
2115     OS << "exclusive";
2116     VisitOMPClauseList(Node, '(');
2117     OS << ")";
2118   }
2119 }
2120 
2121 void OMPClausePrinter::VisitOMPUsesAllocatorsClause(
2122     OMPUsesAllocatorsClause *Node) {
2123   if (Node->getNumberOfAllocators() == 0)
2124     return;
2125   OS << "uses_allocators(";
2126   for (unsigned I = 0, E = Node->getNumberOfAllocators(); I < E; ++I) {
2127     OMPUsesAllocatorsClause::Data Data = Node->getAllocatorData(I);
2128     Data.Allocator->printPretty(OS, nullptr, Policy);
2129     if (Data.AllocatorTraits) {
2130       OS << "(";
2131       Data.AllocatorTraits->printPretty(OS, nullptr, Policy);
2132       OS << ")";
2133     }
2134     if (I < E - 1)
2135       OS << ",";
2136   }
2137   OS << ")";
2138 }
2139 
2140 void OMPClausePrinter::VisitOMPAffinityClause(OMPAffinityClause *Node) {
2141   if (Node->varlist_empty())
2142     return;
2143   OS << "affinity";
2144   char StartSym = '(';
2145   if (Expr *Modifier = Node->getModifier()) {
2146     OS << "(";
2147     Modifier->printPretty(OS, nullptr, Policy);
2148     OS << " :";
2149     StartSym = ' ';
2150   }
2151   VisitOMPClauseList(Node, StartSym);
2152   OS << ")";
2153 }
2154 
2155 void OMPTraitInfo::getAsVariantMatchInfo(ASTContext &ASTCtx,
2156                                          VariantMatchInfo &VMI) const {
2157   for (const OMPTraitSet &Set : Sets) {
2158     for (const OMPTraitSelector &Selector : Set.Selectors) {
2159 
2160       // User conditions are special as we evaluate the condition here.
2161       if (Selector.Kind == TraitSelector::user_condition) {
2162         assert(Selector.ScoreOrCondition &&
2163                "Ill-formed user condition, expected condition expression!");
2164         assert(Selector.Properties.size() == 1 &&
2165                Selector.Properties.front().Kind ==
2166                    TraitProperty::user_condition_unknown &&
2167                "Ill-formed user condition, expected unknown trait property!");
2168 
2169         if (Optional<APSInt> CondVal =
2170                 Selector.ScoreOrCondition->getIntegerConstantExpr(ASTCtx))
2171           VMI.addTrait(CondVal->isNullValue()
2172                            ? TraitProperty::user_condition_false
2173                            : TraitProperty::user_condition_true,
2174                        "<condition>");
2175         else
2176           VMI.addTrait(TraitProperty::user_condition_false, "<condition>");
2177         continue;
2178       }
2179 
2180       Optional<llvm::APSInt> Score;
2181       llvm::APInt *ScorePtr = nullptr;
2182       if (Selector.ScoreOrCondition) {
2183         if ((Score = Selector.ScoreOrCondition->getIntegerConstantExpr(ASTCtx)))
2184           ScorePtr = &*Score;
2185         else
2186           VMI.addTrait(TraitProperty::user_condition_false,
2187                        "<non-constant-score>");
2188       }
2189 
2190       for (const OMPTraitProperty &Property : Selector.Properties)
2191         VMI.addTrait(Set.Kind, Property.Kind, Property.RawString, ScorePtr);
2192 
2193       if (Set.Kind != TraitSet::construct)
2194         continue;
2195 
2196       // TODO: This might not hold once we implement SIMD properly.
2197       assert(Selector.Properties.size() == 1 &&
2198              Selector.Properties.front().Kind ==
2199                  getOpenMPContextTraitPropertyForSelector(
2200                      Selector.Kind) &&
2201              "Ill-formed construct selector!");
2202 
2203       VMI.ConstructTraits.push_back(Selector.Properties.front().Kind);
2204     }
2205   }
2206 }
2207 
2208 void OMPTraitInfo::print(llvm::raw_ostream &OS,
2209                          const PrintingPolicy &Policy) const {
2210   bool FirstSet = true;
2211   for (const OMPTraitSet &Set : Sets) {
2212     if (!FirstSet)
2213       OS << ", ";
2214     FirstSet = false;
2215     OS << getOpenMPContextTraitSetName(Set.Kind) << "={";
2216 
2217     bool FirstSelector = true;
2218     for (const OMPTraitSelector &Selector : Set.Selectors) {
2219       if (!FirstSelector)
2220         OS << ", ";
2221       FirstSelector = false;
2222       OS << getOpenMPContextTraitSelectorName(Selector.Kind);
2223 
2224       bool AllowsTraitScore = false;
2225       bool RequiresProperty = false;
2226       isValidTraitSelectorForTraitSet(
2227           Selector.Kind, Set.Kind, AllowsTraitScore, RequiresProperty);
2228 
2229       if (!RequiresProperty)
2230         continue;
2231 
2232       OS << "(";
2233       if (Selector.Kind == TraitSelector::user_condition) {
2234         if (Selector.ScoreOrCondition)
2235           Selector.ScoreOrCondition->printPretty(OS, nullptr, Policy);
2236         else
2237           OS << "...";
2238       } else {
2239 
2240         if (Selector.ScoreOrCondition) {
2241           OS << "score(";
2242           Selector.ScoreOrCondition->printPretty(OS, nullptr, Policy);
2243           OS << "): ";
2244         }
2245 
2246         bool FirstProperty = true;
2247         for (const OMPTraitProperty &Property : Selector.Properties) {
2248           if (!FirstProperty)
2249             OS << ", ";
2250           FirstProperty = false;
2251           OS << getOpenMPContextTraitPropertyName(Property.Kind,
2252                                                   Property.RawString);
2253         }
2254       }
2255       OS << ")";
2256     }
2257     OS << "}";
2258   }
2259 }
2260 
2261 std::string OMPTraitInfo::getMangledName() const {
2262   std::string MangledName;
2263   llvm::raw_string_ostream OS(MangledName);
2264   for (const OMPTraitSet &Set : Sets) {
2265     OS << '$' << 'S' << unsigned(Set.Kind);
2266     for (const OMPTraitSelector &Selector : Set.Selectors) {
2267 
2268       bool AllowsTraitScore = false;
2269       bool RequiresProperty = false;
2270       isValidTraitSelectorForTraitSet(
2271           Selector.Kind, Set.Kind, AllowsTraitScore, RequiresProperty);
2272       OS << '$' << 's' << unsigned(Selector.Kind);
2273 
2274       if (!RequiresProperty ||
2275           Selector.Kind == TraitSelector::user_condition)
2276         continue;
2277 
2278       for (const OMPTraitProperty &Property : Selector.Properties)
2279         OS << '$' << 'P'
2280            << getOpenMPContextTraitPropertyName(Property.Kind,
2281                                                 Property.RawString);
2282     }
2283   }
2284   return OS.str();
2285 }
2286 
2287 OMPTraitInfo::OMPTraitInfo(StringRef MangledName) {
2288   unsigned long U;
2289   do {
2290     if (!MangledName.consume_front("$S"))
2291       break;
2292     if (MangledName.consumeInteger(10, U))
2293       break;
2294     Sets.push_back(OMPTraitSet());
2295     OMPTraitSet &Set = Sets.back();
2296     Set.Kind = TraitSet(U);
2297     do {
2298       if (!MangledName.consume_front("$s"))
2299         break;
2300       if (MangledName.consumeInteger(10, U))
2301         break;
2302       Set.Selectors.push_back(OMPTraitSelector());
2303       OMPTraitSelector &Selector = Set.Selectors.back();
2304       Selector.Kind = TraitSelector(U);
2305       do {
2306         if (!MangledName.consume_front("$P"))
2307           break;
2308         Selector.Properties.push_back(OMPTraitProperty());
2309         OMPTraitProperty &Property = Selector.Properties.back();
2310         std::pair<StringRef, StringRef> PropRestPair = MangledName.split('$');
2311         Property.RawString = PropRestPair.first;
2312         Property.Kind = getOpenMPContextTraitPropertyKind(
2313             Set.Kind, Selector.Kind, PropRestPair.first);
2314         MangledName = MangledName.drop_front(PropRestPair.first.size());
2315       } while (true);
2316     } while (true);
2317   } while (true);
2318 }
2319 
2320 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
2321                                      const OMPTraitInfo &TI) {
2322   LangOptions LO;
2323   PrintingPolicy Policy(LO);
2324   TI.print(OS, Policy);
2325   return OS;
2326 }
2327 llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
2328                                      const OMPTraitInfo *TI) {
2329   return TI ? OS << *TI : OS;
2330 }
2331 
2332 TargetOMPContext::TargetOMPContext(
2333     ASTContext &ASTCtx, std::function<void(StringRef)> &&DiagUnknownTrait,
2334     const FunctionDecl *CurrentFunctionDecl)
2335     : OMPContext(ASTCtx.getLangOpts().OpenMPIsDevice,
2336                  ASTCtx.getTargetInfo().getTriple()),
2337       FeatureValidityCheck([&](StringRef FeatureName) {
2338         return ASTCtx.getTargetInfo().isValidFeatureName(FeatureName);
2339       }),
2340       DiagUnknownTrait(std::move(DiagUnknownTrait)) {
2341   ASTCtx.getFunctionFeatureMap(FeatureMap, CurrentFunctionDecl);
2342 }
2343 
2344 bool TargetOMPContext::matchesISATrait(StringRef RawString) const {
2345   auto It = FeatureMap.find(RawString);
2346   if (It != FeatureMap.end())
2347     return It->second;
2348   if (!FeatureValidityCheck(RawString))
2349     DiagUnknownTrait(RawString);
2350   return false;
2351 }
2352