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